wsutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const DefaultTimeout = time.Minute

Variables

View Source
var WSBuffer = 12
View Source
var WSReadLimit int64 = 8192000 // 8 MiB

Functions

func NewDialLimiter

func NewDialLimiter() *rate.Limiter

func NewGlobalIdentityLimiter

func NewGlobalIdentityLimiter() *rate.Limiter

func NewIdentityLimiter

func NewIdentityLimiter() *rate.Limiter

func NewSendLimiter

func NewSendLimiter() *rate.Limiter

Types

type Conn

type Conn struct {
	*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(driver json.Driver) *Conn

func (*Conn) Close

func (c *Conn) Close(err error) 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. Context needs to be passed in order
	// to re-use the context that's already used for the limiter.
	Send(context.Context, []byte) error

	// Close should close the websocket connection. The connection will not be
	// reused.
	// If error is nil, the connection should close with a StatusNormalClosure
	// (1000). If not, it should close with a StatusProtocolError (1002).
	Close(err error) error
}

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

type Event

type Event struct {
	Data []byte

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

type Websocket

type Websocket struct {
	Conn Connection
	Addr string

	SendLimiter *rate.Limiter
	DialLimiter *rate.Limiter
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, addr string) (*Websocket, error)

func NewCustom

func NewCustom(
	ctx context.Context, conn Connection, addr string) (*Websocket, error)

NewCustom creates a new undialed Websocket.

func (*Websocket) Close

func (ws *Websocket) Close(err error) error

func (*Websocket) Listen

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

func (*Websocket) Redial

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

func (*Websocket) Send

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

Jump to

Keyboard shortcuts

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