Skip to content

yaronsumel/grpc-throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

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),
    )),
)

About

grpc-throttle interceptor for grpc server (goLang)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages