Documentation
¶
Index ¶
- Constants
- Variables
- func Bridge(conn *websocket.Conn, messenger model.Messenger, opts ...Option) error
- func Close(ctx context.Context, conn *websocket.Conn, err error) error
- func CloseWith(status websocket.StatusCode, reason string) error
- func CloseWithCause(status websocket.StatusCode, reason string, cause error) error
- func PeerGone(err error) bool
- func ReadFirst[T any](ctx context.Context, conn *websocket.Conn, timeout time.Duration, ...) (T, error)
- type Option
- func WithDone(ch <-chan struct{}, err error) Option
- func WithFilter(filter func(msg entity.PubsubMessage) bool) Option
- func WithFrame(frame func(msg entity.PubsubMessage) any) Option
- func WithInbound(inbound func(ctx context.Context, data []byte) error) Option
- func WithLogger(logger log.Logger) Option
- func WithPing(interval, timeout time.Duration) Option
Constants ¶
const ( DefaultPingInterval = 30 * time.Second DefaultPingTimeout = 10 * time.Second )
Variables ¶
var ErrSubscriptionDropped = CloseWith(websocket.StatusTryAgainLater, "subscription dropped")
ErrSubscriptionDropped ends a session whose messenger's channel closed — the bus evicted a subscriber that stopped draining, or pubsub stopped — telling the client to try again later instead of leaving it on a connection that hears nothing.
Functions ¶
func Bridge ¶
Bridge relays JSON both ways between conn and messenger until the session ends, and returns what the handler returns: nil if the connection went first, else the close the session sent — a dropped subscription, a WithDone, an inbound error. It closes the messenger; conn stays the server's to close.
func Close ¶
Close closes conn as the handler's err says: nil a normal closure; the peer's own close, or EOF, nothing more; ctx done going away; a CloseWith its status and reason; any other error an internal error, the one case it returns, for the caller to log. A close the handler's Bridge sent already is not sent again.
func CloseWith ¶ added in v0.9.8
func CloseWith(status websocket.StatusCode, reason string) error
CloseWith is the error a handler returns to have its connection closed with status and reason — a refusal, a displacement, a "try again later". Close sends them and reports no failure. The peer reads the reason: keep out of it what it should not.
func CloseWithCause ¶ added in v0.9.8
func CloseWithCause(status websocket.StatusCode, reason string, cause error) error
CloseWithCause is CloseWith keeping its cause: the peer gets the reason alone, the error's message says both for the logs, and errors.Is finds the cause.
func PeerGone ¶ added in v0.9.8
PeerGone reports whether err means the peer left, hanging up or sending a close frame. ReadFirst returns such an error as is; a handler tells it from a refused handshake by PeerGone, to log the two apart.
func ReadFirst ¶ added in v0.9.8
func ReadFirst[T any](ctx context.Context, conn *websocket.Conn, timeout time.Duration, reason string) (T, error)
ReadFirst decodes the connection's first frame, JSON, into a T — a handshake such as a registration — waiting no longer than timeout.
If the peer leaves first, it returns the read's error as is, for Close to release the connection quietly. If no frame comes in time, or the frame does not decode, it returns CloseWith(StatusPolicyViolation, reason), reason naming the handshake. PeerGone tells the two apart. Checking what the frame holds is the caller's, refusing one with CloseWithCause(StatusPolicyViolation, reason, why).
The read runs on a goroutine of its own rather than under a context with the timeout, since coder/websocket tears the connection down when a read's context ends; after a timeout it stays parked until the server closes the connection. The frame is decoded here, not by wsjson, which closes the connection itself on a bad one.
Types ¶
type Option ¶
type Option func(*session)
func WithDone ¶ added in v0.9.8
WithDone ends the session once ch closes, closing with err — a CloseWith(StatusNormalClosure, "displaced") for a session a newer one claimed, for instance; nil is a normal closure. Given more than once, the first to close ends it.
func WithFilter ¶ added in v0.9.8
func WithFilter(filter func(msg entity.PubsubMessage) bool) Option
WithFilter writes only the bus messages filter keeps — the session's own, matched against its handshake, for instance. By default every message is written.
func WithFrame ¶ added in v0.9.8
func WithFrame(frame func(msg entity.PubsubMessage) any) Option
WithFrame writes what frame makes of a bus message, as JSON — its payload alone, for instance. By default the message itself is written.
func WithInbound ¶ added in v0.9.8
WithInbound hands each text or binary frame the client sends to inbound — an ack, for instance. An error ends the session with that close: a CloseWith's status and reason, an internal error for any other. By default a frame is relayed to the bus.