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 )
const ( // DefaultReadBufferSize is the default size for read buffers. DefaultReadBufferSize = 64 * 1024 // DefaultWriteBufferSize is the default size for write buffers. DefaultWriteBufferSize = 64 * 1024 )
const ( // CompressNone disables connection compression. // // CompressNone may be used in the following cases: // // * If network bandwidth between client and server is unlimited. // * If client and server are located on the same physical host. // * If other CompressType values consume a lot of CPU resources. // CompressNone = CompressType(1) // CompressFlate uses compress/flate with default // compression level for connection compression. // // CompressFlate may be used in the following cases: // // * If network bandwidth between client and server is limited. // * If client and server are located on distinct physical hosts. // * If both client and server have enough CPU resources // for compression processing. // CompressFlate = CompressType(0) // CompressSnappy uses snappy compression. // // CompressSnappy vs CompressFlate comparison: // // * CompressSnappy consumes less CPU resources. // * CompressSnappy consumes more network bandwidth. // CompressSnappy = CompressType(2) )
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 overflow. Increase Client.MaxPendingRequests, " + "reduce requests rate or speed up the server") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // SniffHeader is the header written to each connection established // to the server. // // It is expected that the server replies with the same header. SniffHeader string // ProtocolVersion is the version of RequestWriter and ResponseReader. // // The ProtocolVersion must be changed each time RequestWriter // or ResponseReader changes the underlying format. ProtocolVersion byte // NewResponse must return new response object. NewResponse func() ResponseReader // Addr is the Server address to connect to. Addr string // CompressType is the compression type used for requests. // // CompressFlate is used by default. CompressType CompressType // Dial is a custom function used for connecting to the Server. // // fasthttp.Dial is used by default. Dial func(addr string) (net.Conn, error) // TLSConfig is TLS (aka SSL) config used for establishing encrypted // connection to the server. // // Encrypted connections may be used for transferring sensitive // information over untrusted networks. // // By default connection to the server isn't encrypted. TLSConfig *tls.Config // 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 // 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 { // SniffHeader is the header read from each client connection. // // The server sends the same header to each client. SniffHeader string // ProtocolVersion is the version of HandlerCtx.ReadRequest // and HandlerCtx.WriteResponse. // // The ProtocolVersion must be changed each time HandlerCtx.ReadRequest // or HandlerCtx.WriteResponse changes the underlying format. ProtocolVersion byte // 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 // CompressType is the compression type used for responses. // // CompressFlate is used by default. CompressType CompressType // Concurrency is the maximum number of concurrent goroutines // with Server.Handler the server may run. // // DefaultConcurrency is used by default. Concurrency int // TLSConfig is TLS (aka SSL) config used for accepting encrypted // client connections. // // Encrypted connections may be used for transferring sensitive // information over untrusted networks. // // By default server accepts only unencrypted connections. TLSConfig *tls.Config // 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.