serve_grpc

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 12 Imported by: 0

README

serve-grpc

提供gPRC服务

  • gRPC server runner
  • gPRC server interceptors
    • Recover
    • Logging
    • Authentication by JWT

      Note: 只是认证,没有鉴权。即使 JWT 合法,可能请求也没有权限操作。 鉴权逻辑与业务相关应由业务方实现。

    • Metrics
    • Rate Limit
    • Tracing
    • Validate
      • Basic 基于数据规则的校验
      • Contextual or Business 基于请求上下文业务数据要求的校验

    建议注册顺序(执行顺序):

    • Tracing: Logging 和 Metrics 要用到
    • Logging:必须在 Recover 外层,防止 panic 漏打日志
    • Metrics:必须在 Recover 外层,防止 panic 漏报数据
    • Recover:其他的不依赖 recover 了,要在这个位置兜底
    • Metadata propagation
    • JWT:Rate Limit 和 Validate 可能用到 Token 里的数据
    • 业务鉴权:必须先认证;Validate 可能用到这里产生的数据
    • Rate Limit:如果在这里拦截,后续不需要费力计算。
    • Validate

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListenFunc

type ListenFunc func() (net.Listener, error)

type Server

type Server struct {
	*grpc.Server
	// contains filtered or unexported fields
}

Server is a gRPC server runner.

func NewServer

func NewServer(lisFn ListenFunc, opts ...ServerOption) *Server

NewServer creates a gRPC server by options. Note the required listen func should not produce a TSL listener.

func (*Server) Address

func (s *Server) Address() (net.Addr, error)

func (*Server) Prepare

func (s *Server) Prepare(_ context.Context) error

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

type ServerOption

type ServerOption func(s *Server)

ServerOption is gRPC server option.

func WithLogName

func WithLogName(name string) ServerOption

WithLogName returns a ServerOption that sets the logger name.

func WithOption

func WithOption(opts ...grpc.ServerOption) ServerOption

WithOption returns a ServerOption that sets grpc.ServerOption for the server.

func WithStreamInterceptor

func WithStreamInterceptor(in ...grpc.StreamServerInterceptor) ServerOption

WithStreamInterceptor returns a ServerOption that sets the StreamServerInterceptor for the server.

func WithTLSConfig

func WithTLSConfig(c *tls.Config) ServerOption

WithTLSConfig returns a ServerOption that append grpc.Creds option with the tls.Config. If the tls.Config is nil, the returned ServerOption would do nothing.

func WithTimeout

func WithTimeout(timeout time.Duration) ServerOption

WithTimeout returns a ServerOption that config timeout.

func WithUnaryInterceptor

func WithUnaryInterceptor(in ...grpc.UnaryServerInterceptor) ServerOption

WithUnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the server.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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