Documentation
¶
Overview ¶
Package socket provides a server and client for creating and using sessions over a socket.
Index ¶
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 is a low-level client for reading and writing to a socket.
func NewClient ¶
func NewClient(address string, options ...ClientOption) *Client
NewClient creates and returns a new client configured with the provided address.
type ClientOption ¶ added in v0.8.0
type ClientOption func(*Client)
func WithTCPDialer ¶ added in v0.8.0
func WithTCPDialer() ClientOption
WithTCPDialer sets the dialer to use 'tcp' instead of 'unix'.
type Dispatcher ¶ added in v0.6.0
type Dispatcher struct {
NotFound Handler
// contains filtered or unexported fields
}
Dispatcher is a Handler that dispatches requests based on the type byte set in the beginning of the incoming payload.
func NewDispatcher ¶ added in v0.6.0
func NewDispatcher() *Dispatcher
NewDispatcher creates and return a new dispatcher.
func (*Dispatcher) Handle ¶ added in v0.6.0
func (d *Dispatcher) Handle(t byte, h Handler)
Handle registers the handler for the given type byte in the dispatcher.
func (*Dispatcher) HandleFunc ¶ added in v0.6.0
func (d *Dispatcher) HandleFunc(t byte, f func(context.Context, Socket))
HandleFunc registers the handler for the given type byte in the dispatcher.
func (*Dispatcher) ServeSocket ¶ added in v0.6.0
func (d *Dispatcher) ServeSocket(ctx context.Context, s Socket)
ServeSocket reads the leading type byte and calls the handler with the matching type byte.
type HandlerFunc ¶
HandlerFunc is an adapter to allow the use of ordinary functions as socket handlers.
func (HandlerFunc) ServeSocket ¶
func (f HandlerFunc) ServeSocket(ctx context.Context, s Socket)
ServeSocket calls f(ctx, s)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func NewServer ¶
func NewServer(address string, handler Handler, options ...ServerOption) *Server
NewServer creates and returns a new socket server.
type ServerOption ¶
type ServerOption func(*Server)
func WithLogger ¶
func WithLogger(logger *slog.Logger) ServerOption
WithLogger sets logger on the server.
func WithTCPListener ¶ added in v0.8.0
func WithTCPListener() ServerOption
WithTCPListener sets the listener to use 'tcp' instead of 'unix'.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ServerOption
WithTimeout sets the timeout for all connections.
type Socket ¶
type Socket interface {
io.ReadWriter
// Deadline returns the configured deadline
// on the socket.
Deadline() time.Time
// SetDeadline sets the read and write deadlines associated with the connection.
// It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
SetDeadline(t time.Time) error
}
Socket wraps around io.ReadWriter and SetDeadline. The internal handler is responsible for closing the socket (connection).