...

Source file src/github.com/grpc-ecosystem/go-grpc-middleware/ratelimit/examples_test.go

Documentation: github.com/grpc-ecosystem/go-grpc-middleware/ratelimit

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

View as plain text