ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
- https://blog.csdn.net/qq23001186/article/details/126357542 ~~~ package main import ( "fmt" "log" sentinel "github.com/alibaba/sentinel-golang/api" "github.com/alibaba/sentinel-golang/core/base" "github.com/alibaba/sentinel-golang/core/flow" ) func main() { //基于sentinel的qps限流 //必须初始化 err := sentinel.InitDefault() if err != nil { log.Fatalf("Unexpected error: %+v", err) } //配置限流规则:1秒内通过10次 _, err = flow.LoadRules([]*flow.Rule{ { Resource: "some_test", TokenCalculateStrategy: flow.Direct, ControlBehavior: flow.Reject, //超过直接拒绝 Threshold: 10, //请求次数 StatIntervalInMs: 1000, //允许时间内 }, }) if err != nil { log.Fatalf("Unexpected error: %+v", err) return } for i := 0; i < 12; i++ { e, b := sentinel.Entry("some_test", sentinel.WithTrafficType(base.Inbound)) if b != nil { fmt.Println("限流了") } else { fmt.Println("检查通过") e.Exit() } } } ~~~