Documentation
¶
Index ¶
- Constants
- Variables
- type BatchOptions
- type BatchOutput
- type BatchRequest
- type Batcher
- type Change
- type Config
- type DatabaseListenerCreate
- type DatabaseListenerDictionary
- func (ld *DatabaseListenerDictionary[DB, PS, C]) Get(db DB, keys []string) (map[string]C, error)
- func (ld *DatabaseListenerDictionary[DB, PS, C]) GetIdentifier() string
- func (ld *DatabaseListenerDictionary[DB, PS, C]) Reset(ctx context.Context, db DB, keys []string) error
- func (ld *DatabaseListenerDictionary[DB, PS, C]) Subscribe(db DB, keys []string, identifier string, subscription func(change Change[C])) error
- func (ld *DatabaseListenerDictionary[DB, PS, C]) Unsubscribe(identifier string, keys []string)
- func (ld *DatabaseListenerDictionary[DB, PS, C]) Update(ctx context.Context, db DB, key string, change Change[C]) error
- type IPubSubBackend
- type ISubWorker
- type Identifiable
- type Instance
- type ListenerSubscriptions
- func (ls *ListenerSubscriptions[DB, PS, C]) Add(identifier string, subscription func(c Change[C]))
- func (ls *ListenerSubscriptions[T, PS, C]) Delete(identifier string)
- func (ls *ListenerSubscriptions[T, PS, C]) DisableQueuing(base Change[C])
- func (ls *ListenerSubscriptions[T, PS, C]) IsQueuing() bool
- func (ls *ListenerSubscriptions[T, PS, C]) OnChange(change Change[C])
- type LocalPubSub
- type LocalSubWorker
- func (w *LocalSubWorker) Close()
- func (w *LocalSubWorker) OnError(handler func(channel string, err error))
- func (w *LocalSubWorker) OnMessage(handler func(channel string, message string))
- func (w *LocalSubWorker) Subscribe(ctx context.Context, channels ...string) error
- func (w *LocalSubWorker) Unsubscribe(ctx context.Context, channels ...string) error
- type PoolConfig
- type SubPool
- func (p *SubPool[DB, T]) Close()
- func (p *SubPool[DB, PS]) OnError(handler func(channel string, err error))
- func (p *SubPool[DB, PS]) OnMessage(handler func(channel string, message string))
- func (p *SubPool[DB, PS]) Subscribe(ctx context.Context, channels ...string) error
- func (p *SubPool[DB, PS]) Unsubscribe(ctx context.Context, channels ...string) error
- type Subscription
Constants ¶
const RecommendedWantInterval = 20 * time.Second // The interval how often you should call "Want" on a subscription
const SubscriptionDuration = 60 * time.Second // The duration after which a subscription will be deleted
Variables ¶
var ( ErrChannelNotRegistered = errors.New("channel is not registered") ErrChannelAlreadyRegistered = errors.New("channel already registered by different worker") )
Standardized errors for pub/sub
Functions ¶
This section is empty.
Types ¶
type BatchOptions ¶
type BatchOutput ¶
type BatchOutput[I comparable, O any] struct { Err error Outputs map[I]O }
type BatchRequest ¶
type BatchRequest[I comparable, O any] struct { Inputs []I Collector chan BatchOutput[I, O] }
type Batcher ¶
type Batcher[I comparable, O any] struct { Options BatchOptions Collector chan BatchRequest[I, O] BatchFunc func([]I) (map[I]O, error) }
type Config ¶
type Config[DB any, PS IPubSubBackend[DB]] struct { // The backend for Hydro's pub/sub model (if not set we'll use a local backend that acts as a replacement for a dedicated pub/sub service) PubSubBackend PS }
type DatabaseListenerCreate ¶
type DatabaseListenerCreate[DB any, C Change[C]] struct { Identifier string // Identifier for the listener (REQUIRED) Get func(DB, []string) (map[string]C, error) // Get the base data from results of listeners or just with key (required) OnChange func(key string, change Change[C]) // Called when the listener receives a change (required) PoolConfig PoolConfig // Config for the pooling of subscription workers }
type DatabaseListenerDictionary ¶
type DatabaseListenerDictionary[DB any, PS IPubSubBackend[DB], C Change[C]] struct { Instance *Instance[DB, PS] // Hydro instance related Identifier string // Unique identifier for this listener dictionary // contains filtered or unexported fields }
func NewListenerDictionary ¶
func NewListenerDictionary[DB any, PS IPubSubBackend[DB], C Change[C]](instance *Instance[DB, PS], create DatabaseListenerCreate[DB, C]) *DatabaseListenerDictionary[DB, PS, C]
Helper function for initializing a new listener dictionary properly
func (*DatabaseListenerDictionary[DB, PS, C]) Get ¶
func (ld *DatabaseListenerDictionary[DB, PS, C]) Get(db DB, keys []string) (map[string]C, error)
Get the value for keys from the listener dictionary (makes sure we can add batching in the future)
func (*DatabaseListenerDictionary[DB, PS, C]) GetIdentifier ¶
func (ld *DatabaseListenerDictionary[DB, PS, C]) GetIdentifier() string
func (*DatabaseListenerDictionary[DB, PS, C]) Reset ¶
func (ld *DatabaseListenerDictionary[DB, PS, C]) Reset(ctx context.Context, db DB, keys []string) error
Reset all values for the keys back to the original value by re-getting them and pushing that update
func (*DatabaseListenerDictionary[DB, PS, C]) Subscribe ¶
func (ld *DatabaseListenerDictionary[DB, PS, C]) Subscribe(db DB, keys []string, identifier string, subscription func(change Change[C])) error
func (*DatabaseListenerDictionary[DB, PS, C]) Unsubscribe ¶
func (ld *DatabaseListenerDictionary[DB, PS, C]) Unsubscribe(identifier string, keys []string)
Remove subscriptions for an identifier
type IPubSubBackend ¶
type ISubWorker ¶
type Identifiable ¶
type Identifiable interface {
GetIdentifier() string
}
type Instance ¶
type Instance[DB any, PS IPubSubBackend[DB]] struct { // contains filtered or unexported fields }
type ListenerSubscriptions ¶
type ListenerSubscriptions[DB any, PS IPubSubBackend[DB], C Change[C]] struct { // contains filtered or unexported fields }
A manager of subscriptions to a Listener that automatically evicts them statelessly when no longer wanted.
Also aggressively caches the current return value from the listener. This is done by stacking the changes using the Stack method from the Change interface. OnSubscribe is usually pretty expensive and managing it like this makes sure we always only call it exactly once.
func NewSubs ¶
func NewSubs[DB any, PS IPubSubBackend[DB], C Change[C]](instance *Instance[DB, PS]) *ListenerSubscriptions[DB, PS, C]
Create a new manager of listener subscriptions
func (*ListenerSubscriptions[DB, PS, C]) Add ¶
func (ls *ListenerSubscriptions[DB, PS, C]) Add(identifier string, subscription func(c Change[C]))
Mark a listener subscription as wanted (identifier is a unique identifier of the subscription)
func (*ListenerSubscriptions[T, PS, C]) Delete ¶
func (ls *ListenerSubscriptions[T, PS, C]) Delete(identifier string)
Delete a subscription from the subscriptions
func (*ListenerSubscriptions[T, PS, C]) DisableQueuing ¶
func (ls *ListenerSubscriptions[T, PS, C]) DisableQueuing(base Change[C])
After DisableQueuing the subscriptions start to actually send changes when they are received, before all are queued
func (*ListenerSubscriptions[T, PS, C]) IsQueuing ¶
func (ls *ListenerSubscriptions[T, PS, C]) IsQueuing() bool
Check if the subscriptions are currently still in queuing mode
func (*ListenerSubscriptions[T, PS, C]) OnChange ¶
func (ls *ListenerSubscriptions[T, PS, C]) OnChange(change Change[C])
Handles a change and sends it to all subscribers of the listener
type LocalPubSub ¶
type LocalPubSub struct {
// contains filtered or unexported fields
}
func NewLocalPubSub ¶
func NewLocalPubSub() *LocalPubSub
func (*LocalPubSub) CreateWorker ¶
func (lpb *LocalPubSub) CreateWorker() ISubWorker
type LocalSubWorker ¶
type LocalSubWorker struct {
// contains filtered or unexported fields
}
func (*LocalSubWorker) Close ¶
func (w *LocalSubWorker) Close()
func (*LocalSubWorker) OnError ¶
func (w *LocalSubWorker) OnError(handler func(channel string, err error))
func (*LocalSubWorker) OnMessage ¶
func (w *LocalSubWorker) OnMessage(handler func(channel string, message string))
func (*LocalSubWorker) Subscribe ¶
func (w *LocalSubWorker) Subscribe(ctx context.Context, channels ...string) error
func (*LocalSubWorker) Unsubscribe ¶
func (w *LocalSubWorker) Unsubscribe(ctx context.Context, channels ...string) error
type PoolConfig ¶
type PoolConfig struct {
// How many subscriptions should, at maximum, be done by one worker (default: 100)
MaxAmountByWorker int
}
type SubPool ¶
type SubPool[DB any, PS IPubSubBackend[DB]] struct { // contains filtered or unexported fields }
func NewPubSubPool ¶
func NewPubSubPool[DB any, PS IPubSubBackend[DB]](backend PS, config PoolConfig) *SubPool[DB, PS]