cluster

package
v1.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChannelNotFound is returned when a channel is not issued, or no node holds its WebSocket.
	ErrChannelNotFound = errors.New("channel not found")
	// ErrChannelInUse is returned when another node already holds the WebSocket of the channel.
	ErrChannelInUse = errors.New("channel is already in use")
	// ErrChannelExists is returned when the channel ID has already been issued.
	ErrChannelExists = errors.New("channel already exists")
)

Functions

func Config

func Config(nodeName string, port int, myIP string) *memberlist.Config

func DecodeNotifyMsg added in v1.4.0

func DecodeNotifyMsg[T any](msg *NotifyMsg) (*T, error)

Types

type AuthMessage added in v1.4.0

type AuthMessage struct {
	NotifyMsg
	SessionID string `json:"session_id"`
	Token     string `json:"token"`
}

type Cluster added in v1.10.0

type Cluster interface {
	// Start begins participating in the cluster. Background work stops when ctx is done.
	Start(ctx context.Context) error
	// Subscribe registers the callback for messages broadcast by other nodes.
	// It must be called before Start.
	Subscribe(fn NotifyMsgFn)
	// Broadcast sends msg to all other nodes.
	Broadcast(ctx context.Context, msg any) error
}

type Memberlist

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

func NewMemberlist added in v1.10.0

func NewMemberlist(cfg MemberlistConfig) (*Memberlist, error)

func (*Memberlist) ActiveNodes

func (m *Memberlist) ActiveNodes() []*memberlist.Node

func (*Memberlist) ActiveNodesWithoutSelf

func (m *Memberlist) ActiveNodesWithoutSelf() []*memberlist.Node

func (*Memberlist) Broadcast added in v1.10.0

func (m *Memberlist) Broadcast(_ context.Context, msg any) error

func (*Memberlist) MyNodeName

func (m *Memberlist) MyNodeName() string

func (*Memberlist) Peers added in v1.10.0

func (m *Memberlist) Peers(_ context.Context) ([]string, error)

func (*Memberlist) Start

func (m *Memberlist) Start(ctx context.Context) error

func (*Memberlist) Subscribe added in v1.10.0

func (m *Memberlist) Subscribe(fn NotifyMsgFn)

type MemberlistConfig added in v1.10.0

type MemberlistConfig struct {
	// NodeName defaults to $HOSTNAME (or a random name when unset).
	NodeName string
	// BindPort is the gossip protocol port.
	BindPort int
	MyIP     string
	// ServerPort is the HTTP port of the server, used to build peer addresses.
	ServerPort int
	PeerDomain string
	// SyncDuration is the interval for retrying to join peers resolved from PeerDomain.
	SyncDuration time.Duration
}

type NotifyMsg added in v1.4.0

type NotifyMsg struct {
	MsgType NotifyMsgType   `json:"msg_type"`
	Raw     json.RawMessage `json:"-"`
}

type NotifyMsgFn added in v1.4.0

type NotifyMsgFn func(msg *NotifyMsg)

type NotifyMsgType added in v1.4.0

type NotifyMsgType string
const (
	NotifyMsgType_AuthMessage NotifyMsgType = "auth_message"
)

type PeerCluster added in v1.10.0

type PeerCluster interface {
	Cluster
	// Peers returns the HTTP addresses (host:port) of the other active nodes.
	Peers(ctx context.Context) ([]string, error)
}

PeerCluster is a cluster whose nodes can reach each other directly over HTTP.

type Redis added in v1.10.0

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

Redis is a Relay implementation. Every node subscribes to a broadcast channel and its own inbox channel, and the issued channels and their holders are stored as keys.

func NewRedis added in v1.10.0

func NewRedis(cfg RedisConfig) (*Redis, error)

func (*Redis) Broadcast added in v1.10.0

func (r *Redis) Broadcast(ctx context.Context, msg any) error

func (*Redis) ChannelHolder added in v1.10.2

func (r *Redis) ChannelHolder(ctx context.Context, channelID string) (string, error)

func (*Redis) ClaimChannel added in v1.10.0

func (r *Redis) ClaimChannel(ctx context.Context, channelID string) error

func (*Redis) ForwardRequest added in v1.10.0

func (r *Redis) ForwardRequest(ctx context.Context, channelID string, data []byte) error

func (*Redis) LookupChannel added in v1.10.0

func (r *Redis) LookupChannel(ctx context.Context, channelID string) (string, error)

func (*Redis) OnRequest added in v1.10.0

func (r *Redis) OnRequest(fn RelayRequestFn)

func (*Redis) OnResponse added in v1.10.0

func (r *Redis) OnResponse(fn RelayResponseFn)

func (*Redis) RegisterChannel added in v1.10.0

func (r *Redis) RegisterChannel(ctx context.Context, channelID, subject string) error

func (*Redis) ReleaseChannel added in v1.10.0

func (r *Redis) ReleaseChannel(ctx context.Context, channelID string) error

func (*Redis) Reply added in v1.10.0

func (r *Redis) Reply(ctx context.Context, nodeID string, data []byte) error

func (*Redis) Start added in v1.10.0

func (r *Redis) Start(ctx context.Context) error

func (*Redis) Subscribe added in v1.10.0

func (r *Redis) Subscribe(fn NotifyMsgFn)

func (*Redis) UnclaimChannel added in v1.10.0

func (r *Redis) UnclaimChannel(ctx context.Context, channelID string) error

type RedisConfig added in v1.10.0

type RedisConfig struct {
	// URL is the connection URL (e.g. redis://user:pass@host:6379/0, rediss:// for TLS).
	URL       string
	KeyPrefix string
	// HeartbeatInterval is how often this node refreshes the ownership of its channels.
	HeartbeatInterval time.Duration
	// ClusterMode uses the Redis Cluster protocol (e.g. cluster-mode-enabled managed Redis).
	ClusterMode bool
	// TLSCAFile is a PEM file of CA certificates to verify the server. Setting it enables TLS.
	TLSCAFile string
	// TLSInsecureSkipVerify disables server certificate verification. Setting it enables TLS.
	TLSInsecureSkipVerify bool
}

type Relay added in v1.10.0

type Relay interface {
	Cluster
	// RegisterChannel shares an issued channel so that its WebSocket can connect to any node.
	// The same non-empty subject can register it again; otherwise it returns ErrChannelExists
	// if the channel ID has already been issued.
	RegisterChannel(ctx context.Context, channelID, subject string) error
	// LookupChannel returns the subject of an issued channel, or ErrChannelNotFound.
	LookupChannel(ctx context.Context, channelID string) (string, error)
	// ClaimChannel marks this node as the holder of the channel's WebSocket.
	// It returns ErrChannelInUse if another node holds it.
	ClaimChannel(ctx context.Context, channelID string) error
	// UnclaimChannel gives up holding the channel but keeps it issued,
	// e.g. when the WebSocket upgrade failed after ClaimChannel.
	UnclaimChannel(ctx context.Context, channelID string) error
	// ReleaseChannel removes the channel after its WebSocket is disconnected.
	ReleaseChannel(ctx context.Context, channelID string) error
	// ChannelHolder returns the ID of the node holding the channel's WebSocket,
	// or ErrChannelNotFound if no node holds it.
	ChannelHolder(ctx context.Context, channelID string) (string, error)
	// ForwardRequest sends data to the node holding the channel's WebSocket.
	// It returns ErrChannelNotFound if no node holds it.
	ForwardRequest(ctx context.Context, channelID string, data []byte) error
	// Reply sends data back to the node that forwarded a request.
	Reply(ctx context.Context, nodeID string, data []byte) error
	// OnRequest and OnResponse register the callbacks. They must be called before Start.
	OnRequest(fn RelayRequestFn)
	OnResponse(fn RelayResponseFn)
}

Relay is a cluster whose nodes cannot reach each other directly, so channel state and all traffic between nodes go through the broker.

type RelayRequestFn added in v1.10.0

type RelayRequestFn func(from, channelID string, data []byte)

RelayRequestFn handles a request forwarded from the node `from` to the channel held by this node.

type RelayResponseFn added in v1.10.0

type RelayResponseFn func(data []byte)

RelayResponseFn handles a response replied to a request this node forwarded.

Jump to

Keyboard shortcuts

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