Documentation
¶
Index ¶
- Constants
- Variables
- func NewBufferPair(ctx context.Context, size int) (Transport, Transport)
- func NewWebSocketHandler(opts SocketOpts, transportHandler Handler) (h http.Handler)
- func SMuxArg[X any](tr Transport) (x X, ok bool)
- func SMuxClient(tr Transport, h Handler) (top Transport, call SMuxCall)
- type ControlPacket
- type Handler
- type HandshakeResponse
- type SMuxCall
- type SocketOpts
- type Transport
- type TransportError
- type TypeTransport
Constants ¶
const ( // DefaultMaxPacketSize is the maximum size of a JSON packet we accept. DefaultMaxPacketSize = 262144 // 256k, 2^18 // DefaultInMessageBuffer allows for this many packets to be pending before we close the connection. DefaultInMessageBuffer = 128 // DefaultRateLimit is the number of messages per second we allow. DefaultRateLimit = 32 // DefaultRateBurst is the maximum burst of messages we allow. DefaultRateBurst = 128 )
const (
SMuxMessageBuffer = 64
)
Variables ¶
var ( ErrProtocol = errors.New("bad smux") ErrBuffer = errors.New("buffer full") )
Functions ¶
func NewBufferPair ¶ added in v0.1.70
NewBufferPair constructs two Transport interfaces that are connected to each other. Pass a buffer size, or zero for blocking. Internally uses channels, so write/read has those semantics.
func NewWebSocketHandler ¶ added in v0.1.70
func NewWebSocketHandler(opts SocketOpts, transportHandler Handler) (h http.Handler)
NewWebSocketHandler returns an http.Handler that upgrades requests to WebSocket connections and wraps them in a Transport interface. The returned Transport supports reading and writing ControlPacket as well as regular packets. This always sets InsecureSkipVerify, you should wrap this with something that checks the origin. The provided handle function is called for each established connection. When the handle function returns, the WebSocket connection is closed.
func SMuxArg ¶ added in v0.1.77
SMuxArg unmarshals the initial argument used to start the SMux call, if any.
func SMuxClient ¶ added in v0.1.76
SMuxClient represents one side of a multiplexed connection built over a Transport. It can be both the client and/or the server. If the provided Handler is nil, simply closes all incoming calls.
Types ¶
type ControlPacket ¶ added in v0.1.72
ControlPacket wraps an optional control ID and an actual data packet. It has special encoding over a WebSocket, but is otherwise "just a tuple".
type HandshakeResponse ¶ added in v0.1.70
type HandshakeResponse struct {
Ok bool `json:"ok"`
MaxPacketSize int `json:"max_packet_size"`
RateLimit int `json:"rate_limit"`
RateBurst int `json:"rate_burst"`
}
HandshakeResponse is the response sent to the client after a successful hello.
type SMuxCall ¶ added in v0.1.76
SMuxCall makes a call to the other side of this multiplexed Transport.
type SocketOpts ¶ added in v0.1.70
type SocketOpts struct {
// MaxPacketSize is the maximum size of a JSON packet we accept.
// Defaults to DefaultMaxPacketSize if zero.
MaxPacketSize int
// InMessageBuffer allows for this many packets to be pending before we close the connection.
// Defaults to DefaultInMessageBuffer if zero.
InMessageBuffer int
// RateLimit is the number of messages per second we allow.
// This is how much the 'bucket' refills per second.
// Defaults to DefaultRateLimit if zero.
RateLimit int
// RateBurst is the maximum burst of messages we allow.
// This is the total capacity of the 'bucket'.
// Defaults to DefaultRateBurst if zero.
RateBurst int
// PingEvery sends a ping every ~duration, +/- a small random variability.
PingEvery time.Duration
// SubProto, if set, must be provided by the client for this socket to connect properly.
SubProto string
}
SocketOpts configures the WebSocket handler.
type Transport ¶
type Transport interface {
// Context returns the underlying Context for the physical transport, e.g., of the HTTP connection.
Context() (ctx context.Context)
// ReadJSON reads JSON from the transport into the given target.
// It will also return an error if the underlying Transport has shut down.
ReadJSON(v any) (err error)
// WriteJSON writes JSON from the given data to the transport.
// Implementations may queue sends and never return an error.
WriteJSON(v any) (err error)
}
type TransportError ¶ added in v0.1.59
TransportError is a custom error that is sent to the client as a JSON object inside the close reason, with a specific status code.
func DecodeTransportError ¶ added in v0.1.70
func DecodeTransportError(raw string) (err TransportError)
DecodeTransportError decodes a previously encoded TransportError.
func (TransportError) Encode ¶ added in v0.1.70
func (e TransportError) Encode() (out string)
Encode encodes this TransportError to string.
func (TransportError) Error ¶ added in v0.1.59
func (e TransportError) Error() (reason string)
type TypeTransport ¶ added in v0.1.77
type TypeTransport[X any] interface { // Context returns the underlying Context for the physical transport, e.g., of the HTTP connection. Context() (ctx context.Context) // Read reads from this TypeTransport. Read() (x X, err error) // Write writes into this TypeTransport. Write(x X) (err error) }
func NewTyped ¶ added in v0.1.77
func NewTyped[X any](tr Transport) (tt TypeTransport[X])
NewTyped wraps the given Transport with JSON serialization to provide a typed interface.