mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
add example for http rate llimit (#3072)
This commit is contained in:
@ -21,6 +21,7 @@ require (
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||
github.com/nacos-group/nacos-sdk-go v1.1.4
|
||||
github.com/polarismesh/polaris-go v1.5.4
|
||||
golang.org/x/time v0.3.0
|
||||
google.golang.org/grpc v1.58.2
|
||||
google.golang.org/protobuf v1.31.0
|
||||
k8s.io/client-go v0.27.4
|
||||
@ -123,7 +124,6 @@ require (
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
golang.org/x/term v0.13.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20230920204549-e6e6cdab5c13 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
|
||||
|
||||
@ -977,6 +977,8 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 h1:vVKdlvoWBphwdxWKrFZEuM0kGgGLxUOYcY4U/2Vjg44=
|
||||
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
|
||||
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
|
||||
52
example/httpserver/rate/main.go
Normal file
52
example/httpserver/rate/main.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
type HelloReq struct {
|
||||
g.Meta `path:"/hello" method:"get" sort:"1"`
|
||||
Name string `v:"required" dc:"Your name"`
|
||||
}
|
||||
|
||||
type HelloRes struct {
|
||||
Reply string `dc:"Reply content"`
|
||||
}
|
||||
|
||||
type Hello struct{}
|
||||
|
||||
func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
|
||||
g.Log().Debugf(ctx, `receive say: %+v`, req)
|
||||
res = &HelloRes{
|
||||
Reply: fmt.Sprintf(`Hi %s`, req.Name),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var limiter = rate.NewLimiter(rate.Limit(10), 1) // 10 request per second
|
||||
|
||||
func Limiter(r *ghttp.Request) {
|
||||
if !limiter.Allow() {
|
||||
r.Response.WriteStatusExit(429)
|
||||
r.ExitAll()
|
||||
}
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// curl "http://127.0.0.1:8080/hello?name=world"
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Use(Limiter, ghttp.MiddlewareHandlerResponse)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Bind(
|
||||
new(Hello),
|
||||
)
|
||||
})
|
||||
s.SetPort(8080)
|
||||
s.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user