server

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: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKey    = errors.New("invalid secret key provided")
	ErrNoClientID    = errors.New("required client id header is missing")
	ErrNoProxyTarget = errors.New("no proxy target found for request")
	ErrInvalidData   = errors.New("invalid data received")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Dispatchers uint          `json:"dispatchers" toml:"dispatchers" yaml:"dispatchers" xml:"dispatchers"`
	Timeout     time.Duration `json:"timeout" toml:"timeout" yaml:"timeout" xml:"timeout"`
	IdleTimeout time.Duration `json:"idleTimeout" toml:"idle_timeout" yaml:"idleTimeout" xml:"idle_timeout"`
	SecretKey   string        `json:"secretKey" toml:"secret_key" yaml:"secretKey" xml:"secret_key"`
	// IDHeader sets the upstream header to parse for a remote client.
	// Default behavior is to send requests to clients randomly.
	// If this value is set, requests can only be directed to clients by providing the client ID in this header.
	IDHeader string `json:"idHeader" toml:"id_header" yaml:"idHeader" xml:"id_header"`
	// If a KeyValidator method is provided, then Secretkey is ignored.
	// If the validator returns a string then all pool IDs become a
	// sha256 of that string and the client's generated or provided id.
	// See the NewPool function to see that in action.
	// This allows you to let clients provide their own ID, but a secure
	// access-ID is created with your provided seed to prevent hash collisions.
	KeyValidator func(context.Context, http.Header) (string, error) `json:"-" toml:"-" yaml:"-" xml:"-"`
	// Logger allows routing logs from this package to somewhere special.
	// If left nil logs are written to stdout.
	Logger mulch.Logger `json:"-" toml:"-" yaml:"-" xml:"-"`
}

Config configures a Server.

func NewConfig

func NewConfig() *Config

NewConfig creates a new ProxyConfig.

type ConnStats

type ConnStats struct {
	Remote    string    `json:"remote"`
	Requests  int       `json:"requests"`
	Connected time.Time `json:"conneteed"`
	Idle      string    `json:"idle"`
}

type Connection

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

Connection manages a single websocket connection from the peer. Supports multiple connections from a single peer at the same time (a pool).

func NewConnection

func NewConnection(pool *Pool, sock *websocket.Conn) *Connection

NewConnection returns a new Connection. Each connection gets a go routine to read (wait for) messages.

func (*Connection) Close

func (c *Connection) Close(reason string)

Close the connection.

func (*Connection) Give

func (c *Connection) Give()

Give a connection back to the idle buffer pool. Signals that this connection is ready to be used again. This gets called when an http request has completed.

func (*Connection) Status

func (c *Connection) Status() ConnectionStatus

func (*Connection) Take

func (c *Connection) Take() *Connection

Take notifies that this connection is going to be used. Returns nil if the connection is busy. This gets called at the beginning of an http request.

type ConnectionStatus

type ConnectionStatus int

ConnectionStatus is an enumeration that represents the status of WebSocket connection.

const (
	// Idle state means it is opened but not doing work now.
	// The default value for Connection is Idle.
	Idle   ConnectionStatus = iota
	Busy                    // Unavailable for use.
	Closed                  // Never use again.
)

func (ConnectionStatus) String

func (c ConnectionStatus) String() string

type Metrics

type Metrics struct {
	Uptime    prometheus.CounterFunc
	Pools     prometheus.Gauge
	Conns     *prometheus.GaugeVec
	Regs      *prometheus.CounterVec
	PoolConns *prometheus.GaugeVec
	// contains filtered or unexported fields
}

Metrics contains the exported application metrics in prometheus format.

func (*Metrics) Wrap

func (m *Metrics) Wrap(next http.HandlerFunc, handler string) http.Handler

type Pool

type Pool struct {
	mulch.Logger
	// contains filtered or unexported fields
}

Pool handles all connections from the peer. Each pool is unique by it's clientID.

func NewPool

func NewPool(server *Server, client *PoolConfig, altID string) *Pool

NewPool creates a new Pool, and starts one go routine per pool to keep it clean and running. Each pool represents 1 client, and each client may have many connections.

func (*Pool) IsEmpty

func (pool *Pool) IsEmpty() bool

IsEmpty cleans the pool and return true if the pool is empty.

func (*Pool) Register

func (pool *Pool) Register(ws *websocket.Conn)

Register creates a new Connection and adds it to the pool.

func (*Pool) Shutdown

func (pool *Pool) Shutdown()

Shutdown closes every connection in the pool and closes all channels.

func (*Pool) Size

func (pool *Pool) Size(now time.Time) *PoolSize

Size return the number of connection in each state in the pool. Uses `now` to calculate how long a connection has been established.

type PoolConfig

type PoolConfig struct {
	*mulch.Handshake
	Sock *websocket.Conn
	// contains filtered or unexported fields
}

PoolConfig is a struct for transitting a new pool's data through a channel.

type PoolSize

type PoolSize struct {
	Total  int          `json:"total"`
	Idle   int          `json:"idle"`
	Busy   int          `json:"busy"`
	Closed int          `json:"closed"`
	Conns  []*ConnStats `json:"conns"`
}

PoolSize is the number of connection in each state in the pool.

type Server

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

Server is a Reverse HTTP Proxy over WebSocket. This is the Server part, Clients offer websocket connections, and those are pooled to transfer HTTP Requests and responses.

func NewServer

func NewServer(config *Config) *Server

NewServer return a new Server instance.

func (*Server) HandleRegister

func (s *Server) HandleRegister() http.Handler

HandleRegister receives http requests for /register paths. Receives the WebSocket upgrade handshake request from clients.

func (*Server) HandleRequest

func (s *Server) HandleRequest(name string) http.Handler

HandleRequest receives http requests for /request paths.

func (*Server) HandleStats

func (s *Server) HandleStats(resp http.ResponseWriter, req *http.Request)

func (*Server) ProxyError

func (s *Server) ProxyError(resp http.ResponseWriter, req *http.Request, err error, regFail string)

ProxyError log error and return a HTTP 526 error with the message.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown stops the Server.

func (*Server) StartDispatcher

func (s *Server) StartDispatcher()

StartDispatcher dispatches connections from available pools to client requests. You need to start this in a go routine.

type Stats

type Stats struct {
	Pools   map[clientID]any `json:"pools"`
	Threads map[uint]uint64  `json:"threads"`
}

Jump to

Keyboard shortcuts

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