https://github.com/yaronsumel/grpc-throttle
grpc-throttle interceptor for grpc server (goLang)
https://github.com/yaronsumel/grpc-throttle
golang grpc grpc-middleware
Last synced: about 2 months ago
JSON representation
grpc-throttle interceptor for grpc server (goLang)
- Host: GitHub
- URL: https://github.com/yaronsumel/grpc-throttle
- Owner: yaronsumel
- License: mit
- Created: 2017-07-17T07:18:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-17T16:23:16.000Z (about 7 years ago)
- Last Synced: 2024-11-15T08:04:45.661Z (over 1 year ago)
- Topics: golang, grpc, grpc-middleware
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 10
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# grpc-throttle
grpc-throttle interceptor for go-grpc-middleware. inspired by [jbrandhorst](https://jbrandhorst.com/post/go-semaphore/)
## Get
`$ go get github.com/yaronsumel/grpc-throttle`
## Usage
Make SemaphoreMap with specific size per methods
```go
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
```go
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
```go
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),
)),
)
```