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 represents zero time. TimeZero time.Duration = 0 // TimeForever represents forever time. 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 ( // ErrClientTimeout represents a timeout error because of timer or context. ErrClientTimeout = errors.New("timeout") // ErrClientInvalidTimeoutZero represents an error of 0 time parameter. ErrClientInvalidTimeoutZero = errors.New("invalid timeout, should not be 0") // ErrClientInvalidTimeoutLessThanZero represents an error of less than 0 time parameter. ErrClientInvalidTimeoutLessThanZero = errors.New("invalid timeout, should not be < 0") // ErrClientInvalidTimeoutZeroWithNonNilCallback represents an error with 0 time parameter but with non-nil callback. ErrClientInvalidTimeoutZeroWithNonNilCallback = errors.New("invalid timeout 0 with non-nil callback") // ErrClientOverstock represents an error of Client's send queue is full. ErrClientOverstock = errors.New("timeout: rpc Client's send queue is full") // ErrClientReconnecting represents an error that Client is reconnecting. ErrClientReconnecting = errors.New("client reconnecting") // ErrClientStopped represents an error that Client is stopped. ErrClientStopped = errors.New("client stopped") // ErrClientInvalidPoolDialers represents an error of empty dialer array. ErrClientInvalidPoolDialers = errors.New("invalid dialers: empty array") )
client error
var ( // ErrInvalidRspMessage represents an error of invalid message CMD. ErrInvalidRspMessage = errors.New("invalid response message cmd") // ErrMethodNotFound represents an error of method not found. ErrMethodNotFound = errors.New("method not found") // ErrInvalidFlagBitIndex represents an error of invlaid flag bit index. ErrInvalidFlagBitIndex = errors.New("invalid index, should be 0-7") )
message error
var ( // ErrContextResponseToNotify represents an error that response to a notify message. ErrContextResponseToNotify = errors.New("should not response to a context with notify message") )
context error
var ( // ErrTimeout represents an error of timeout. ErrTimeout = errors.New("timeout") )
general errors
Functions ¶
func BeforeRecv ¶
BeforeRecv registers default handler which will be called before Recv.
func BeforeSend ¶
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))
HandleConnected registers default handler which will be called when client connected.
func HandleDisconnected ¶
func HandleDisconnected(onDisConnected func(*Client))
HandleDisconnected registers default handler which will be called when client disconnected.
func HandleMessageDropped ¶
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 ¶
HandleOverstock registers default handler which will be called when client send queue is overstock.
func HandleSessionMiss ¶
HandleSessionMiss registers default handler which will be called when async message seq not found.
func RecvBufferSize ¶
func RecvBufferSize() int
RecvBufferSize returns default client's read buffer size.
func SendQueueSize ¶
func SendQueueSize() int
SendQueueSize returns default client's send queue channel capacity.
func SetAsyncResponse ¶
func SetAsyncResponse(async bool)
SetAsyncResponse sets default AsyncResponse flag.
func SetBufferFactory ¶
SetBufferFactory registers default buffer maker.
func SetReaderWrapper ¶
SetReaderWrapper registers default reader wrapper for net.Conn.
func SetRecvBufferSize ¶
func SetRecvBufferSize(size int)
SetRecvBufferSize sets default client's read buffer size.
func SetSendQueueSize ¶
func SetSendQueueSize(size int)
SetSendQueueSize 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 // contains filtered or unexported fields }
Client represents an arpc Client. There may be multiple outstanding Calls or Notifys associated with a single Client, and a 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. Call will block waiting for the server's response until 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. CallWith blocks to wait for a response from the server until it times out.
func (*Client) NewMessage ¶
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
}
ClientPool represents an arpc Client Pool.
func NewClientPool ¶
func NewClientPool(dialer DialerFunc, size int) (*ClientPool, error)
NewClientPool creates a ClientPool.
func NewClientPoolFromDialers ¶
func NewClientPoolFromDialers(dialers []DialerFunc) (*ClientPool, error)
NewClientPoolFromDialers creates a ClientPool by multiple dialers.
func (*ClientPool) Get ¶
func (pool *ClientPool) Get(index int) *Client
Get returns a Client by index.
func (*ClientPool) Next ¶
func (pool *ClientPool) Next() *Client
Next returns a Client by round robin.
type Context ¶
Context represents an arpc Call's context.
func (*Context) Abort ¶ added in v1.1.0
func (ctx *Context) Abort()
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) Next ¶
func (ctx *Context) Next()
Next calls next middleware or method/router handler.
func (*Context) Set ¶
func (ctx *Context) Set(key interface{}, value interface{})
Set sets key-value pair.
func (*Context) Value ¶ added in v1.1.0
func (ctx *Context) Value(key interface{}) interface{}
Value returns the value associated with this context for key, implements stdlib's Context.
func (*Context) Values ¶
func (ctx *Context) Values() map[interface{}]interface{}
Values returns values.
type DialerFunc ¶
DialerFunc defines the dialer used by arpc Client to connect to the server.
type Handler ¶
type Handler interface { // Clone returns a copy of Handler. Clone() Handler // LogTag returns log tag value. LogTag() string // SetLogTag sets log tag. SetLogTag(tag string) // HandleConnected registers handler which will be called when client connected. HandleConnected(onConnected func(*Client)) // OnConnected will be called when client is connected. OnConnected(c *Client) // HandleDisconnected registers handler which will be called when client is disconnected. HandleDisconnected(onDisConnected func(*Client)) // OnDisconnected will be called when client is disconnected. OnDisconnected(c *Client) // HandleOverstock registers handler which will be called when client send queue is overstock. HandleOverstock(onOverstock func(c *Client, m *Message)) // OnOverstock will be called when client chSend is full. OnOverstock(c *Client, m *Message) // HandleMessageDropped registers handler which will be called when message dropped. HandleMessageDropped(onOverstock func(c *Client, m *Message)) // OnOverstock will be called when message is dropped. OnMessageDropped(c *Client, m *Message) // HandleSessionMiss registers handler which will be called when async message seq not found. HandleSessionMiss(onSessionMiss func(c *Client, m *Message)) // OnSessionMiss will be called when async message seq not found. OnSessionMiss(c *Client, m *Message) // BeforeRecv registers handler which will be called before Recv. BeforeRecv(h func(net.Conn) error) // BeforeSend registers handler which will be called before Send. BeforeSend(h func(net.Conn) error) // BatchRecv returns BatchRecv flag. BatchRecv() bool // SetBatchRecv sets BatchRecv flag. SetBatchRecv(batch bool) // BatchSend returns BatchSend flag. BatchSend() bool // SetBatchSend sets BatchSend flag. SetBatchSend(batch bool) // AsyncResponse returns AsyncResponse flag. AsyncResponse() bool // SetAsyncResponse sets AsyncResponse flag. SetAsyncResponse(async bool) // WrapReader wraps net.Conn to Read data with io.Reader. WrapReader(conn net.Conn) io.Reader // SetReaderWrapper registers reader wrapper for net.Conn. SetReaderWrapper(wrapper func(conn net.Conn) io.Reader) // Recv reads a message from a client. Recv(c *Client) (*Message, error) // Send writes buffer data to a connection. Send(c net.Conn, buffer []byte) (int, error) // SendN writes multiple buffer data to a connection. SendN(conn net.Conn, buffers net.Buffers) (int, error) // RecvBufferSize returns client's read buffer size. RecvBufferSize() int // SetRecvBufferSize sets client's read buffer size. SetRecvBufferSize(size int) // SendQueueSize returns client's send queue channel capacity. SendQueueSize() int // SetSendQueueSize sets client's send queue channel capacity. SetSendQueueSize(size int) // Use 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) }
Handler defines net message handler interface.
var DefaultHandler Handler = NewHandler()
DefaultHandler is the default Handler used by arpc
type HandlerFunc ¶
type HandlerFunc func(*Context)
HandlerFunc defines message handler of arpc middleware and method/router.
type Message ¶
type Message struct { Buffer []byte // contains filtered or unexported fields }
Message represents an arpc 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 }
Server represents an arpc Server.
func (*Server) NewMessage ¶
NewMessage creates a Message.
type WebsocketConn ¶
type WebsocketConn interface {
HandleWebsocket(func())
}
WebsocketConn defines websocket-conn interface.