pool

package module
v0.0.0-...-8737e65 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 4 Imported by: 0

README

websocket-connection-pool

Minimalistic Websocket connection pool library for gorilla/websocket

Import

go get github.com/dobecad/websocket-connection-pool

Usage

// Create a new Pool using default settings
pool := DefaultPool("ws://localhost:8080/ws")

// or use the following PoolConfig to customize the default pool
poolOpts := &PoolConfig {
    Capacity: 5,
    PingInterval: time.Minute,
    CreateConn: func (wsUrl string) (*websocket.Conn, error) {
        conn, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
        if err != nil {
            return nil, err
        }
        return conn, nil
    },
}
pool := NewPool("ws://localhost:8080/ws", poolOpts)

// Grab a connection from the Pool
conn, err := pool.GetConnection()

// Release the connection to put the connection back in the pool
defer pool.ReleaseConnection(conn)

Documentation

Index

Constants

View Source
const (
	DefaultPingInterval = 30 * time.Second
	MinCapacity         = 2
	DefaultCapacity     = 5
)

Variables

View Source
var (
	ErrAllConnectionsAcquired = errors.New("all connections have been acquired")
)

Functions

This section is empty.

Types

type Pool

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

func DefaultPool

func DefaultPool(wsUrl string) *Pool

Create a new Websocket Connection Pool using default settings

func NewPool

func NewPool(wsUrl string, config *PoolConfig) *Pool

Create a new Websocket Connection Pool

func (*Pool) Close

func (p *Pool) Close() error

Close all of the connections in the connection Pool

func (*Pool) GetConnection

func (p *Pool) GetConnection() (*websocket.Conn, error)

Grab a connection from the Pool

func (*Pool) ReleaseConnection

func (p *Pool) ReleaseConnection(conn *websocket.Conn)

Release a connection so that it goes back to the Pool

type PoolConfig

type PoolConfig struct {
	// Max number of connections to keep in the Pool. Minimum is 2 connections
	Capacity uint32

	// Function for creating the websocket connection.
	//
	// Allows you to define how your websocket connection should be made
	CreateConn func(string) (*websocket.Conn, error)

	// How often your websocket should ping the server to
	// keep the connection alive
	PingInterval time.Duration
}

Jump to

Keyboard shortcuts

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