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.org/x/time/rate包中的rate.Limiter可基于令牌桶算法实现HTTP请求限流,支持全局限流或按客户端IP独立限流,结合中间件和定期清理机制保障服务稳定性。

如何在golang中实现http请求限流

在Golang中实现HTTP请求限流,主要是为了防止服务被过多请求压垮,保障系统稳定性。常用的方式是使用令牌桶或漏桶算法,结合 golang.org/x/time/rate 包来实现简单高效的限流控制。

使用 golang.org/x/time/rate 实现限流

rate.Limiter 是 Go 官方提供的限流工具,基于令牌桶算法,使用简单且性能良好。你可以为每个请求者(如IP)分配一个限流器,也可以全局限流。

示例:对所有请求统一限流

package main

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

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

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

func limit(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {if !limiter.Allow() {http.StatusText(http.StatusTooManyRequests)w.WriteHeader(http.StatusTooManyRequests)w.Write([]byte("too many requests"))return}next(w, r)}}

func handler(w http.ResponseWriter, r *http.Request) {w.Write([]byte("Hello, limited world!"))}

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

基于客户端IP的独立限流

更常见的场景是为每个客户端IP设置独立的限流策略,避免某个用户影响其他用户。

可以使用 map + sync.Mutex 或 sync.Map 来存储每个IP对应的限流器。

var visitors = make(map[string]*rate.Limiter)var mu sync.RWMutex

func getVisitorLimiter(ip string) *rate.Limiter {mu.RLock()limiter, exists := visitors[ip]mu.RUnlock()if exists {return limiter}

mu.Lock()// 双检确认,避免重复创建if limiter, exists = visitors[ip]; exists {    mu.Unlock()    return limiter}limiter = rate.NewLimiter(2, 5) // 每秒2次请求,最多5个突发visitors[ip] = limitermu.Unlock()return limiter

}

func ipLimit(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {ip := r.RemoteAddr // 注意:反向代理时可能需要读取 X-Forwarded-Forlimiter := getVisitorLimiter(ip)

    if !limiter.Allow() {        http.StatusText(http.StatusTooManyRequests)        w.WriteHeader(http.StatusTooManyRequests)        w.Write([]byte("too many requests"))        return    }    next(w, r)}

}

定期清理过期的限流器

如果不限期清理 map 中的旧IP记录,内存会持续增长。可以通过启动一个后台协程定期清理长时间未活动的IP。

例如:每5分钟清理超过30分钟无请求的IP

func cleanupVisitors() {    for {        time.Sleep(5 * time.Minute)        mu.Lock()        for ip, limiter := range visitors {            // 假设我们通过 LastSeen 时间判断,这里简化为检查是否“空闲”            // 更完整可扩展结构体包含 lastSeen 字段            if limiter.Tokens() == float64(limiter.Burst()) {                delete(visitors, ip)            }        }        mu.Unlock()    }}

// 在 main 中启动清理func main() {go cleanupVisitors()// ...}

基本上就这些。使用 rate.Limiter 配合中间件方式,可以灵活实现全局或按IP的HTTP请求限流。关键是根据业务需求设置合理的速率和突发值,并注意内存管理。

以上就是如何在Golang中实现HTTP请求限流的详细内容,更多请关注创想鸟其它相关文章!

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Go语言中如何扩展或修改第三方包函数:替代方案与实践
上一篇 2025年12月16日 11:17:10
Go语言指针操作:如何正确修改通过指针传递的字符串值
下一篇 2025年12月16日 11:17:23

相关推荐

发表回复

登录后才能评论
关注微信