Documentation ¶
Index ¶
- Constants
- Variables
- func AsyncResponse() bool
- func BatchRecv() bool
- func BatchSend() bool
- func BeforeRecv(h func(net.Conn) error)
- func BeforeSend(h func(net.Conn) error)
- func Handle(m string, h HandlerFunc, args ...interface{})
- func HandleConnected(onConnected func(*Client))
- func HandleDisconnected(onDisConnected func(*Client))
- func HandleMessageDropped(onOverstock func(c *Client, m *Message))
- func HandleNotFound(h HandlerFunc)
- func HandleOverstock(onOverstock func(c *Client, m *Message))
- func HandleSessionMiss(onSessionMiss func(c *Client, m *Message))
- func RecvBufferSize() int
- func SendQueueSize() int
- func SetAsyncResponse(async bool)
- func SetBatchRecv(batch bool)
- func SetBatchSend(batch bool)
- func SetBufferFactory(f func(int) []byte)
- func SetHandler(h Handler)
- func SetLogTag(tag string)
- func SetReaderWrapper(wrapper func(conn net.Conn) io.Reader)
- func SetRecvBufferSize(size int)
- func SetSendQueueSize(size int)
- func Use(h HandlerFunc)
- func UseCoder(coder MessageCoder)
- type Client
- func (c *Client) Call(method string, req interface{}, rsp interface{}, timeout time.Duration, ...) error
- func (c *Client) CallAsync(method string, req interface{}, handler HandlerFunc, timeout time.Duration, ...) error
- func (c *Client) CallWith(ctx context.Context, method string, req interface{}, rsp interface{}, ...) error
- func (c *Client) CheckState() error
- func (c *Client) Delete(key interface{})
- func (c *Client) Get(key interface{}) (interface{}, bool)
- func (c *Client) NewMessage(cmd byte, method string, v interface{}) *Message
- func (c *Client) Notify(method string, data interface{}, timeout time.Duration, args ...interface{}) error
- func (c *Client) NotifyWith(ctx context.Context, method string, data interface{}, args ...interface{}) error
- func (c *Client) PushMsg(msg *Message, timeout time.Duration) error
- func (c *Client) Restart() error
- func (c *Client) Set(key interface{}, value interface{})
- func (c *Client) Stop()
- type ClientPool
- type Context
- func (ctx *Context) Abort()
- func (ctx *Context) Bind(v interface{}) error
- func (ctx *Context) Body() []byte
- func (ctx *Context) Deadline() (deadline time.Time, ok bool)
- func (ctx *Context) Done() <-chan struct{}
- func (ctx *Context) Err() error
- func (ctx *Context) Error(v interface{}) error
- func (ctx *Context) Get(key interface{}) (interface{}, bool)
- func (ctx *Context) Next()
- func (ctx *Context) Set(key interface{}, value interface{})
- func (ctx *Context) Value(key interface{}) interface{}
- func (ctx *Context) Values() map[interface{}]interface{}
- func (ctx *Context) Write(v interface{}) error
- func (ctx *Context) WriteWithTimeout(v interface{}, timeout time.Duration) error
- type DialerFunc
- type Handler
- type HandlerFunc
- type Header
- type Message
- func (m *Message) BodyLen() int
- func (m *Message) Cmd() byte
- func (m *Message) Data() []byte
- func (m *Message) Error() error
- func (m *Message) Get(key interface{}) (interface{}, bool)
- func (m *Message) IsAsync() bool
- func (m *Message) IsError() bool
- func (m *Message) IsFlagBitSet(index int) bool
- func (m *Message) Len() int
- func (m *Message) Method() string
- func (m *Message) MethodLen() int
- func (m *Message) Seq() uint64
- func (m *Message) Set(key interface{}, value interface{})
- func (m *Message) SetAsync(isAsync bool)
- func (m *Message) SetBodyLen(l int)
- func (m *Message) SetCmd(cmd byte)
- func (m *Message) SetError(isError bool)
- func (m *Message) SetFlagBit(index int, value bool) error
- func (m *Message) SetMethodLen(l int)
- func (m *Message) SetSeq(seq uint64)
- func (m *Message) Values() map[interface{}]interface{}
- type MessageCoder
- type Server
- type WebsocketConn
Constants ¶
const ( TimeZero time.Duration = 0 TimeForever time.Duration = 1<<63 - 1 )
const ( // CmdNone is invalid CmdNone byte = 0 // CmdRequest the other side should response to a request message CmdRequest byte = 1 // CmdResponse the other side should not response to a request message CmdResponse byte = 2 // CmdNotify the other side should not response to a request message CmdNotify byte = 3 )
const ( // HeaderIndexBodyLenBegin . HeaderIndexBodyLenBegin = 0 // HeaderIndexBodyLenEnd . HeaderIndexBodyLenEnd = 4 // HeaderIndexReserved . HeaderIndexReserved = 4 // HeaderIndexCmd . HeaderIndexCmd = 5 // HeaderIndexFlag . HeaderIndexFlag = 6 // HeaderIndexMethodLen . HeaderIndexMethodLen = 7 // HeaderIndexSeqBegin . HeaderIndexSeqBegin = 8 // HeaderIndexSeqEnd . HeaderIndexSeqEnd = 16 // HeaderFlagMaskError . HeaderFlagMaskError byte = 0x01 // HeaderFlagMaskAsync . HeaderFlagMaskAsync byte = 0x02 )
const ( // HeadLen represents Message head length. HeadLen int = 16 // MaxMethodLen limits Message method length. MaxMethodLen int = 127 // MaxBodyLen limits Message body length. MaxBodyLen int = 1024*1024*64 - 16 )
Variables ¶
var ( // Represents a timeout error because of timer or context. ErrClientTimeout = errors.New("timeout") // Represents an error of 0 time parameter. ErrClientInvalidTimeoutZero = errors.New("invalid timeout, should not be 0") // Represents an error of less than 0 time parameter. ErrClientInvalidTimeoutLessThanZero = errors.New("invalid timeout, should not be < 0") // Represents an error with 0 time parameter but with non-nil callback. ErrClientInvalidTimeoutZeroWithNonNilCallback = errors.New("invalid timeout 0 with non-nil callback") // Represents an error of Client's send queue is full. ErrClientOverstock = errors.New("timeout: rpc Client's send queue is full") // Represents an error that Client is reconnecting. ErrClientReconnecting = errors.New("client reconnecting") // Represents an error that Client is stopped. ErrClientStopped = errors.New("client stopped") // Represents an error of empty dialer array. ErrClientInvalidPoolDialers = errors.New("invalid dialers: empty array") )
var ( // Invalid message CMD. ErrInvalidRspMessage = errors.New("invalid response message cmd") // Method not found. ErrMethodNotFound = errors.New("method not found") // Invlaid flag bit index. ErrInvalidFlagBitIndex = errors.New("invalid index, should be 0-7") )
var (
ErrContextResponseToNotify = errors.New("should not response to a context with notify message")
)
var (
ErrTimeout = errors.New("timeout")
)
Functions ¶
func BeforeRecv ¶
Registers default handler which will be called before Recv.
func BeforeSend ¶
Registers default handler which will be called before Send.
func Handle ¶
func Handle(m string, h HandlerFunc, args ...interface{})
Handle registers default method/router handler.
If pass a Boolean value of "true", the handler will be called asynchronously in a new goroutine, Else the handler will be called synchronously in the client's reading goroutine one by one.
func HandleConnected ¶
func HandleConnected(onConnected func(*Client))
Registers default handler which will be called when client connected.
func HandleDisconnected ¶
func HandleDisconnected(onDisConnected func(*Client))
Registers default handler which will be called when client disconnected.
func HandleMessageDropped ¶
Registers default handler which will be called when message dropped.
func HandleNotFound ¶
func HandleNotFound(h HandlerFunc)
HandleNotFound registers default "" method/router handler, It will be called when mothod/router is not found.
func HandleOverstock ¶
Registers default handler which will be called when client send queue is overstock.
func HandleSessionMiss ¶
Registers default handler which will be called when async message seq not found.
func SetBufferFactory ¶
SetBufferFactory registers default buffer maker.
func SetReaderWrapper ¶
Registers default reader wrapper for net.Conn.
func SetSendQueueSize ¶
func SetSendQueueSize(size int)
Sets default client's send queue channel capacity.
func UseCoder ¶
func UseCoder(coder MessageCoder)
UseCoder registers default message coding middleware, coder.Encode will be called before message send, coder.Decode will be called after message recv.
Types ¶
type Client ¶
type Client struct { Conn net.Conn Codec codec.Codec Handler Handler Reader io.Reader Dialer DialerFunc Head Header IsAsync bool // contains filtered or unexported fields }
Client may be used by multiple goroutines simultaneously.
func (*Client) Call ¶
func (c *Client) Call(method string, req interface{}, rsp interface{}, timeout time.Duration, args ...interface{}) error
Call makes an rpc call with a timeout.
func (*Client) CallAsync ¶
func (c *Client) CallAsync(method string, req interface{}, handler HandlerFunc, timeout time.Duration, args ...interface{}) error
CallAsync makes an asynchronous rpc call with timeout. CallAsync will not block waiting for the server's response, But the handler will be called if the response arrives before the timeout.
func (*Client) CallWith ¶
func (c *Client) CallWith(ctx context.Context, method string, req interface{}, rsp interface{}, args ...interface{}) error
CallWith uses context to make rpc call.
func (*Client) NewMessage ¶
Creates a Message by client's seq, handler and codec.
func (*Client) Notify ¶
func (c *Client) Notify(method string, data interface{}, timeout time.Duration, args ...interface{}) error
Notify makes a notify with timeout. A notify does not need a response from the server.
func (*Client) NotifyWith ¶
func (c *Client) NotifyWith(ctx context.Context, method string, data interface{}, args ...interface{}) error
NotifyWith use context to make rpc notify. A notify does not need a response from the server.
type ClientPool ¶
type ClientPool struct {
// contains filtered or unexported fields
}
func NewClientPool ¶
func NewClientPool(dialer DialerFunc, size int) (*ClientPool, error)
Creates a ClientPool.
func NewClientPoolFromDialers ¶
func NewClientPoolFromDialers(dialers []DialerFunc) (*ClientPool, error)
Creates a ClientPool by multiple dialers.
type Context ¶
func (*Context) Abort ¶
func (ctx *Context) Abort()
Stops the one-by-one-calling of middlewares and method/router handler.
func (*Context) Bind ¶
Bind parses the body data and stores the result in the value pointed to by 'v'.
func (*Context) Set ¶
func (ctx *Context) Set(key interface{}, value interface{})
Sets key-value pair.
func (*Context) Value ¶
func (ctx *Context) Value(key interface{}) interface{}
Returns the value associated with this context for key, implements stdlib's Context.
type Handler ¶
type Handler interface { Clone() Handler LogTag() string SetLogTag(tag string) // Registers handler which will be called when client connected. HandleConnected(onConnected func(*Client)) // Will be called when client is connected. OnConnected(c *Client) // Registers handler which will be called when client is disconnected. HandleDisconnected(onDisConnected func(*Client)) // Will be called when client is disconnected. OnDisconnected(c *Client) // Registers handler which will be called when client send queue is overstock. HandleOverstock(onOverstock func(c *Client, m *Message)) // Will be called when client chSend is full. OnOverstock(c *Client, m *Message) // Registers handler which will be called when message dropped. HandleMessageDropped(onOverstock func(c *Client, m *Message)) // Will be called when message is dropped. OnMessageDropped(c *Client, m *Message) // Registers handler which will be called when async message seq not found. HandleSessionMiss(onSessionMiss func(c *Client, m *Message)) // Will be called when async message seq not found. OnSessionMiss(c *Client, m *Message) // Registers handler which will be called before Recv. BeforeRecv(h func(net.Conn) error) // Registers handler which will be called before Send. BeforeSend(h func(net.Conn) error) // Returns BatchRecv flag. BatchRecv() bool // Sets BatchRecv flag. SetBatchRecv(batch bool) // Returns BatchSend flag. BatchSend() bool // Sets BatchSend flag. SetBatchSend(batch bool) // Returns AsyncResponse flag. AsyncResponse() bool // Sets AsyncResponse flag. SetAsyncResponse(async bool) // Wraps net.Conn to Read data with io.Reader. WrapReader(conn net.Conn) io.Reader // Registers reader wrapper for net.Conn. SetReaderWrapper(wrapper func(conn net.Conn) io.Reader) // Reads a message from a client. Recv(c *Client) (*Message, error) // Writes buffer data to a connection. Send(c net.Conn, buffer []byte) (int, error) // Writes multiple buffer data to a connection. SendN(conn net.Conn, buffers net.Buffers) (int, error) // Returns client's read buffer size. RecvBufferSize() int // Sets client's read buffer size. SetRecvBufferSize(size int) // Returns client's send queue channel capacity. SendQueueSize() int // Sets client's send queue channel capacity. SetSendQueueSize(size int) // Registers method/router handler middleware. Use(h HandlerFunc) // UseCoder registers message coding middleware, // coder.Encode will be called before message send, // coder.Decode will be called after message recv. UseCoder(coder MessageCoder) // Coders returns coding middlewares. Coders() []MessageCoder // Handle registers method/router handler. // // If pass a Boolean value of "true", the handler will be called asynchronously in a new goroutine, // Else the handler will be called synchronously in the client's reading goroutine one by one. Handle(m string, h HandlerFunc, args ...interface{}) // HandleNotFound registers "" method/router handler, // It will be called when mothod/router is not found. HandleNotFound(h HandlerFunc) // OnMessage finds method/router middlewares and handler, then call them one by one. OnMessage(c *Client, m *Message) // GetBuffer makes a buffer by size. GetBuffer(size int) []byte // SetBufferFactory registers buffer maker. SetBufferFactory(f func(int) []byte) }
Defines net message handler interface.
var DefaultHandler Handler = NewHandler()
type HandlerFunc ¶
type HandlerFunc func(*Context)
Defines message handler of middleware and method/router.
type Message ¶
type Message struct { Buffer []byte // contains filtered or unexported fields }
Message represents an nrpc Message.
func (*Message) IsFlagBitSet ¶
IsFlagBitSet returns flag bit value.
func (*Message) Set ¶
func (m *Message) Set(key interface{}, value interface{})
Set sets key-value pair.
func (*Message) SetFlagBit ¶
SetFlagBit sets flag bit value by index.
func (*Message) SetMethodLen ¶
SetMethodLen sets method length.
type MessageCoder ¶
type MessageCoder interface { // Encode wrap message before send to client Encode(*Client, *Message) *Message // Decode unwrap message between recv and handle Decode(*Client, *Message) *Message }
MessageCoder defines Message coding middleware interface.
type Server ¶
type Server struct { Accepted int64 CurrLoad int64 MaxLoad int64 Codec codec.Codec Handler Handler Listener net.Listener // contains filtered or unexported fields }
func (*Server) NewMessage ¶
NewMessage creates a Message.
type WebsocketConn ¶
type WebsocketConn interface {
HandleWebsocket(func())
}