forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
30 lines (26 loc) · 853 Bytes
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package ratelimit_test
import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/ratelimit"
"google.golang.org/grpc"
)
// alwaysPassLimiter is an example limiter which implements Limiter interface.
// It does not limit any request because Limit function always returns false.
type alwaysPassLimiter struct{}
func (*alwaysPassLimiter) Limit() bool {
return false
}
// Simple example of server initialization code.
func Example() {
// Create unary/stream rateLimiters, based on token bucket here.
// You can implement your own ratelimiter for the interface.
limiter := &alwaysPassLimiter{}
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
ratelimit.UnaryServerInterceptor(limiter),
),
grpc_middleware.WithStreamServerChain(
ratelimit.StreamServerInterceptor(limiter),
),
)
}