GRPC supports interceptor features, enhancing the flexibility and extensibility of GRPC.

Interceptor Usage

Server

Use grpcx.Server.ChainUnary to add extra server interceptors:

  1. c := grpcx.Server.NewConfig()
  2. c.Options = append(c.Options, []grpc.ServerOption{
  3. grpcx.Server.ChainUnary(
  4. grpcx.Server.UnaryValidate,
  5. )}...,
  6. )
  7. s := grpcx.Server.New(c)
  8. user.Register(s)
  9. s.Run()

Client

Use grpcx.Client.ChainUnary to add extra server interceptors:

  1. conn = grpcx.Client.MustNewGrpcClientConn("demo", grpcx.Client.ChainUnary(
  2. grpcx.Client.UnaryTracing,
  3. ))

Interceptor List

The grpcx component of the framework offers a series of commonly used interceptors, some built-in and others optional.

InterceptorUsage PartBuilt-inDescription
UnaryErrorClientYesSupports the framework’s error handling component.
UnaryTracingClientYesSupports link tracing.
StreamTracingClientYesSupports link tracing (long connection).
UnaryErrorServerYesSupports the framework’s error handling component.
UnaryRecoverServerYesSupports automatic capture of server panic without crashing.
UnaryAllowNilResServerYesSupports returning nil Res objects.
UnaryValidateServerNoSupports the framework’s automatic error validation, based on struct tags. Needs to be manually introduced.
UnaryTracingServerYesSupports link tracing.
StreamTracingServerYesSupports link tracing (long connection).