项目作者: yaronsumel

项目描述 :
grpc-throttle interceptor for grpc server (goLang)
高级语言: Go
项目地址: git://github.com/yaronsumel/grpc-throttle.git
创建时间: 2017-07-17T07:18:56Z
项目社区:https://github.com/yaronsumel/grpc-throttle

开源协议:MIT License

下载


grpc-throttle

grpc-throttle interceptor for go-grpc-middleware. inspired by jbrandhorst

Get

$ go get github.com/yaronsumel/grpc-throttle

Usage

Make SemaphoreMap with specific size per methods

  1. var sMap = throttle.SemaphoreMap{
  2. "/authpb.Auth/Method": make(throttle.Semaphore, 1),
  3. }

Create ThrottleFunc which returns Semaphore for method.. or control it in any other way using the the context

  1. func ThrottleFunc(ctx context.Context,fullMethod string) (throttle.Semaphore, bool) {
  2. if s, ok := sMap[fullMethod]; ok {
  3. return s, true
  4. }
  5. return nil, false
  6. }

Use it as interceptor

  1. server := grpc.NewServer(
  2. grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
  3. // keep it last in the interceptor chain
  4. throttle.StreamServerInterceptor(ThrottleFunc)
  5. )),
  6. grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
  7. // keep it last in the interceptor chain
  8. throttle.UnaryServerInterceptor(ThrottleFunc),
  9. )),
  10. )