Documentation
¶
Index ¶
- Variables
- func Config(nodeName string, port int, myIP string) *memberlist.Config
- func DecodeNotifyMsg[T any](msg *NotifyMsg) (*T, error)
- type AuthMessage
- type Cluster
- type Memberlist
- func (m *Memberlist) ActiveNodes() []*memberlist.Node
- func (m *Memberlist) ActiveNodesWithoutSelf() []*memberlist.Node
- func (m *Memberlist) Broadcast(_ context.Context, msg any) error
- func (m *Memberlist) MyNodeName() string
- func (m *Memberlist) Peers(_ context.Context) ([]string, error)
- func (m *Memberlist) Start(ctx context.Context) error
- func (m *Memberlist) Subscribe(fn NotifyMsgFn)
- type MemberlistConfig
- type NotifyMsg
- type NotifyMsgFn
- type NotifyMsgType
- type PeerCluster
- type Redis
- func (r *Redis) Broadcast(ctx context.Context, msg any) error
- func (r *Redis) ChannelHolder(ctx context.Context, channelID string) (string, error)
- func (r *Redis) ClaimChannel(ctx context.Context, channelID string) error
- func (r *Redis) ForwardRequest(ctx context.Context, channelID string, data []byte) error
- func (r *Redis) LookupChannel(ctx context.Context, channelID string) (string, error)
- func (r *Redis) OnRequest(fn RelayRequestFn)
- func (r *Redis) OnResponse(fn RelayResponseFn)
- func (r *Redis) RegisterChannel(ctx context.Context, channelID, subject string) error
- func (r *Redis) ReleaseChannel(ctx context.Context, channelID string) error
- func (r *Redis) Reply(ctx context.Context, nodeID string, data []byte) error
- func (r *Redis) Start(ctx context.Context) error
- func (r *Redis) Subscribe(fn NotifyMsgFn)
- func (r *Redis) UnclaimChannel(ctx context.Context, channelID string) error
- type RedisConfig
- type Relay
- type RelayRequestFn
- type RelayResponseFn
Constants ¶
This section is empty.
Variables ¶
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 DecodeNotifyMsg ¶ added in v1.4.0
Types ¶
type AuthMessage ¶ added in v1.4.0
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) 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) ChannelHolder ¶ added in v1.10.2
func (*Redis) ClaimChannel ¶ added in v1.10.0
func (*Redis) ForwardRequest ¶ added in v1.10.0
func (*Redis) LookupChannel ¶ added in v1.10.0
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 (*Redis) ReleaseChannel ¶ added in v1.10.0
func (*Redis) Subscribe ¶ added in v1.10.0
func (r *Redis) Subscribe(fn NotifyMsgFn)
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
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.