Documentation ¶
Index ¶
Constants ¶
const ( // DefaultMaxPendingRequests is the default number of pending requests // a single Client may queue before sending them to the server. // // This parameter may be overridden by Client.MaxPendingRequests. DefaultMaxPendingRequests = 1000 // DefaultConcurrency is the default maximum number of concurrent // Server.Handler goroutines the server may run. DefaultConcurrency = 10000 // DefaultHandshakeTimeout is the default timeout before declaring whether or not a handshake has failed. DefaultHandshakeTimeout = 3 * time.Second // DefaultReadBufferSize is the default size for read buffers. DefaultReadBufferSize = 64 * 1024 // DefaultWriteBufferSize is the default size for write buffers. DefaultWriteBufferSize = 64 * 1024 )
Variables ¶
var ( // ErrTimeout is returned from timed out calls. ErrTimeout = fasthttp.ErrTimeout // ErrPendingRequestsOverflow is returned when Client cannot send // more requests to the server due to Client.MaxPendingRequests limit. ErrPendingRequestsOverflow = errors.New("pending requests overflowed") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // NewResponse must return new response object. NewResponse func() ResponseReader // Addr is the Server address to connect to. Addr string // Dial is a custom function used for connecting to the Server. // // fasthttp.Dial is used by default. Dial func(addr string) (net.Conn, error) Handshake func(conn net.Conn) (net.Conn, error) HandshakeTimeout time.Duration // MaxPendingRequests is the maximum number of pending requests // the client may issue until the server responds to them. // // DefaultMaxPendingRequests is used by default. MaxPendingRequests int // MaxBatchDelay is the maximum duration before pending requests // are sent to the server. // // Requests' batching may reduce network bandwidth usage and CPU usage. // // By default requests are sent immediately to the server. MaxBatchDelay time.Duration // Maximum duration for full response reading (including body). // // This also limits idle connection lifetime duration. // // By default response read timeout is unlimited. ReadTimeout time.Duration // Maximum duration for full request writing (including body). // // By default request write timeout is unlimited. WriteTimeout time.Duration // ReadBufferSize is the size for read buffer. // // DefaultReadBufferSize is used by default. ReadBufferSize int // WriteBufferSize is the size for write buffer. // // DefaultWriteBufferSize is used by default. WriteBufferSize int // Prioritizes new requests over old requests if MaxPendingRequests pending // requests is reached. PrioritizeNewRequests bool OnMessageSent func(conn net.Conn) OnMessageRecv func(conn net.Conn) // contains filtered or unexported fields }
Client sends rpc requests to the Server over a single connection.
Use multiple clients for establishing multiple connections to the server if a single connection processing consumes 100% of a single CPU core on either multi-core client or server.
func (*Client) DoDeadline ¶
func (c *Client) DoDeadline(req RequestWriter, resp ResponseReader, deadline time.Time) error
DoDeadline sends the given request to the server set in Client.Addr.
ErrTimeout is returned if the server didn't return response until the given deadline.
func (*Client) PendingRequests ¶
PendingRequests returns the number of pending requests at the moment.
This function may be used either for informational purposes or for load balancing purposes.
func (*Client) SendNowait ¶
func (c *Client) SendNowait(req RequestWriter, releaseReq func(req RequestWriter)) bool
SendNowait schedules the given request for sending to the server set in Client.Addr.
req cannot be used after SendNowait returns and until releaseReq is called. releaseReq is called when the req is no longer needed and may be re-used.
req cannot be re-used if releaseReq is nil.
Returns true if the request is successfully scheduled for sending, otherwise returns false.
Response for the given request is ignored.
type HandlerCtx ¶
type HandlerCtx interface { // ConcurrencyLimitError must set the response // to 'concurrency limit exceeded' error. ConcurrencyLimitError(concurrency int) // Init must prepare ctx for reading the next request. Init(conn net.Conn, logger fasthttp.Logger) // ReadRequest must read request from br. ReadRequest(br *bufio.Reader) error // WriteResponse must write response to bw. WriteResponse(bw *bufio.Writer) error }
HandlerCtx is an interface implementing context passed to Server.Handler
type RequestWriter ¶
type RequestWriter interface { // WriteRequest must write request to bw. WriteRequest(bw *bufio.Writer) error }
RequestWriter is an interface for writing rpc request to buffered writer.
type ResponseReader ¶
type ResponseReader interface { // ReadResponse must read response from br. ReadResponse(br *bufio.Reader) error }
ResponseReader is an interface for reading rpc response from buffered reader.
type Server ¶
type Server struct { // NewHandlerCtx must return new HandlerCtx NewHandlerCtx func() HandlerCtx // Handler must process incoming requests. // // The handler must return either ctx passed to the call // or new non-nil ctx. // // The handler may return ctx passed to the call only if the ctx // is no longer used after returning from the handler. // Otherwise new ctx must be returned. Handler func(ctx HandlerCtx) HandlerCtx Handshake func(conn net.Conn) (net.Conn, error) HandshakeTimeout time.Duration // Concurrency is the maximum number of concurrent goroutines // with Server.Handler the server may run. // // DefaultConcurrency is used by default. Concurrency int // MaxBatchDelay is the maximum duration before ready responses // are sent to the client. // // Responses' batching may reduce network bandwidth usage and CPU usage. // // By default responses are sent immediately to the client. MaxBatchDelay time.Duration // Maximum duration for reading the full request (including body). // // This also limits the maximum lifetime for idle connections. // // By default request read timeout is unlimited. ReadTimeout time.Duration // Maximum duration for writing the full response (including body). // // By default response write timeout is unlimited. WriteTimeout time.Duration // ReadBufferSize is the size for read buffer. // // DefaultReadBufferSize is used by default. ReadBufferSize int // WriteBufferSize is the size for write buffer. // // DefaultWriteBufferSize is used by default. WriteBufferSize int // Logger, which is used by the Server. // // Standard logger from log package is used by default. Logger fasthttp.Logger // PipelineRequests enables requests' pipelining. // // Requests from a single client are processed serially // if is set to true. // // Enabling requests' pipelining may be useful in the following cases: // // - if requests from a single client must be processed serially; // - if the Server.Handler doesn't block and maximum throughput // must be achieved for requests' processing. // // By default requests from a single client are processed concurrently. PipelineRequests bool // contains filtered or unexported fields }
Server accepts rpc requests from Client.