websockets

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package websockets used for communicating with the server via WebSocket protocol

Package websockets used for communicating with the server via WebSocket protocol

Package websockets provides functions to manage the CookieFarm client submission.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircuitOpen = errors.New("circuit breaker is open")
)
View Source
var OnNewConfig func()

Functions

func ConfigHandler

func ConfigHandler(payload json.RawMessage) error

ConfigHandler processes the configuration update received from the WebSocket server

func GetConnection

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

bad handshake (401) connection refused (503)

func Start

func Start(flagsChan <-chan models.ClientData) error

Start initializes the submission loop to the cookiefarm server.

func WSHandleMessage

func WSHandleMessage(message []byte) error

WSHandleMessage processes incoming WebSocket messages based on their type

func WSReader

func WSReader(conn *websocket.Conn)

WSReader reads messages from the WebSocket connection and handles them

Types

type CircuitBreaker

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

CircuitBreaker is a struct that implements the circuit breaker pattern

func (*CircuitBreaker) IsAllowed

func (cb *CircuitBreaker) IsAllowed() bool

IsAllowed checks if a connection attempt is allowed based on the circuit breaker state

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure registers a failed connection attempt and updates the state

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess registers a successful connection and resets the failure count

type CircuitState

type CircuitState int

CircuitState state of the circuit breaker

const (
	FlagEvent   = "flag"
	ConfigEvent = "config"

	StateClosed   CircuitState = iota // Allowed connections
	StateHalfOpen                     // Allowed connections with limited retries
	StateOpen                         // Blocked connections

)

type ConnectionMonitor

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

ConnectionMonitor monitors WebSocket connections and provides statistics

func GetMonitor

func GetMonitor() *ConnectionMonitor

GetMonitor returns the singleton instance of ConnectionMonitor

func (*ConnectionMonitor) GetStats

func (m *ConnectionMonitor) GetStats() ConnectionStats

GetStats returns a copy of the current connection statistics

func (*ConnectionMonitor) GetStatus

func (m *ConnectionMonitor) GetStatus() ConnectionStatus

GetStatus returns the current connection status

func (*ConnectionMonitor) MeasureLatency

func (m *ConnectionMonitor) MeasureLatency()

MeasureLatency sends a ping and measures the time until pong is received

func (*ConnectionMonitor) RecordDisconnect

func (m *ConnectionMonitor) RecordDisconnect(err error)

RecordDisconnect records a disconnection event

func (*ConnectionMonitor) RecordMessageReceived

func (m *ConnectionMonitor) RecordMessageReceived()

RecordMessageReceived records statistics about received messages

func (*ConnectionMonitor) RecordMessageSent

func (m *ConnectionMonitor) RecordMessageSent()

RecordMessageSent records statistics about sent messages

func (*ConnectionMonitor) SetConnection

func (m *ConnectionMonitor) SetConnection(conn *websocket.Conn)

SetConnection registers a WebSocket connection with the monitor

func (*ConnectionMonitor) SetStatus

func (m *ConnectionMonitor) SetStatus(status ConnectionStatus)

SetStatus updates the connection status

func (*ConnectionMonitor) StopMonitoring

func (m *ConnectionMonitor) StopMonitoring()

StopMonitoring stops the monitoring process

type ConnectionStats

type ConnectionStats struct {
	// Connection tracking
	ConnectionAttempts int
	SuccessfulConnects int
	FailedConnects     int
	LastConnectTime    time.Time
	LastDisconnectTime time.Time

	// Message tracking
	MessagesSent     int
	MessagesReceived int
	LastSendTime     time.Time
	LastReceiveTime  time.Time

	// Error tracking
	LastError       error
	ConsecutiveErrs int

	// Status
	CurrentStatus ConnectionStatus

	// Latency tracking
	LastPingTime   time.Time
	LastPongTime   time.Time
	CurrentLatency time.Duration
	AverageLatency time.Duration
	// contains filtered or unexported fields
}

ConnectionStats holds statistics about the WebSocket connection

type ConnectionStatus

type ConnectionStatus int

ConnectionStatus represents the current status of the WebSocket connection

const (
	StatusDisconnected ConnectionStatus = iota
	StatusConnecting
	StatusConnected
	StatusReconnecting
	StatusFailed
)

type Event

type Event struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

type EventWS

type EventWS struct {
	Type    string `json:"type"`
	Payload []byte `json:"payload"`
}

type EventWSFlag

type EventWSFlag struct {
	Type    string            `json:"type"`
	Payload models.ClientData `json:"payload"`
}

type NewMessageEvent

type NewMessageEvent struct {
	Sent time.Time `json:"sent"`
}

NewMessageEvent represents a new message event

Jump to

Keyboard shortcuts

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