Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang HTTP请求限流与并发控制项目_创想鸟

Golang HTTP请求限流与并发控制项目

使用令牌桶和信号量机制控制限流与并发,保障Golang HTTP服务稳定性。首先通过golang.org/x/time/rate实现每秒10个请求、突发50的令牌桶限流;接着用带缓冲channel(容量5)限制最大并发数,防止资源耗尽;最后结合IP级限流管理器,按IP维度分配独立令牌桶,并定期清理长时间未活跃的IP记录,实现精细化控制。实际应用中建议结合监控动态调整阈值。

golang http请求限流与并发控制项目

在构建高并发的 Golang HTTP 服务时,控制请求频率和并发量是保障系统稳定的关键。不加限制的请求可能导致后端资源耗尽、响应延迟上升甚至服务崩溃。通过合理的限流与并发控制机制,可以有效保护服务稳定性,提升用户体验。

使用令牌桶实现HTTP请求限流

令牌桶算法是一种常见且灵活的限流方式,它允许一定程度的突发流量,同时保证平均速率不超过设定值。Golang 标准库 golang.org/x/time/rate 提供了开箱即用的令牌桶实现。

以下是一个基于 rate.Limiter 的中间件示例:

package main

import ("golang.org/x/time/rate""net/http""time")

var limiter = rate.NewLimiter(10, 50) // 每秒10个令牌,最多容纳50个

立即学习“go语言免费学习笔记(深入)”;

func rateLimit(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {if !limiter.Allow() {http.StatusText(http.StatusTooManyRequests)http.Error(w, "请求过于频繁", http.StatusTooManyRequests)return}next.ServeHTTP(w, r)}}

func handler(w http.ResponseWriter, r *http.Request) {w.Write([]byte("处理请求"))}

func main() {mux := http.NewServeMux()mux.HandleFunc("/", rateLimit(handler))http.ListenAndServe(":8080", mux)}

上面代码中,每秒最多处理10个请求,最多可积压40个(burst=50)。适合用于API接口防刷或防止爬虫滥用。

控制最大并发请求数

除了按时间频率限流,有时需要硬性限制同时处理的请求数量,防止资源(如数据库连接、内存)被耗尽。可以通过带缓冲的 channel 实现信号量机制。

示例:限制最多同时处理5个请求

var sem = make(chan struct{}, 5)

func concurrencyLimit(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {select {case sem <- struct{}{}:// 获取执行权default:http.Error(w, "服务繁忙,请稍后再试", http.StatusServiceUnavailable)return}defer func() { <-sem }() // 释放next.ServeHTTP(w, r)}}

该方法简单有效,适用于 IO 密集型任务较多、资源敏感的服务场景。

结合限流与并发控制的完整结构

实际项目中,通常将两种策略结合使用。例如:先通过并发控制防止系统过载,再用限流器平滑请求分布。

可以封装一个通用的限流管理器,支持按 IP 或用户维度进行控制:

type IpLimiter struct {    mu sync.RWMutex    limiters map[string]*rate.Limiter    lastSeen map[string]time.Time    r  float64    b int}

func NewIpLimiter(r float64, b int) IpLimiter {il := &IpLimiter{limiters: make(map[string]rate.Limiter),lastSeen: make(map[string]time.Time),r: r,b: b,}go il.cleanup()return il}

func (il IpLimiter) GetLimiter(ip string) rate.Limiter {il.mu.Lock()defer il.mu.Unlock()

limiter, exists := il.limiters[ip]if !exists {    limiter = rate.NewLimiter(il.r, il.b)    il.limiters[ip] = limiter    il.lastSeen[ip] = time.Now()} else {    il.lastSeen[ip] = time.Now()}return limiter

}

func (il IpLimiter) cleanup() {for {time.Sleep(time.Minute)il.mu.Lock()for ip, last := range il.lastSeen {if time.Since(last) > 3time.Minute {delete(il.limiters, ip)delete(il.lastSeen, ip)}}il.mu.Unlock()}}

在中间件中调用:

var ipLimiter = NewIpLimiter(1, 5) // 每秒1次,最多5次突发

func limitByIP(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {ip := r.RemoteAddrif !ipLimiter.GetLimiter(ip).Allow() {http.Error(w, "访问过于频繁", http.StatusTooManyRequests)return}next(w, r)}}

基本上就这些。通过合理组合令牌桶限流与 channel 控制并发,可以构建出稳定可靠的 HTTP 服务。关键是根据业务场景选择合适的阈值,并配合监控及时调整策略。

以上就是Golang HTTP请求限流与并发控制项目的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1412289.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golang如何实现微服务自动注册与发现
上一篇 2025年12月16日 06:12:07
日志收集与IO操作优化实践
下一篇 2025年12月16日 06:12:25

相关推荐

发表回复

登录后才能评论
关注微信