Documentation ¶
Overview ¶
Package callmeback is a generic server-side "come again in ..." middleware for gRPC.
In the case where a gRPC stream would be nice to provide but impossible to deploy (see 1) this interceptor enables a pool-based unary call replacement for push-based streams.
It adds a trailer duration value indicating to the client the time it is safe to pause for before calling again.
Example ¶
Simple example of server initialization code.
package main import ( "context" "time" "github.com/fenollp/grpc-callmeback-interceptor/go-callmeback" "github.com/grpc-ecosystem/go-grpc-middleware" "google.golang.org/grpc" ) type everySecond struct{} func (*everySecond) PleaseComeAgain(ctx context.Context, c callmeback.Context) (time.Duration, error) { return time.Second, nil } // Simple example of server initialization code. func main() { // Create unary/stream rateLimiters, based on token bucket here. cb := &everySecond{} _ = grpc.NewServer( grpc_middleware.WithUnaryServerChain( callmeback.UnaryServerInterceptor(cb), ), ) }
Output:
Index ¶
Examples ¶
Constants ¶
View Source
const ErrBadTrailer = errStr("bad callmeback trailer")
ErrBadTrailer is a constant error
Variables ¶
View Source
var Trailer = "x-pleasecomeagain"
Trailer names the trailer value passed to the client
Functions ¶
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(callmebacker CallMeBacker) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a new unary server interceptor that instructs the client to call again.
Types ¶
type CallMeBacker ¶
type CallMeBacker interface { // If PleaseComeAgain returns a non-nil error, the request will have been processed for nothing. // If it returns a positive duration, a trailer is added to the response. PleaseComeAgain(ctx context.Context, c Context) (time.Duration, error) }
CallMeBacker defines the interface to instruct client when to call back.
Click to show internal directories.
Click to hide internal directories.