interceptor

package
v1.16.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLogInterceptor added in v1.10.0

func AccessLogInterceptor() grpc.UnaryServerInterceptor

func StatusInterceptor

func StatusInterceptor(fn func(code codes.Code)) grpc.UnaryServerInterceptor

StatusInterceptor returns a UnaryServerInterceptor that provides a hook to access the grpc status code for each request.

Types

type ShutdownCounter

type ShutdownCounter struct {
	// contains filtered or unexported fields
}

ShutdownCounter provides an interceptor function that can be used with StatusInterceptor to shutdown the server when the criteria meet.

func NewShutdownCounter

func NewShutdownCounter(ctx context.Context, config ShutdownCounterConfig) *ShutdownCounter

NewShutdownCounter returns a ShutdownCounter.

func (*ShutdownCounter) InterceptorFn

func (c *ShutdownCounter) InterceptorFn(code codes.Code)

InterceptorFn is intended to be used to create a StatusInterceptor to monitor service status and shutdown the server when the criteria meet.

shutdownCounterConfig := interceptor.ShutdownCounterConfig{
	ReportOnly:            true,
	ConsecutiveCountLimit: 4,
	TimeRangeCountLimit:   10,
	TickerDuration:        60 * time.Second,
	ShutdownFn: func() {
		grpcServer.GracefulStop()
		if err := server.Shutdown(ctx); err != nil {
			log.Fatalf("failed to shutdown server: %v", err)
		}
	},
}
interceptors = []grpc.UnaryServerInterceptor{
	interceptor.StatusInterceptor((interceptor.NewShutdownCounter(ctx, shutdownCounterConfig)).InterceptorFn),
}

type ShutdownCounterConfig

type ShutdownCounterConfig struct {
	ReportOnly            bool
	ConsecutiveCountLimit int32
	TimeRangeCountLimit   int32
	TickerDuration        time.Duration

	// The function is provided by users to shutdown the server. This function is
	// guaranteed to run only once.
	ShutdownFn func()
}

ShutdownCounterConfig configures the behavior of ShutdownCounter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL