Documentation
¶
Overview ¶
Package cluster is what several Clowder processes share through Redis (SPEC D41, §8.1). Bridge carries the events of the hub of one process to the hubs of the others, LoginCounter keeps the failed logins of the login limiter, so that its limit holds across the processes, and Presence keeps who is online. It is the only package that imports go-redis; the rest of Clowder sees the Redis client through the small interfaces of this package and of the packages that use it.
Index ¶
- Constants
- func CallsChannel(process string) string
- func ProcessKey(process string) string
- type Bridge
- type BridgeMetrics
- type BridgeOption
- type CallLink
- func (l *CallLink) Abandon() error
- func (l *CallLink) Alive(ctx context.Context, process string) (bool, error)
- func (l *CallLink) Announce(ctx context.Context, addr string) error
- func (l *CallLink) Close() error
- func (l *CallLink) ID() string
- func (l *CallLink) Messages() <-chan calls.Envelope
- func (l *CallLink) Send(ctx context.Context, to string, env calls.Envelope) error
- type CallLinkConfig
- type LoginCounter
- func (c *LoginCounter) Add(ctx context.Context, ns, key string, now time.Time, window time.Duration, ...) error
- func (c *LoginCounter) Clear(ctx context.Context, ns, key string) error
- func (c *LoginCounter) Close() error
- func (c *LoginCounter) Count(ctx context.Context, ns, key string, now time.Time, window time.Duration) (int, error)
- type Presence
Constants ¶
const ( // TagOriginFailed is the tag of the failure to read random bytes for the id of the process. TagOriginFailed = "OriginFailed" // TagEventEncodeFailed is the tag of an event that could not be encoded for Redis. TagEventEncodeFailed = "EventEncodeFailed" )
const ( // CallsChannelPrefix starts the Redis channel that carries the signalling // messages addressed to one process: clowder:calls:<process id> (SPEC D65). CallsChannelPrefix = "clowder:calls:" // ProcessKeyPrefix starts the key with which a process announces that it // runs: clowder:process:<process id>, whose value is the media address of // the process (SPEC D65). ProcessKeyPrefix = "clowder:process:" // ProcessKeyTTL is how long the key of a process lives in Redis without a // refresh. A process that dies disappears from Redis after it, and the // calls it held are ended (SPEC D65). ProcessKeyTTL = 15 * time.Second // ProcessKeyRefresh is how often a process refreshes its key. ProcessKeyRefresh = 5 * time.Second )
const ( // TagCallEnvelopeEncodeFailed is the tag of a call message that could not // be encoded for Redis. TagCallEnvelopeEncodeFailed = "CallEnvelopeEncodeFailed" // TagCallEnvelopeMalformed is the tag of a message of a calls channel that // is not a valid envelope. TagCallEnvelopeMalformed = "CallEnvelopeMalformed" // TagCallNobodyListening is the tag of a call message sent to a process // whose channel has no subscriber: the process is not running. TagCallNobodyListening = "CallNobodyListening" // TagRedisProcessFailed is the tag of a command that writes, reads or // removes the key of a process in Redis; the metadata "op" names the // operation (set, exists or delete). TagRedisProcessFailed = "RedisProcessFailed" )
const ( // TagRedisURLUnparseable is the tag of a Redis URL that go-redis cannot parse. TagRedisURLUnparseable = "RedisURLUnparseable" // TagRedisPingFailed is the tag of a Redis server that does not answer the first PING. TagRedisPingFailed = "RedisPingFailed" // TagRedisSubscribeFailed is the tag of the failure to subscribe to the events channel. TagRedisSubscribeFailed = "RedisSubscribeFailed" // TagRedisPublishFailed is the tag of the failure to publish an event to Redis. TagRedisPublishFailed = "RedisPublishFailed" // TagRedisReceiveFailed is the tag of the failure to receive from the subscription, for example when the connection broke. TagRedisReceiveFailed = "RedisReceiveFailed" // TagRedisCloseFailed is the tag of the failure to close the Redis client or its subscription. TagRedisCloseFailed = "RedisCloseFailed" // TagRedisCountFailed is the tag of the failure to count the failed logins of a key in Redis. TagRedisCountFailed = "RedisCountFailed" // TagRedisAddFailed is the tag of the failure to record a failed login in Redis. TagRedisAddFailed = "RedisAddFailed" // TagRedisClearFailed is the tag of the failure to forget the failed logins of a key in Redis. TagRedisClearFailed = "RedisClearFailed" // TagRedisPresenceFailed is the tag of the failure of a command that records or reads who is online in Redis; the metadata "op" names the operation (add, remove, refresh, drop or online). TagRedisPresenceFailed = "RedisPresenceFailed" )
const EventsChannel = "clowder:events"
EventsChannel is the Redis channel that carries the live events of every process (SPEC D41).
const TagEventMalformed = "EventMalformed"
TagEventMalformed is the tag of a message of the Redis channel that is not a valid event.
Variables ¶
This section is empty.
Functions ¶
func CallsChannel ¶ added in v1.2.0
CallsChannel returns the Redis channel of the call messages addressed to the process.
func ProcessKey ¶ added in v1.2.0
ProcessKey returns the Redis key with which the process announces itself.
Types ¶
type Bridge ¶
type Bridge struct {
// contains filtered or unexported fields
}
Bridge carries the events of the hub of this process to the hubs of the other processes that share its Redis, and theirs to this one (SPEC D41). It is the Publisher of every service when Clowder runs on several processes. Make it with NewBridge and stop it with Close. It is safe for concurrent use.
Publish hands the event to the local hub at once and queues it for Redis. A sender goroutine publishes the queue to EventsChannel; a receiver goroutine reads the channel and publishes the events of other processes to the local hub. When the connection to Redis broke and came back, the receiver closes every subscription of the local hub as dropped (Hub.DropAll), because events may have been missed.
func NewBridge ¶
func NewBridge(ctx context.Context, redisURL string, hub *live.Hub, logger *slog.Logger, opts ...BridgeOption) (*Bridge, error)
NewBridge connects to Redis at redisURL (redis:// or rediss://), checks the connection with a PING, subscribes to EventsChannel and starts the sender and the receiver. ctx bounds the connecting only; the goroutines run until Close. The error never holds the password of the URL.
func (*Bridge) Close ¶
Close stops the sender (after it tried to send what is queued, for a second at most) and the receiver, waits for both to end and closes the connection to Redis. It is safe to call more than once and from several goroutines; calls after the first return nil. Call it before closing the hub.
func (*Bridge) Lost ¶
Lost returns the number of events that did not reach Redis since the bridge started: the queue was full or the publish failed. Other processes missed them.
func (*Bridge) Publish ¶
Publish delivers the event to the local hub at once and queues it for the other processes. It never blocks: when the queue to Redis is full the event is not sent to Redis, it is counted (Lost) and logged, and the local hub still has it. After Close it only publishes to the local hub.
type BridgeMetrics ¶ added in v1.2.0
type BridgeMetrics struct {
// Reconnects counts the times the connection to Redis came back after a
// break.
Reconnects *metrics.Counter
// Published counts the events the bridge sent to Redis.
Published *metrics.Counter
// Received counts the events of other processes the bridge delivered to
// the local hub.
Received *metrics.Counter
}
BridgeMetrics are the counters of a Bridge (SPEC D60). A nil counter counts nothing.
type BridgeOption ¶ added in v1.2.0
type BridgeOption func(*bridgeOptions)
BridgeOption changes how NewBridge makes the bridge.
func WithMetrics ¶ added in v1.2.0
func WithMetrics(m BridgeMetrics) BridgeOption
WithMetrics makes the bridge count its work in m.
type CallLink ¶ added in v1.2.0
type CallLink struct {
// contains filtered or unexported fields
}
CallLink is what the calls of a process share with the other processes through Redis (SPEC D65). It implements calls.Relay, which carries the signalling of a participant whose socket reached a process other than the home of the call, and the service of the calls asks it whether the home of a call is alive (Alive). A process announces itself with Announce, which keeps the key clowder:process:<id> alive from a goroutine until Close. Make it with NewCallLink and stop it with Close. It is safe for concurrent use.
No media goes through Redis: only the JSON of the signalling messages does, on the channel clowder:calls:<process id> of the process they are for.
func NewCallLink ¶ added in v1.2.0
func NewCallLink(ctx context.Context, cfg CallLinkConfig) (*CallLink, error)
NewCallLink connects to Redis, checks the connection with a PING and subscribes to the channel of the process. ctx bounds the connecting only; the goroutines run until Close. The error never holds the password of the URL. It fails with TagRedisURLUnparseable, TagRedisPingFailed and TagRedisSubscribeFailed.
func (*CallLink) Abandon ¶ added in v1.2.0
Abandon stops the link as a process that crashed leaves it: the goroutines and the connection end, and the key is left to expire after its time to live. Only a test of that expiry calls it.
func (*CallLink) Alive ¶ added in v1.2.0
Alive reports whether the process announced itself and its key has not expired. It fails with TagRedisProcessFailed when Redis cannot be read.
func (*CallLink) Announce ¶ added in v1.2.0
Announce writes the key of the process, with the media address as its value, and starts the goroutine that refreshes it until Close (SPEC D65). The other processes then see the process as alive. A second call does nothing. It fails with TagRedisProcessFailed.
func (*CallLink) Close ¶ added in v1.2.0
Close stops the receiver and the refresher, removes the key of the process (so the others see it gone at once) and closes the connection to Redis. It is safe to call more than once; calls after the first return its result.
func (*CallLink) Messages ¶ added in v1.2.0
Messages implements calls.Relay: the envelopes addressed to this process. The channel is closed by Close.
func (*CallLink) Send ¶ added in v1.2.0
Send implements calls.Relay: it publishes the envelope on the channel of the process to. It fails with TagCallEnvelopeEncodeFailed, with TagRedisPublishFailed, and with TagCallNobodyListening when no process is subscribed there, which is how the sender learns that the other process is gone.
type CallLinkConfig ¶ added in v1.2.0
type CallLinkConfig struct {
// RedisURL is the redis:// or rediss:// URL of the server.
RedisURL string
// ProcessID is the ID of this process (16 hexadecimal characters), the
// home that the calls it starts record.
ProcessID string
// KeyTTL and KeyRefresh set the life of the key of the process and how often
// it is refreshed; zero takes ProcessKeyTTL and ProcessKeyRefresh. Tests
// make them short.
KeyTTL time.Duration
KeyRefresh time.Duration
// Logger receives the failures; nil logs nothing.
Logger *slog.Logger
}
CallLinkConfig holds the settings of NewCallLink.
type LoginCounter ¶
type LoginCounter struct {
// contains filtered or unexported fields
}
LoginCounter counts the failed logins in Redis, so that the limit of SPEC §7.1 holds across all the processes that share it (SPEC D41, §8.1). It implements the interface app.FailureCounter. Each key is a sorted set whose scores are the times of the failures; the key expires when the window has passed. The times come from the caller, not from Redis, so that a test can use a fake clock. A LoginCounter is safe for concurrent use. Make it with NewLoginCounter and release it with Close.
func NewLoginCounter ¶
func NewLoginCounter(ctx context.Context, redisURL string) (*LoginCounter, error)
NewLoginCounter connects to Redis at redisURL (redis:// or rediss://) with a client of its own and checks the connection with a PING. ctx bounds the connecting only. The error never holds the password of the URL. It fails with TagRedisURLUnparseable and TagRedisPingFailed.
func (*LoginCounter) Add ¶
func (c *LoginCounter) Add(ctx context.Context, ns, key string, now time.Time, window time.Duration, keep int) error
Add records a failure of key in ns at now. The failures older than window are forgotten, and so are all but the latest keep ones, so that a key never holds more than keep entries. The key expires after window.
func (*LoginCounter) Clear ¶
func (c *LoginCounter) Clear(ctx context.Context, ns, key string) error
Clear forgets every failure of key in ns.
func (*LoginCounter) Close ¶
func (c *LoginCounter) Close() error
Close closes the connection to Redis.
type Presence ¶ added in v1.2.0
type Presence struct {
// contains filtered or unexported fields
}
Presence says who is online across all the processes that share one Redis (SPEC §7.15). It implements live.Presence. Each process keeps in Redis the set of its online users, with a time to live, and refreshes it from a goroutine every few seconds, so that a process that dies disappears by itself after the time to live; a heartbeat in a sorted set, taken from the clock of Redis, tells which processes are alive. Make it with NewPresence and stop it with Close. It is safe for concurrent use.
Connect and Disconnect never fail and never block longer than a couple of seconds: when Redis does not answer, the failure is logged and the next refresh repairs the set. Online fails only when Redis cannot be read, and then still returns the users known to be online in this process.
func NewPresence ¶ added in v1.2.0
NewPresence connects to Redis at redisURL (redis:// or rediss://), checks the connection with a PING and starts the goroutine that refreshes the users of this process. ctx bounds the connecting only; the goroutine runs until Close. The error never holds the password of the URL. It fails with TagRedisURLUnparseable and TagRedisPingFailed.
func (*Presence) Close ¶ added in v1.2.0
Close stops the refresher and waits for it, deletes the data of this process from Redis at once (a process that dies without Close disappears after the time to live instead) and closes the connection to Redis. It is safe to call more than once and from several goroutines; calls after the first return nil. After Close, Connect, Disconnect and Online work on this process alone and do not touch Redis.
func (*Presence) Connect ¶ added in v1.2.0
Connect implements live.Presence. The first connection of a user in this process puts the user in Redis.
func (*Presence) Disconnect ¶ added in v1.2.0
Disconnect implements live.Presence. The last connection of a user in this process takes the user out of Redis.
func (*Presence) Online ¶ added in v1.2.0
Online implements live.Presence. The users with a connection in this process are online without asking Redis; the others are looked up in the sets of all the live processes. When Redis cannot be read, Online returns the users known to be online here together with the error.