server

package
v0.0.0-...-564fdec Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2019 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	XVersion           = "X-RPCX-Version"
	XMessageType       = "X-RPCX-MesssageType"
	XHeartbeat         = "X-RPCX-Heartbeat"
	XOneway            = "X-RPCX-Oneway"
	XMessageStatusType = "X-RPCX-MessageStatusType"
	XSerializeType     = "X-RPCX-SerializeType"
	XMessageID         = "X-RPCX-MessageID"
	XServicePath       = "X-RPCX-ServicePath"
	XServiceMethod     = "X-RPCX-ServiceMethod"
	XMeta              = "X-RPCX-Meta"
	XErrorMessage      = "X-RPCX-ErrorMessage"
)
View Source
const (
	// ReaderBuffsize is used for bufio reader.
	// connection读取buffer的大小 1KB
	ReaderBuffsize = 1024
	// WriterBuffsize is used for bufio writer.
	// connection写入buffer的大小 1KB
	WriterBuffsize = 1024
)

Variables

View Source
var (
	// RemoteConnContextKey is a context key. It can be used in
	// services with context.WithValue to access the connection arrived on.
	// The associated value will be of type net.Conn.
	RemoteConnContextKey = &contextKey{"remote-conn"} // 存放net.Conn
	// StartRequestContextKey records the start time
	StartRequestContextKey = &contextKey{"start-parse-request"} // 解析请求开始时间
	// StartSendRequestContextKey records the start time
	StartSendRequestContextKey = &contextKey{"start-send-request"} // 发送请求开始时间
)
View Source
var ErrServerClosed = errors.New("http: Server closed")

ErrServerClosed is returned by the Server's Serve, ListenAndServe after a call to Shutdown or Close.

View Source
var UsePool bool

Functions

func HTTPRequest2RpcxRequest

func HTTPRequest2RpcxRequest(r *http.Request) (*protocol.Message, error)

HTTPRequest2RpcxRequest converts a http request to a rpcx request. http请求转换为rpcx请求

func RegisterMakeListener

func RegisterMakeListener(network string, ml MakeListener)

RegisterMakeListener registers a MakeListener for network. 可以自定义新的网络协议及其关联的listen: 在自定义的listen中执行RegisterMakeListener操作 即可完成对应的listener注册

Types

type MakeListener

type MakeListener func(s *Server, address string) (ln net.Listener, err error)

MakeListener defines a listener generater. 定义不同网络类型的listen

type OptionFn

type OptionFn func(*Server)

OptionFn configures options of server. 增加server一些额外的特性,目前仅提供了三个option: WithTLSConfig/WithReadTimeout/WithWriteTimeout

func WithReadTimeout

func WithReadTimeout(readTimeout time.Duration) OptionFn

设定读操作有效期 WithReadTimeout sets readTimeout.

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) OptionFn

tls配置

WithTLSConfig sets tls.Config.

func WithWriteTimeout

func WithWriteTimeout(writeTimeout time.Duration) OptionFn

设定写操作有效期 WithWriteTimeout sets writeTimeout.

type Plugin

type Plugin interface {
}

Plugin is the server plugin interface.

type PluginContainer

type PluginContainer interface {
	Add(plugin Plugin)
	Remove(plugin Plugin)
	All() []Plugin

	DoRegister(name string, rcvr interface{}, metadata string) error
	DoRegisterFunction(name string, fn interface{}, metadata string) error
	DoUnregister(name string) error

	DoPostConnAccept(net.Conn) (net.Conn, bool)
	DoPostConnClose(net.Conn) bool

	DoPreReadRequest(ctx context.Context) error
	DoPostReadRequest(ctx context.Context, r *protocol.Message, e error) error

	DoPreWriteResponse(context.Context, *protocol.Message, *protocol.Message) error
	DoPostWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error

	DoPreWriteRequest(ctx context.Context) error
	DoPostWriteRequest(ctx context.Context, r *protocol.Message, e error) error
}

PluginContainer represents a plugin container that defines all methods to manage plugins. And it also defines all extension points.

定义所有的管理plugin的方法 常用于server更具体的对象起作用:Service、Connection、Request、Response 而Option是用来对server起作用的

type PostConnAcceptPlugin

type PostConnAcceptPlugin interface {
	HandleConnAccept(net.Conn) (net.Conn, bool)
}

PostConnAcceptPlugin represents connection accept plugin. if returns false, it means subsequent IPostConnAcceptPlugins should not contiune to handle this conn and this conn has been closed.

type PostConnClosePlugin

type PostConnClosePlugin interface {
	HandleConnClose(net.Conn) bool
}

PostConnClosePlugin represents client connection close plugin.

type PostReadRequestPlugin

type PostReadRequestPlugin interface {
	PostReadRequest(ctx context.Context, r *protocol.Message, e error) error
}

PostReadRequestPlugin represents .

type PostWriteRequestPlugin

type PostWriteRequestPlugin interface {
	PostWriteRequest(ctx context.Context, r *protocol.Message, e error) error
}

PostWriteRequestPlugin represents .

type PostWriteResponsePlugin

type PostWriteResponsePlugin interface {
	PostWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
}

PostWriteResponsePlugin represents .

type PreReadRequestPlugin

type PreReadRequestPlugin interface {
	PreReadRequest(ctx context.Context) error
}

PreReadRequestPlugin represents .

type PreWriteRequestPlugin

type PreWriteRequestPlugin interface {
	PreWriteRequest(ctx context.Context) error
}

PreWriteRequestPlugin represents .

type PreWriteResponsePlugin

type PreWriteResponsePlugin interface {
	PreWriteResponse(context.Context, *protocol.Message, *protocol.Message) error
}

PreWriteResponsePlugin represents .

type RegisterFunctionPlugin

type RegisterFunctionPlugin interface {
	RegisterFunction(name string, fn interface{}, metadata string) error
}

RegisterFunctionPlugin is .

type RegisterPlugin

type RegisterPlugin interface {
	Register(name string, rcvr interface{}, metadata string) error
	Unregister(name string) error
}

RegisterPlugin is .

type Reset

type Reset interface {
	Reset()
}

Reset defines Reset method for pooled object.

type Server

type Server struct {
	Plugins PluginContainer // 增加一些Server的特性

	// AuthFunc can be used to auth.
	AuthFunc func(ctx context.Context, req *protocol.Message, token string) error // 认证
	// contains filtered or unexported fields
}

Server is rpcx server that use TCP or UDP. rpc的server端 支持TCP、UDP

func NewServer

func NewServer(options ...OptionFn) *Server

新建Server 目前支持OptionFn: WithTLSConfig()、WithReadeTimeout()、WithWriteTimeout() NewServer returns a server.

func (*Server) ActiveClientConn

func (s *Server) ActiveClientConn() []net.Conn

当前server 活跃的connection的记录

func (*Server) Address

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

Server地址: ip:port 默认情况Server完全启动之后方可获取该值

Address returns listened address.

func (*Server) Close

func (s *Server) Close() error

Close immediately closes all active net.Listeners. 立即关闭所有的active状态的net.Listener

func (*Server) Register

func (s *Server) Register(rcvr interface{}, metadata string) error

Register publishes in the server the set of methods of the receiver value that satisfy the following conditions:

  • exported method of exported type
  • three arguments, the first is of context.Context, both of exported type for three arguments
  • the third argument is a pointer
  • one return value, of type error

It returns an error if the receiver is not an exported type or has no suitable methods. It also logs the error. The client accesses each method using a string of the form "Type.Method", where Type is the receiver's concrete type. server端提供的service需要一定的条件方能对外发布 - 所属的类型属于可导出其对应的method也是可导出的, - 对应的method包括三个输入参数: 1、context.Context 2、三个参数也是可导出类型 3、第3个参数是一个pointer类型,仅有一个返回值(类型error) 注意:

1、对应的方法的receiver不属于可导出的 或 没有对应的方法 都会返回一个error ,并记录操作
2、client访问每个method都采用类似"Type.Method"格式的字符串
其中"Type.Method"中的Type是一个具体的receiver类型

Register方法完成两个操作:1、先在本地map 记录新增的service信息

func (*Server) RegisterFunction

func (s *Server) RegisterFunction(servicePath string, fn interface{}, metadata string) error

RegisterFunction publishes a function that satisfy the following conditions:

  • three arguments, the first is of context.Context, both of exported type for three arguments
  • the third argument is a pointer
  • one return value, of type error

The client accesses function using a string of the form "servicePath.Method".

用于完成function的注册 而非method,不过需要遵守如下的要求 - 包括三个输入参数:第一个参数必须是context.Context,参数都是可导出类型 - 第三个参数是一个pointer指针 - 返回值包括一个error类型 通过使用"servicePath.function"来访问function

func (*Server) RegisterFunctionName

func (s *Server) RegisterFunctionName(servicePath string, name string, fn interface{}, metadata string) error

RegisterFunctionName is like RegisterFunction but uses the provided name for the function instead of the function's concrete type.

和RegisterFuntion类似 只不过增加了可指定function的name

func (*Server) RegisterName

func (s *Server) RegisterName(name string, rcvr interface{}, metadata string) error

RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type. 类似Register不过可以指定类型名称 而不是用receiver的类型来填充

func (*Server) RegisterOnShutdown

func (s *Server) RegisterOnShutdown(f func(s *Server))

RegisterOnShutdown registers a function to call on Shutdown. This can be used to gracefully shutdown connections.

常用于优雅的关闭connection

func (*Server) SendMessage

func (s *Server) SendMessage(conn net.Conn, servicePath, serviceMethod string, metadata map[string]string, data []byte) error

给指定的client发送Message,能够在context.Context获取client的信息:conn 用于完成server-client之间的双向通信 SendMessage中的servicePath、serviceMe、metadata可以设置为零值

SendMessage a request to the specified client. The client is designated by the conn. conn can be gotten from context in services:

ctx.Value(RemoteConnContextKey)

servicePath, serviceMethod, metadata can be set to zero values.

func (*Server) Serve

func (s *Server) Serve(network, address string) (err error)

Serve starts and listens RPC requests. It is blocked until receiving connectings from clients.

启动server并监听client的请求 该操作属于阻塞的 直到接收到client的连接connection

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

用于完成劫持http连接时的 处理rpc request 当前Server本身就是一个handler ServeHTTP implements an http.Handler that answers RPC requests.

func (*Server) Shutdown

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

Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing the listener, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the Server's underlying Listener.

Shutdown通过优雅的方式关闭connection而不是直接中断connection 执行Shutdown首先关闭对应的listener接着再关闭所有idle状态下的connections 最后等待所有active的connection状态变为idle,再将其执行shutdown,由于提供了context,一旦对应的context设定有效期,若是有效期在shutdown前完成,Shutdown则返回一个context的error 否则就返回一个关闭Server底层listener的error

func (*Server) UnregisterAll

func (s *Server) UnregisterAll() error

UnregisterAll unregisters all services. You can call this method when you want to shutdown/upgrade this node.

取消注册的services 当前进行shutdown或升级节点 可以通过调用该方法

Jump to

Keyboard shortcuts

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