W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
限流器中間件,用于服務(wù)端流量控制,默認(rèn)使用bbr limiter算法。
WithLimiter
?替換默認(rèn)的限流算法
// WithLimiter set Limiter implementation,
// default is bbr limiter
func WithLimiter(limiter ratelimit.Limiter) Option {
return func(o *options) {
o.limiter = limiter
}
}
所提供的限流器需要實(shí)現(xiàn) aegis 的 Limiter 接口, 更多信息可以參考aegis/ratelimit
// Limiter is a rate limiter.
type Limiter interface {
Allow() (DoneFunc, error)
}
var opts = []http.ServerOption{
http.Middleware(
ratelimit.Server(),
),
}
srv := http.NewServer(opts...)
當(dāng)觸發(fā)限流器時(shí),會(huì)直接拒絕當(dāng)前請(qǐng)求,并返回錯(cuò)誤?ErrLimitExceed
?,定義如下:
// ErrLimitExceed is service unavailable due to rate limit exceeded.
var ErrLimitExceed = errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded")
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: