Documentation ¶
Overview ¶
Package server provides a framework for managing multiple services within a single process. A server process may listen on multiple ports, providing different services on different ports.
Index ¶
- Constants
- Variables
- func RegisterStreamFilter(name string, filter StreamFilter)
- type Attachment
- type FilterFunc
- type Handler
- type Method
- type Option
- func WithAddress(s string) Option
- func WithCloseWaitTime(t time.Duration) Option
- func WithContainer(container string) Option
- func WithCurrentCompressType(t int) Option
- func WithCurrentSerializationType(t int) Option
- func WithDisableKeepAlives(disable bool) Option
- func WithDisableRequestTimeout(disable bool) Option
- func WithEnvName(envName string) Option
- func WithFilter(f filter.ServerFilter) Option
- func WithFilters(fs []filter.ServerFilter) Option
- func WithHandler(h transport.Handler) Option
- func WithIdleTimeout(t time.Duration) Option
- func WithListener(lis net.Listener) Option
- func WithMaxCloseWaitTime(t time.Duration) Option
- func WithMaxRoutines(routines int) Option
- func WithMaxWindowSize(w uint32) Option
- func WithNamedFilter(name string, f filter.ServerFilter) Option
- func WithNamespace(namespace string) Option
- func WithNetwork(s string) Option
- func WithProtocol(s string) Option
- func WithRESTOptions(opts ...restful.Option) Option
- func WithRegistry(r registry.Registry) Option
- func WithServerAsync(serverAsync bool) Option
- func WithServiceName(s string) Option
- func WithSetName(setName string) Option
- func WithStreamFilter(sf StreamFilter) Option
- func WithStreamFilters(sfs ...StreamFilter) Option
- func WithStreamTransport(st transport.ServerStreamTransport) Option
- func WithTLS(certFile, keyFile, caFile string) Option
- func WithTimeout(t time.Duration) Option
- func WithTransport(t transport.ServerTransport) Option
- func WithWritev(writev bool) Option
- type Options
- type Server
- func (s *Server) AddService(serviceName string, service Service)
- func (s *Server) Close(ch chan struct{}) error
- func (s *Server) Register(serviceDesc interface{}, serviceImpl interface{}) error
- func (s *Server) RegisterOnShutdown(fn func())
- func (s *Server) Serve() error
- func (s *Server) Service(serviceName string) Service
- func (s *Server) StartNewProcess(args ...string) (uintptr, error)
- type Service
- type ServiceDesc
- type Stream
- type StreamDesc
- type StreamFilter
- type StreamFilterChain
- type StreamHandle
- type StreamHandler
- type StreamHandlerWapper
- type StreamServerInfo
Constants ¶
const MaxCloseWaitTime = 10 * time.Second
MaxCloseWaitTime is the max waiting time for closing services.
Variables ¶
var DefaultServerCloseSIG = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV}
DefaultServerCloseSIG are signals that trigger server shutdown.
var DefaultServerGracefulSIG = syscall.SIGUSR2
DefaultServerGracefulSIG is signal that triggers server graceful restart.
var New = func(opts ...Option) Service { o := defaultOptions() s := &service{ opts: o, handlers: make(map[string]Handler), streamHandlers: make(map[string]StreamHandler), streamInfo: make(map[string]*StreamServerInfo), } for _, o := range opts { o(s.opts) } o.Transport = attemptSwitchingTransport(o) if !s.opts.handlerSet { s.opts.ServeOptions = append(s.opts.ServeOptions, transport.WithHandler(s)) } s.ctx, s.cancel = context.WithCancel(context.Background()) return s }
New creates a service. It will use transport.DefaultServerTransport unless Option WithTransport() is called to replace its transport.ServerTransport plugin.
Functions ¶
func RegisterStreamFilter ¶
func RegisterStreamFilter(name string, filter StreamFilter)
RegisterStreamFilter registers server stream filter with name.
Types ¶
type Attachment ¶
type Attachment struct {
// contains filtered or unexported fields
}
Attachment stores the attachment of tRPC requests/responses.
func GetAttachment ¶
func GetAttachment(msg codec.Msg) *Attachment
GetAttachment returns Attachment from msg. If there is no Attachment in the msg, an empty attachment bound to the msg will be returned.
func (*Attachment) Request ¶
func (a *Attachment) Request() io.Reader
Request returns Request Attachment.
func (*Attachment) SetResponse ¶
func (a *Attachment) SetResponse(attachment io.Reader)
SetResponse sets Response attachment.
type FilterFunc ¶
type FilterFunc func(reqBody interface{}) (filter.ServerChain, error)
FilterFunc reads reqBody, parses it, and returns a filter.Chain for server stub.
type Handler ¶
type Handler func(ctx context.Context, f FilterFunc) (rspBody interface{}, err error)
Handler is the default handler.
type Method ¶
type Method struct { Name string Func func(svr interface{}, ctx context.Context, f FilterFunc) (rspBody interface{}, err error) Bindings []*restful.Binding }
Method provides the information of an RPC Method.
type Option ¶
type Option func(*Options)
Option sets server options.
func WithAddress ¶
WithAddress returns an Option that sets address (ip:port or :port).
func WithCloseWaitTime ¶
WithCloseWaitTime returns an Option that sets min waiting time when close service. It's used for service's graceful restart. Default: 0ms, max: 10s.
func WithContainer ¶
WithContainer returns an Option that sets container name.
func WithCurrentCompressType ¶
WithCurrentCompressType returns an Option that sets current compress type.
func WithCurrentSerializationType ¶
WithCurrentSerializationType returns an Option that sets current serialization type. It's often used for transparent proxy without serialization. If current serialization type is not set, serialization type will be determined by serialization field of request protocol.
func WithDisableKeepAlives ¶
WithDisableKeepAlives returns an Option that disables keep-alives.
func WithDisableRequestTimeout ¶
WithDisableRequestTimeout returns an Option that disables timeout for handling requests.
func WithEnvName ¶
WithEnvName returns an Option that sets environment name.
func WithFilter ¶
func WithFilter(f filter.ServerFilter) Option
WithFilter returns an Option that adds a filter.Filter (pre or post).
func WithFilters ¶
func WithFilters(fs []filter.ServerFilter) Option
WithFilters returns an Option that adds a filter chain.
func WithHandler ¶
WithHandler returns an Option that sets transport.Handler (service itself by default).
func WithIdleTimeout ¶
WithIdleTimeout returns an Option that sets idle connection timeout. Notice: it doesn't work for server streaming.
func WithListener ¶
WithListener returns an Option that sets net.Listener for accept, read/write op customization.
func WithMaxCloseWaitTime ¶
WithMaxCloseWaitTime returns an Option that sets max waiting time when close service. It's used for wait requests finish. Default: 0ms.
func WithMaxRoutines ¶
WithMaxRoutines returns an Option that sets max number of goroutines. It only works for server async mode. MaxRoutines should be set to twice as expected number of routines (can be calculated by expected QPS), and larger than MAXPROCS. If MaxRoutines is not set or set to 0, it will be set to (1<<31 - 1). Requests exceeding MaxRoutines will be queued. Prolonged overages may lead to OOM! MaxRoutines is not the solution to alleviate server overloading.
func WithMaxWindowSize ¶
WithMaxWindowSize returns an Option that sets max window size for server stream.
func WithNamedFilter ¶
func WithNamedFilter(name string, f filter.ServerFilter) Option
WithNamedFilter returns an Option that adds named filter
func WithNamespace ¶
WithNamespace returns an Option that sets namespace for server.
func WithNetwork ¶
WithNetwork returns an Option that sets network, tcp by default.
func WithProtocol ¶
WithProtocol returns an Option that sets protocol of service. This Option also sets framerbuilder and codec plugin.
func WithRESTOptions ¶
WithRESTOptions returns an Option that sets RESTful router options.
func WithRegistry ¶
WithRegistry returns an Option that sets registry.Registry. One service, one registry.Registry.
func WithServerAsync ¶
WithServerAsync returns an Option that sets whether to enable server asynchronous or not. When enable it, the server can cyclically receive packets and process request and response packets concurrently for the same connection.
func WithServiceName ¶
WithServiceName returns an Option that sets service name.
func WithSetName ¶
WithSetName returns an Option that sets "Set" name.
func WithStreamFilter ¶
func WithStreamFilter(sf StreamFilter) Option
WithStreamFilter returns an Option that adds a stream filter (pre or post).
func WithStreamFilters ¶
func WithStreamFilters(sfs ...StreamFilter) Option
WithStreamFilters returns an Option that adds a stream filter chain.
func WithStreamTransport ¶
func WithStreamTransport(st transport.ServerStreamTransport) Option
WithStreamTransport returns an Option that sets transport.ServerStreamTransport for server.
func WithTLS ¶
WithTLS returns an Option that sets TLS certificate files' path. The input param certFile represents server certificate. The input param keyFile represents server private key. The input param caFile represents CA certificate, which is used for client-to-server authentication(mTLS). If cafile is empty, no client validation. Also, caFile="root" means local ca file would be used to validate client. All certificates must be X.509 certificates.
func WithTimeout ¶
WithTimeout returns an Option that sets timeout for handling a request.
func WithTransport ¶
func WithTransport(t transport.ServerTransport) Option
WithTransport returns an Option that sets transport.ServerTransport.
func WithWritev ¶
WithWritev returns an Option that sets whether to enable writev or not.
type Options ¶
type Options struct { Namespace string // namespace like "Production", "Development" etc. EnvName string // environment name SetName string // "Set" name ServiceName string // service name Address string // listen address, ip:port Timeout time.Duration // timeout for handling a request DisableRequestTimeout bool // whether to disable request timeout that inherits from upstream DisableKeepAlives bool // disables keep-alives CurrentSerializationType int CurrentCompressType int ServeOptions []transport.ListenServeOption Transport transport.ServerTransport Registry registry.Registry Codec codec.Codec Filters filter.ServerChain // filter chain FilterNames []string // the name of filters StreamHandle StreamHandle // server stream processing StreamTransport transport.ServerStreamTransport // server stream transport plugin MaxWindowSize uint32 // max window size for server stream CloseWaitTime time.Duration // min waiting time when closing server for wait deregister finish MaxCloseWaitTime time.Duration // max waiting time when closing server for wait requests finish RESTOptions []restful.Option // RESTful router options StreamFilters StreamFilterChain // contains filtered or unexported fields }
Options are server side options.
type Server ¶
type Server struct { MaxCloseWaitTime time.Duration // max waiting time when closing server // contains filtered or unexported fields }
Server is a tRPC server. One process, one server. A server may offer one or more services.
func (*Server) AddService ¶
AddService adds a service for the server. The param serviceName refers to the name used for Naming Services and configured by config file (typically trpc_go.yaml). When trpc.NewServer() is called, it will traverse service configuration from config file, and call AddService to add a service implementation to the server's map[string]Service (serviceName as key).
func (*Server) Close ¶
Close implements Service interface, notifying all services of server shutdown. Would wait no more than 10s.
func (*Server) Register ¶
Register implements Service interface, registering a proto service. Normally, a server contains only one service, so the registration is straightforward. When it comes to server with multiple services, remember to use Service("servicename") to specify which service this proto service is registered for. Otherwise, this proto service will be registered for all services of the server.
func (*Server) RegisterOnShutdown ¶
func (s *Server) RegisterOnShutdown(fn func())
RegisterOnShutdown registers a hook function that would be executed when server is shutting down.
type Service ¶
type Service interface { // Register registers a proto service. Register(serviceDesc interface{}, serviceImpl interface{}) error // Serve starts serving. Serve() error // Close stops serving. Close(chan struct{}) error }
Service is the interface that provides services.
type ServiceDesc ¶
type ServiceDesc struct { ServiceName string HandlerType interface{} Methods []Method Streams []StreamDesc StreamHandle StreamHandle }
ServiceDesc describes a proto service.
type Stream ¶
type Stream interface { // Context is context of server stream. Context() context.Context // SendMsg sends streaming data. SendMsg(m interface{}) error // RecvMsg receives streaming data. RecvMsg(m interface{}) error }
Stream is the interface that defines server stream api.
type StreamDesc ¶
type StreamDesc struct { // StreamName is the name of stream. StreamName string // Handler is a stream handler. Handler StreamHandlerWapper // ServerStreams indicates whether it's server streaming. ServerStreams bool // ClientStreams indicates whether it's client streaming. ClientStreams bool }
StreamDesc describes a server stream.
type StreamFilter ¶
type StreamFilter func(ss Stream, info *StreamServerInfo, handler StreamHandler) error
StreamFilter is server stream filter.
func GetStreamFilter ¶
func GetStreamFilter(name string) StreamFilter
GetStreamFilter gets server stream filter by name.
type StreamFilterChain ¶
type StreamFilterChain []StreamFilter
StreamFilterChain server stream filters chain.
func (StreamFilterChain) Filter ¶
func (c StreamFilterChain) Filter(ss Stream, info *StreamServerInfo, handler StreamHandler) error
Filter implements StreamFilter for multi stream filters.
type StreamHandle ¶
type StreamHandle interface { // StreamHandleFunc does server stream processing. StreamHandleFunc(ctx context.Context, sh StreamHandler, si *StreamServerInfo, req []byte) ([]byte, error) // Init does the initialization, mainly passing and saving Options. Init(opts *Options) error }
StreamHandle is the interface that defines server stream processing.
type StreamHandler ¶
StreamHandler is server stream handler.
type StreamHandlerWapper ¶
StreamHandlerWapper is server stream handler wrapper. The input param srv should be an implementation of server stream proto service. The input param stream is used by srv.
type StreamServerInfo ¶
type StreamServerInfo struct { // FullMethod is the full RPC method string, i.e., /package.service/method. FullMethod string // IsClientStream indicates whether the RPC is a client streaming RPC. IsClientStream bool // IsServerStream indicates whether the RPC is a server streaming RPC. IsServerStream bool }
StreamServerInfo is stream information on server side.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package mockserver is a generated GoMock package.
|
Package mockserver is a generated GoMock package. |