Documentation
¶
Overview ¶
Package quasizero implements a general purpose, ultra-low latency TCP server.
Example ¶
package main import ( "context" "fmt" "net" "time" "github.com/bsm/quasizero" ) func main() { // start a TCP listener lis, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } defer lis.Close() // define command map cmds := map[int32]quasizero.Handler{ // ECHO 1: quasizero.HandlerFunc(func(req *quasizero.Request, res *quasizero.Response) error { res.Set(req.Payload) return nil }), // SHUTDOWN 9: quasizero.HandlerFunc(func(req *quasizero.Request, res *quasizero.Response) error { go func() { time.Sleep(time.Second) _ = lis.Close() }() res.SetString("OK") return nil }), } // start serving (in background) go func() { srv := quasizero.NewServer(cmds, nil) if err := srv.Serve(lis); err != nil { panic(err) } }() // connect client clnt, err := quasizero.NewClient(context.TODO(), lis.Addr().String(), nil) if err != nil { panic(err) } defer clnt.Close() // send an echo request res1, err := clnt.Call(&quasizero.Request{Code: 1, Payload: []byte("hello")}) if err != nil { panic(err) } fmt.Printf("server responded to ECHO with %q\n", res1.Payload) // send a shutdown request res2, err := clnt.Call(&quasizero.Request{Code: 9}) if err != nil { panic(err) } fmt.Printf("server responded to SHUTDOWN with %q\n", res2.Payload) }
Output: server responded to ECHO with "hello" server responded to SHUTDOWN with "OK"
Index ¶
- type Client
- type Handler
- type HandlerFunc
- type Pipeline
- type Request
- func (*Request) Descriptor() ([]byte, []int)
- func (m *Request) GetCode() int32
- func (m *Request) GetMetadata() map[string]string
- func (m *Request) GetPayload() []byte
- func (*Request) ProtoMessage()
- func (m *Request) Reset()
- func (m *Request) SetMeta(key, value string)
- func (m *Request) String() string
- func (m *Request) XXX_DiscardUnknown()
- func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Request) XXX_Merge(src proto.Message)
- func (m *Request) XXX_Size() int
- func (m *Request) XXX_Unmarshal(b []byte) error
- type Response
- func (*Response) Descriptor() ([]byte, []int)
- func (m *Response) GetErrorMessage() string
- func (m *Response) GetMetadata() map[string]string
- func (m *Response) GetPayload() []byte
- func (*Response) ProtoMessage()
- func (m *Response) Release()
- func (m *Response) Reset()
- func (m *Response) Set(data []byte)
- func (m *Response) SetError(err error)
- func (m *Response) SetErrorf(msg string, args ...interface{})
- func (m *Response) SetMeta(key, value string)
- func (m *Response) SetString(data string)
- func (m *Response) String() string
- func (m *Response) XXX_DiscardUnknown()
- func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Response) XXX_Merge(src proto.Message)
- func (m *Response) XXX_Size() int
- func (m *Response) XXX_Unmarshal(b []byte) error
- type ResponseBatch
- type Server
- type ServerConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client holds a pool of connections to a quasizero server instance.
func NewClientDialer ¶
func NewClientDialer(ctx context.Context, d *net.Dialer, addr string, opt *pool.Options) (*Client, error)
NewClientDialer connects a client through a custom dialer.
type HandlerFunc ¶
HandlerFunc is a Handler short-cut.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline can execute commands.
func (*Pipeline) Exec ¶
func (p *Pipeline) Exec() (ResponseBatch, error)
Exec executes the pipeline and returns responses.
type Request ¶
type Request struct { // Request/command code. Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` // Custom metadata. Metadata map[string]string `` /* 157-byte string literal not displayed */ // Raw payload. Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
func (*Request) Descriptor ¶
func (*Request) GetMetadata ¶
func (*Request) GetPayload ¶
func (*Request) ProtoMessage ¶
func (*Request) ProtoMessage()
func (*Request) XXX_DiscardUnknown ¶
func (m *Request) XXX_DiscardUnknown()
func (*Request) XXX_Marshal ¶
func (*Request) XXX_Unmarshal ¶
type Response ¶
type Response struct { // Optional error message. ErrorMessage string `protobuf:"bytes,1,opt,name=error_message,json=errorMessage,proto3" json:"error_message,omitempty"` // Custom metadata. Metadata map[string]string `` /* 157-byte string literal not displayed */ // Raw payload. Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
func (*Response) Descriptor ¶
func (*Response) GetErrorMessage ¶
func (*Response) GetMetadata ¶
func (*Response) GetPayload ¶
func (*Response) ProtoMessage ¶
func (*Response) ProtoMessage()
func (*Response) Release ¶
func (m *Response) Release()
Release releases the message and returns it to the memory pool. You must not use it after calling this function.
func (*Response) XXX_DiscardUnknown ¶
func (m *Response) XXX_DiscardUnknown()
func (*Response) XXX_Marshal ¶
func (*Response) XXX_Unmarshal ¶
type ResponseBatch ¶
type ResponseBatch []*Response
ResponseBatch is a slice of individual responses.
func (ResponseBatch) Release ¶
func (b ResponseBatch) Release()
Release releases the response batch and returns it to the memory pool. You must not use the batch or any of the included responses after calling this function.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server instances can handle client requests.
type ServerConfig ¶
type ServerConfig struct { // Timeout represents the per-request socket read/write timeout. // Default: 0 (disabled) Timeout time.Duration // IdleTimeout forces servers to close idle connection once timeout is reached. // Default: 0 (disabled) IdleTimeout time.Duration // If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence // of communication. This is useful for two reasons: // 1) Detect dead peers. // 2) Take the connection alive from the point of view of network // equipment in the middle. // On Linux, the specified value (in seconds) is the period used to send ACKs. // Note that to close the connection the double of the time is needed. // On other kernels the period depends on the kernel configuration. // Default: 0 (disabled) TCPKeepAlive time.Duration // OnError is called on client errors. Use for verbose logging. OnError func(error) }
ServerConfig holds the server configuration