wsutil

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2020 License: ISC Imports: 16 Imported by: 5

Documentation

Overview

Package wsutil provides abstractions around the Websocket, including rate limits.

Index

Constants

View Source
const CopyBufferSize = 2048

Variables

View Source
var (
	// WSTimeout is the timeout for connecting and writing to the Websocket,
	// before Gateway cancels and fails.
	WSTimeout = 5 * time.Minute
	// WSBuffer is the size of the Event channel. This has to be at least 1 to
	// make space for the first Event: Ready or Resumed.
	WSBuffer = 10
	// WSError is the default error handler
	WSError = func(err error) { log.Println("Gateway error:", err) }
	// WSExtraReadTimeout is the duration to be added to Hello, as a read
	// timeout for the websocket.
	WSExtraReadTimeout = time.Second
	// WSDebug is used for extra debug logging. This is expected to behave
	// similarly to log.Println().
	WSDebug = func(v ...interface{}) {}
)
View Source
var CloseDeadline = time.Second

CloseDeadline controls the deadline to wait for sending the Close frame.

View Source
var ErrEmptyPayload = errors.New("empty payload")
View Source
var ErrWebsocketClosed = errors.New("websocket is closed")

ErrWebsocketClosed is returned if the websocket is already closed.

Functions

func HandleEvent added in v0.5.0

func HandleEvent(h EventHandler, ev Event) error

func InjectValues

func InjectValues(rawurl string, values url.Values) string

func NewDialLimiter

func NewDialLimiter() *rate.Limiter

func NewGlobalIdentityLimiter

func NewGlobalIdentityLimiter() *rate.Limiter

func NewIdentityLimiter

func NewIdentityLimiter() *rate.Limiter

func NewSendLimiter

func NewSendLimiter() *rate.Limiter

func WaitForEvent added in v0.5.0

func WaitForEvent(ctx context.Context, h EventHandler, ch <-chan Event, fn func(*OP) bool) error

WaitForEvent blocks until fn() returns true. All incoming events are handled regardless.

Types

type Conn

type Conn struct {
	Conn *websocket.Conn
	json.Driver
	// contains filtered or unexported fields
}

Conn is the default Websocket connection. It compresses all payloads using zlib.

func NewConn

func NewConn() *Conn

func NewConnWithDriver added in v0.5.0

func NewConnWithDriver(driver json.Driver) *Conn

func (*Conn) Close

func (c *Conn) Close() (err error)

func (*Conn) Dial

func (c *Conn) Dial(ctx context.Context, addr string) error

func (*Conn) Listen

func (c *Conn) Listen() <-chan Event

func (*Conn) Send

func (c *Conn) Send(ctx context.Context, b []byte) error

type Connection

type Connection interface {
	// Dial dials the address (string). Context needs to be passed in for
	// timeout. This method should also be re-usable after Close is called.
	Dial(context.Context, string) error

	// Listen sends over events constantly. Error will be non-nil if Data is
	// nil, so check for Error first.
	Listen() <-chan Event

	// Send allows the caller to send bytes. Thread safety is a requirement.
	Send(context.Context, []byte) error

	// Close should close the websocket connection. The connection will not be
	// reused.
	Close() error
}

Connection is an interface that abstracts around a generic Websocket driver. This connection expects the driver to handle compression by itself, including modifying the connection URL.

type Event

type Event struct {
	Data []byte

	// Error is non-nil if Data is nil.
	Error error
}

type EventHandler added in v0.5.0

type EventHandler interface {
	HandleOP(op *OP) error
}

type EventLoopHandler added in v0.10.0

type EventLoopHandler interface {
	EventHandler
	HeartbeatCtx(context.Context) error
}

TODO API

type ExtraHandler added in v0.5.0

type ExtraHandler struct {
	Check func(*OP) bool
	// contains filtered or unexported fields
}

type ExtraHandlers added in v0.5.0

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

func (*ExtraHandlers) Add added in v0.5.0

func (ex *ExtraHandlers) Add(check func(*OP) bool) (<-chan *OP, func())

func (*ExtraHandlers) Check added in v0.5.0

func (ex *ExtraHandlers) Check(op *OP)

Check runs and sends OP data. It is not thread-safe.

type OP added in v0.5.0

type OP struct {
	Code OPCode   `json:"op"`
	Data json.Raw `json:"d,omitempty"`

	// Only for Gateway Dispatch (op 0)
	Sequence  int64  `json:"s,omitempty"`
	EventName string `json:"t,omitempty"`
}

func AssertEvent added in v0.5.0

func AssertEvent(ev Event, code OPCode, v interface{}) (*OP, error)

func DecodeOP added in v0.5.0

func DecodeOP(ev Event) (*OP, error)

func (*OP) UnmarshalData added in v0.5.0

func (op *OP) UnmarshalData(v interface{}) error

type OPCode added in v0.5.0

type OPCode uint8

OPCode is a generic type for websocket OP codes.

type PacemakerLoop added in v0.5.0

type PacemakerLoop struct {
	Extras ExtraHandlers

	ErrorLog func(error)
	// contains filtered or unexported fields
}

PacemakerLoop provides an event loop with a pacemaker.

func NewLoop added in v0.5.0

func NewLoop(heartrate time.Duration, evs <-chan Event, evl EventLoopHandler) *PacemakerLoop

func (*PacemakerLoop) Echo added in v0.5.0

func (p *PacemakerLoop) Echo()

Echo calls the pacemaker's Echo function.

func (*PacemakerLoop) Pace added in v0.5.0

func (p *PacemakerLoop) Pace(ctx context.Context) error

Pace calls the pacemaker's Pace function.

func (*PacemakerLoop) RunAsync added in v0.5.0

func (p *PacemakerLoop) RunAsync(exit func(error))

func (*PacemakerLoop) Stop added in v0.5.0

func (p *PacemakerLoop) Stop()

Stop calls the pacemaker's Stop function.

func (*PacemakerLoop) Stopped added in v0.5.0

func (p *PacemakerLoop) Stopped() bool

type Websocket

type Websocket struct {
	Conn Connection
	Addr string

	// Timeout for connecting and writing to the Websocket, uses default
	// WSTimeout (global).
	Timeout time.Duration

	SendLimiter *rate.Limiter
	DialLimiter *rate.Limiter
}

func New

func New(addr string) *Websocket

func NewCustom

func NewCustom(conn Connection, addr string) *Websocket

NewCustom creates a new undialed Websocket.

func (*Websocket) Close

func (ws *Websocket) Close() error

func (*Websocket) Dial

func (ws *Websocket) Dial(ctx context.Context) error

func (*Websocket) Listen

func (ws *Websocket) Listen() <-chan Event

func (*Websocket) Send

func (ws *Websocket) Send(b []byte) error

func (*Websocket) SendCtx added in v0.10.0

func (ws *Websocket) SendCtx(ctx context.Context, b []byte) error

Jump to

Keyboard shortcuts

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