websockets

package
v0.38.0-util Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2025 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PingPeriod defines the interval at which ping messages are sent to the client.
	// This value must be less than pongWait, cause it that case the server ensures it sends a ping well before the PongWait
	// timeout elapses. Each new pong message resets the server's read deadline, keeping the connection alive as long as
	// the client is responsive.
	//
	// Example:
	// At t=9, the server sends a ping, initial read deadline is t=10 (for the first message)
	// At t=10, the client responds with a pong. The server resets its read deadline to t=20.
	// At t=18, the server sends another ping. If the client responds with a pong at t=19, the read deadline is extended to t=29.
	//
	// In case of failure:
	// If the client stops responding, the server will send a ping at t=9 but won't receive a pong by t=10. The server then closes the connection.
	PingPeriod = (PongWait * 9) / 10

	// PongWait specifies the maximum time to wait for a pong response message from the peer
	// after sending a ping
	PongWait = 10 * time.Second

	// WriteWait specifies a timeout for the write operation. If the write
	// isn't completed within this duration, it fails with a timeout error.
	// SetWriteDeadline ensures the write operation does not block indefinitely
	// if the client is slow or unresponsive. This prevents resource exhaustion
	// and allows the server to gracefully handle timeouts for delayed writes.
	WriteWait = 10 * time.Second

	// DefaultInactivityTimeout is the default duration a WebSocket connection can remain open without any active subscriptions
	// before being automatically closed
	DefaultInactivityTimeout time.Duration = 1 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code int
const (
	InvalidMessage Code = iota
	InvalidArgument
	NotFound
	SubscriptionError
)

type Config

type Config struct {
	MaxSubscriptionsPerConnection uint64
	MaxResponsesPerSecond         uint64
	// InactivityTimeout specifies the duration a WebSocket connection can remain open without any active subscriptions
	// before being automatically closed
	InactivityTimeout time.Duration
}

func NewDefaultWebsocketConfig

func NewDefaultWebsocketConfig() Config

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

func NewWebSocketController

func NewWebSocketController(
	logger zerolog.Logger,
	config Config,
	conn WebsocketConnection,
	dataProviderFactory dp.DataProviderFactory,
) *Controller

func (*Controller) HandleConnection

func (c *Controller) HandleConnection(ctx context.Context)

HandleConnection manages the lifecycle of a WebSocket connection, including setup, message processing, and graceful shutdown.

Parameters: - ctx: The context for controlling cancellation and timeouts.

type Handler

type Handler struct {
	*common.HttpHandler
	// contains filtered or unexported fields
}

func NewWebSocketHandler

func NewWebSocketHandler(
	logger zerolog.Logger,
	config Config,
	chain flow.Chain,
	maxRequestSize int64,
	dataProviderFactory dp.DataProviderFactory,
) *Handler

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type WebsocketConnection

type WebsocketConnection interface {
	ReadJSON(v interface{}) error
	WriteJSON(v interface{}) error
	WriteControl(messageType int, deadline time.Time) error
	Close() error
	SetReadDeadline(deadline time.Time) error
	SetWriteDeadline(deadline time.Time) error
	SetPongHandler(h func(string) error)
}

type WebsocketConnectionImpl

type WebsocketConnectionImpl struct {
	// contains filtered or unexported fields
}

func NewWebsocketConnection

func NewWebsocketConnection(conn *websocket.Conn) *WebsocketConnectionImpl

func (*WebsocketConnectionImpl) Close

func (c *WebsocketConnectionImpl) Close() error

func (*WebsocketConnectionImpl) ReadJSON

func (c *WebsocketConnectionImpl) ReadJSON(v interface{}) error

func (*WebsocketConnectionImpl) SetPongHandler

func (c *WebsocketConnectionImpl) SetPongHandler(h func(string) error)

func (*WebsocketConnectionImpl) SetReadDeadline

func (c *WebsocketConnectionImpl) SetReadDeadline(deadline time.Time) error

func (*WebsocketConnectionImpl) SetWriteDeadline

func (c *WebsocketConnectionImpl) SetWriteDeadline(deadline time.Time) error

func (*WebsocketConnectionImpl) WriteControl

func (c *WebsocketConnectionImpl) WriteControl(messageType int, deadline time.Time) error

func (*WebsocketConnectionImpl) WriteJSON

func (c *WebsocketConnectionImpl) WriteJSON(v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL