Documentation ¶
Overview ¶
Package connection provides a Websocket that will automatically reconnect if the connection is dropped.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 (*Conn) CanConnect ¶
CanConnect checks whether it is possible to reconnect given the recent number of attempts.
func (*Conn) Close ¶
Close closes the network connection and cleans up private resources after the connection is done.
func (*Conn) Dial ¶
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 ¶
IsConnected returns the WebSocket connection state.
func (*Conn) WriteMessage ¶
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:
- The client has not called Dial (ErrNotDialed).
- The connection is disconnected and it was not able to reconnect (ErrTooManyReconnects or an internal connection error).
- The write call in the websocket package failed (gorilla/websocket error).