grpc-throttle interceptor for go-grpc-middleware. inspired by jbrandhorst
$ go get github.com/yaronsumel/grpc-throttle
Make SemaphoreMap with specific size per methods
var sMap = throttle.SemaphoreMap{
"/authpb.Auth/Method": make(throttle.Semaphore, 1),
}
Create ThrottleFunc which returns Semaphore for method.. or control it in any other way using the the context
func ThrottleFunc(ctx context.Context,fullMethod string) (throttle.Semaphore, bool) {
if s, ok := sMap[fullMethod]; ok {
return s, true
}
return nil, false
}
Use it as interceptor
server := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
// keep it last in the interceptor chain
throttle.StreamServerInterceptor(ThrottleFunc)
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
// keep it last in the interceptor chain
throttle.UnaryServerInterceptor(ThrottleFunc),
)),
)