connection

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package connection provides a Websocket that will automatically reconnect if the connection is dropped.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTooManyReconnects is returned when the number of reconnections
	// has reached MaxReconnectionsTotal within MaxElapsedTime.
	ErrTooManyReconnects = errors.New("websocket cannot reconnect right now (too many attemps)")
	// ErrNotDialed is returned when WriteMessage is called, but
	// the websocket has not been created yet (call Dial).
	ErrNotDailed = errors.New("websocket not created yet, please call Dial()")
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// InitialInterval is the first interval at which the backoff starts
	// running.
	InitialInterval time.Duration
	// RandomizationFactor is used to create the range of values:
	// [currentInterval - randomizationFactor * currentInterval,
	// currentInterval + randomizationFactor * currentInterval] and picking
	// a random value from the range.
	RandomizationFactor float64
	// Multiplier is used to increment the backoff interval by multiplying it.
	Multiplier float64
	// MaxInterval is an interval such that, once reached, the backoff will
	// retry with a constant delay of MaxInterval.
	MaxInterval time.Duration
	// MaxElapsedTime is the amount of time after which the ExponentialBackOff
	// returns Stop. It never stops if MaxElapsedTime == 0.
	MaxElapsedTime time.Duration
	// MaxReconnectionsTotal is the maximum number of reconnections that can
	// happen within MaxReconnectionsTime.
	MaxReconnectionsTotal int
	// MaxReconnectionsTime is the time period during which the number of
	// reconnections must be less than MaxReconnectionsTotal to allow a
	// reconnection atttempt.
	MaxReconnectionsTime time.Duration
	// contains filtered or unexported fields
}

Conn contains the state needed to connect, reconnect, and send messages. Default values must be updated before calling `Dial`.

func NewConn

func NewConn() *Conn

NewConn creates a new Conn with default values.

func (*Conn) CanConnect

func (c *Conn) CanConnect() bool

CanConnect checks whether it is possible to reconnect given the recent number of attempts.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the network connection and cleans up private resources after the connection is done.

func (*Conn) Dial

func (c *Conn) Dial(address string, header http.Header) error

Dial creates a new persistent client connection and sets the necessary state for future reconnections. It also starts a goroutine to reset the number of reconnections.

A call to Dial is a prerequisite to writing any messages. The function only needs to be called once on start to create the connection. Alternatively, if Close is called, Dial will have to be called again if the connection needs to be recreated.

The function returns an error if the url is invalid or if a 4XX error (except 408 and 425) is received in the HTTP response.

func (*Conn) IsConnected

func (c *Conn) IsConnected() bool

IsConnected returns the WebSocket connection state.

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data []byte) error

WriteMessage sends a message as a slice of bytes. If the write fails or a disconnect has been detected, it will close the connection and try to reconnect and resend the message.

The write will fail under the following conditions:

  1. The client has not called Dial (ErrNotDialed).
  2. The connection is disconnected and it was not able to reconnect (ErrTooManyReconnects or an internal connection error).
  3. The write call in the websocket package failed (gorilla/websocket error).

Jump to

Keyboard shortcuts

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