Skip to content

Commit

Permalink
add doc of the rate limiter middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
faycheng committed May 20, 2019
1 parent b9580ac commit 9acc8c5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/wiki-cn/blademaster-mid.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ func Example() {
}
```

# 内置中间件

## 自适应限流

更多关于自适应限流的信息,请参考:[kratos 自适应限流](/doc/wiki-cn/ratelimit.md)

```go
func Example() {
myHandler := func(ctx *bm.Context) {
mid := metadata.Int64(ctx, metadata.Mid)
ctx.JSON(fmt.Sprintf("%d", mid), nil)
}


e := bm.DefaultServer(nil)

// 挂载自适应限流中间件到 bm engine,使用默认配置
limiter := bm.NewRateLimiter(nil)
e.Use(limiter.Limit())

e.GET("/user", myHandler)

e.Start()
}
```

# 扩展阅读

[bm快速开始](blademaster-quickstart.md) [bm模块说明](blademaster-mod.md) [bm基于pb生成](blademaster-pb.md)
Expand Down
46 changes: 46 additions & 0 deletions doc/wiki-cn/warden-mid.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,52 @@ func serverLogging() grpc.UnaryServerInterceptor {
}
```

# 内置拦截器

## 自适应限流拦截器

更多关于自适应限流的信息,请参考:[kratos 自适应限流](/doc/wiki-cn/ratelimit.md)

```go
package grpc

import (
pb "kratos-demo/api"
"kratos-demo/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/net/rpc/warden"
"github.com/bilibili/kratos/pkg/net/rpc/warden/ratelimiter"
)

// New new a grpc server.
func New(svc *service.Service) *warden.Server {
var rc struct {
Server *warden.ServerConfig
}
if err := paladin.Get("grpc.toml").UnmarshalTOML(&rc); err != nil {
if err != paladin.ErrNotExist {
panic(err)
}
}
ws := warden.NewServer(rc.Server)

// 挂载自适应限流拦截器到 warden server,使用默认配置
limiter := ratelimiter.New(nil)
ws.Use(limiter.Limit())

// 注意替换这里:
// RegisterDemoServer方法是在"api"目录下代码生成的
// 对应proto文件内自定义的service名字,请使用正确方法名替换
pb.RegisterDemoServer(ws.Server(), svc)

ws, err := ws.Start()
if err != nil {
panic(err)
}
return ws
}
```

# 扩展阅读

[warden快速开始](warden-quickstart.md) [warden基于pb生成](warden-pb.md) [warden负载均衡](warden-balancer.md) [warden服务发现](warden-resolver.md)
Expand Down

0 comments on commit 9acc8c5

Please sign in to comment.