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, and LoginCounter keeps the failed logins of the login limiter, so that its limit holds across the processes. 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
- type Bridge
- 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)
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 ( // 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" )
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 ¶
This section is empty.
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) (*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 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.