client

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultMaxBackoff   = 30 * time.Second
	DefaultBackoffReset = 10 * time.Second
	DefaultPoolIdleSize = 10
	DefaultPoolMaxSize  = 100
)
View Source
const (
	UNKNOWN    = -1
	CONNECTING = iota
	IDLE
	RUNNING
)

Status of a Connection.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*Config
	// contains filtered or unexported fields
}

Client connects to one or more Server using HTTP websockets. The Server can then send HTTP requests to execute.

func NewClient

func NewClient(config *Config) *Client

NewClient creates a new Client.

func (*Client) GetID added in v0.0.6

func (c *Client) GetID() string

GetID returns the client ID hash.

func (*Client) PoolStats added in v0.0.6

func (c *Client) PoolStats() map[string]*PoolSize

PoolStats returns stats for all pools.

func (*Client) Shutdown

func (c *Client) Shutdown()

Shutdown the Proxy.

func (*Client) Start

func (c *Client) Start(ctx context.Context)

Start the Proxy.

type Config

type Config struct {
	// Name is an optional client identifier. Only used in logs.
	Name string
	// ID is a required client identifier. All connections are pooled using the ID,
	// so make this unique if you don't want this client pooled with another.
	ID string
	// ClientIDs is simply saved with the connection data and available in stats.
	// It may be empty and is not directly used by this library.
	// It's for you to identify your clients with your own ID(s).
	ClientIDs []interface{}
	// Websocket URLs this client shall connect to.
	Targets []string
	// Minimum count of idle connections to maintain at all times.
	PoolIdleSize int
	// Maximum websocket connections to keep per target.
	PoolMaxSize int
	// SecretKey is passed as a header to the server to "authenticate".
	// The target servers must accept this value.
	SecretKey string
	// How often to reap dead connections from the target pools.
	// This also controls how often to re-try connections to the targets.
	CleanInterval time.Duration
	// How many seconds to backoff on every connection attempt.
	Backoff time.Duration
	// Maximum backoff length.
	MaxBackoff time.Duration
	// What to reset the backoff to when max is hit.
	// Set this to max to stay at max.
	BackoffReset time.Duration
	// If RRConfig is non-nil then the servers provided in Targets are
	// tried sequentially after they cannot be reached in RetryInterval.
	*RoundRobinConfig
	// If this is true, then the servers provided in Targets are tried
	// sequentially after they cannot be reached in RetryInterval.
	// Handler is an optional custom handler for all proxied requests.
	// Leaving this nil makes all requests use an empty http.Client.
	Handler func(http.ResponseWriter, *http.Request)
	// Logger allows routing logs from this package however you'd like.
	// If left nil, you will get no logs. Use DefaultLogger to print logs to stdout.
	mulch.Logger
}

Config is the required data to initialize a client proxy connection.

func NewConfig

func NewConfig() *Config

NewConfig creates a new ProxyConfig.

type Connection

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

Connection handle a single websocket (HTTP/TCP) connection to an Server.

func NewConnection

func NewConnection(pool *Pool) *Connection

NewConnection creates a Connection object.

func (*Connection) Close

func (c *Connection) Close()

Close the ws/tcp connection.

func (*Connection) Connect

func (c *Connection) Connect(ctx context.Context) error

Connect to the remote server using an HTTP websocket.

func (*Connection) Status

func (c *Connection) Status() int

type Pool

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

Pool of connections to a remote Server.

func NewPool

func NewPool(client *Client, target string, secretKey string) *Pool

NewPool creates a new Pool.

func StartPool

func StartPool(ctx context.Context, client *Client, target string, secretKey string) *Pool

StartPool creates and starts a pool in one command.

func (*Pool) Remove

func (p *Pool) Remove(conn *Connection)

Remove a connection from the pool.

func (*Pool) Shutdown

func (p *Pool) Shutdown()

Shutdown and close all connections in the pool.

func (*Pool) Size

func (p *Pool) Size() *PoolSize

Size returns the current telemetric state of the pool.

func (*Pool) Start

func (p *Pool) Start(ctx context.Context)

Start connects to the remote server and runs a ticker loop to maintain the connection.

type PoolSize

type PoolSize struct {
	Disconnects int
	Connecting  int
	Idle        int
	Running     int
	Total       int
	LastConn    time.Time
	LastTry     time.Time
	Active      bool
}

PoolSize represent the number of open connections per status.

func (*PoolSize) String

func (ps *PoolSize) String() string

type RoundRobinConfig added in v0.0.6

type RoundRobinConfig struct {
	// When RoundRobin is true, this configures how long a server is
	// retried unsuccessfully before trying the next server in Targets list.
	RetryInterval time.Duration
	// Callback is called when the tunnel changes.
	Callback func(ctx context.Context, socket string)
}

RoundRobinConfig is the configuration specific to round robin target acquisition.

Jump to

Keyboard shortcuts

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