Persistence#
Builder#
- class ChannelRepositoryBuilder#
Wires a channel repository and its connections, listeners and queues repositories.
Every part is stored in storage under its own namespace (
channel:,connection:,listener:,queue:), unless a dedicated storage or repository is given for it. Each method returns the builder, so calls can be chained:repository = ( ChannelRepositoryBuilder(SSEChannel, storage) .listeners(MyListener) .queues_repository(MyQueuesRepository(connection)) .build() )
Channels built by the resulting repository create their listeners and queues through it. Nothing is persisted implicitly: call the repository’s
persistwhen needed.When a part is configured more than once, the last call wins, except for error handlers, that are added.
The builder is generic in channel_class, so type checkers know which channels the repository returns.
- Parameters:
channel_class – Class of channels the repository creates, loads and persists, e.g.
SSEChannel.storage (KvStorage) – Default storage. Defaults to a new
InMemoryStorage.
- __init__(channel_class, storage=None)#
- connections(storage)#
- Parameters:
storage (KvStorage) – Dedicated storage for connections.
- Return type:
- listeners(listener_factory=<class 'eric_sse.listener.MessageQueueListener'>, storage=None)#
Uses a
KvListenerRepository.- Parameters:
listener_factory (Callable[[], MessageQueueListener]) – Called once per connection to create its listener: a listener class, or any callable returning a listener, e.g.
lambda: ProfiledListener(MyListener()). Defaults toMessageQueueListener.storage (KvStorage | None) – Dedicated storage for listeners.
- Return type:
- listeners_repository(repository)#
Uses a custom listeners repository, that also decides which listeners are created.
- Parameters:
repository (ListenerRepositoryInterface)
- Return type:
- queues(queue_factory=<class 'eric_sse.queues.InMemoryQueue'>, storage=None)#
Uses a
KvQueueRepository.- Parameters:
queue_factory (Callable[[], Queue]) – Called once per connection to create its queue. Defaults to
InMemoryQueue.storage (KvStorage | None) – Dedicated storage for queues.
- Return type:
- queues_repository(repository)#
Uses a custom queues repository, that also decides which queues are created.
- Parameters:
repository (QueueRepositoryInterface)
- Return type:
- queuing_error_handler(handler)#
Adds a handler registered on every channel created or loaded by the repository. Unlike other parts, repeated calls add handlers instead of replacing them.
- Parameters:
handler (QueuingErrorHandler)
- Return type:
- listener_error_handler(handler)#
Adds a handler registered on every channel created or loaded by the repository. Unlike other parts, repeated calls add handlers instead of replacing them.
- Parameters:
handler (ListenerErrorHandler)
- Return type:
- build()#
Returns a new channel repository. The builder can be reused.
- Return type:
Base repositories#
- class KvStorage#
-
Represents a Key Value storage engine. Provides functionalities do load, persist and find by key prefix
- abstract fetch_by_prefix(prefix)#
Search by KV prefix. An empty prefix returns all items.
Implementations must tolerate deletions of fetched keys while the result is being iterated.
- class InMemoryStorage#
Bases:
KvStorage[T]In memory implementation
- fetch_by_prefix(prefix)#
Search by KV prefix. An empty prefix returns all items.
Implementations must tolerate deletions of fetched keys while the result is being iterated.
- class PrefixedStorage#
Bases:
KvStorage[T]A namespaced view of another
KvStorage. All keys are prefixed with prefix.Allows several repositories to share a single storage instance. Prefixes of storages sharing the same instance must not be prefixes of each other.
- fetch_by_prefix(prefix)#
Search by KV prefix. An empty prefix returns all items.
Implementations must tolerate deletions of fetched keys while the result is being iterated.
- class KvListenerRepository#
Bases:
ListenerRepositoryInterface[L]Listener repository backed by a
KvStorage.Listeners are stored as they are, keyed by connection id; serialization, if needed, is up to the storage.
- Parameters:
storage
listener_factory – Called by
create()to create a listener, e.g. a listener class. Defaults toMessageQueueListener.
- __init__(storage, listener_factory=<class 'eric_sse.listener.MessageQueueListener'>)#
- create()#
Creates a new listener. It is not persisted.
- Return type:
L
- load(connection_id)#
Loads a listener given the connection id it belongs to.
- Parameters:
connection_id (str)
- Return type:
L
- class KvQueueRepository#
Bases:
QueueRepositoryInterface[Q]Queue repository backed by a
KvStorage.Queues are stored as they are, keyed by connection id; serialization, if needed, is up to the storage.
Each queue class decides what of its state is serialised. A durable queue keeps its messages in its own backend and serialises only its identity and configuration (e.g. through
__getstate__), because messages are written by the queue itself on every push and pop.InMemoryQueuecan’t be serialised: use it with a storage that keeps objects in process, such asInMemoryStorage.- Parameters:
storage
queue_factory – Called by
create()to create a queue. Defaults toInMemoryQueue.
- __init__(storage, queue_factory=<class 'eric_sse.queues.InMemoryQueue'>)#
- create()#
Creates a new queue. It is not persisted.
- Return type:
Q
- load(connection_id)#
Loads a queue given the connection id it belongs to.
- Parameters:
connection_id (str)
- Return type:
Q
- class ChannelRepository#
Bases:
ChannelRepositoryInterface[C]Persists channels of a given class, using their
to_dict()andfrom_dict()methods.Channels it builds get
connections_repositoryas connections_factory, so that new connections are created by the persistence layer.- Parameters:
storage – Storage for channels configuration.
connections_repository
channel_class – Class of channels this repository creates, loads and persists, e.g.
SSEChannel.queuing_error_handlers – Registered on every channel created or loaded, see
add_queuing_error_handler().listener_error_handlers – Registered on every channel created or loaded, see
add_listener_error_handler().
- __init__(storage, connections_repository, channel_class, queuing_error_handlers=(), listener_error_handlers=())#
- Parameters:
storage (KvStorage)
connections_repository (ConnectionRepositoryInterface)
channel_class (type[C])
queuing_error_handlers (Iterable[QueuingErrorHandler])
listener_error_handlers (Iterable[ListenerErrorHandler])
- property connections_repository: ConnectionRepositoryInterface#
Repository to be used to persist connections. It is also the connections factory injected into the channels it builds.
- create(channel_data)#
Creates a channel. It is not persisted.
- Parameters:
channel_data (dict) – Constructor parameters of channel_class, except connections_factory.
- Return type:
C
- persist(channel)#
Persists channel configuration and its connections. Raises a TypeError if channel is not exactly of channel_class.
- Parameters:
channel (C)
- class ConnectionRepository#
Bases:
ConnectionRepositoryInterfaceConcrete Connection Repository
Relies on
KvStorageabstraction for final writes of connections data, and on correspondant repositories for related objects ones.- __init__(storage, listeners_repository, queues_repository)#
- Parameters:
storage (KvStorage)
listeners_repository (ListenerRepositoryInterface)
queues_repository (QueueRepositoryInterface)
- create(listener=None)#
Creates a connection. Listener, if not provided, and queue are created by the corresponding repositories.
- Parameters:
listener (MessageQueueListener | None)
- Return type:
- static _channel_prefix(channel_id)#
Key prefix of a channel’s connections. The id is encoded so that it can’t contain the separator: otherwise channel
awould match the connections of channela:b.
- load_ids(channel_id)#
Ids of the connections managed by a given channel, without loading their listeners and queues.
- load_all(channel_id)#
Loads all connections managed by a given channel
- Parameters:
channel_id (str)
- Return type:
- load_one(channel_id, connection_id)#
Loads a connection given the connection and channel id it belongs to.
- Parameters:
- Return type:
- persist(channel_id, connection)#
Persists a connection and assign it to a channel.
- Parameters:
channel_id (str)
connection (Connection)
Interoperability#
- class QueueRepositoryInterface#
-
Generic in the class of queues it creates, loads and persists.
- abstract create()#
Creates a new queue. It is not persisted.
- Return type:
Q
- abstract load(connection_id)#
Loads a queue given the connection id it belongs to.
- Parameters:
connection_id (str)
- Return type:
Q
- class ListenerRepositoryInterface#
-
Generic in the class of listeners it creates, loads and persists.
- abstract create()#
Creates a new listener. It is not persisted.
- Return type:
L
- abstract load(connection_id)#
Loads a listener given the connection id it belongs to.
- Parameters:
connection_id (str)
- Return type:
L
- class ConnectionRepositoryInterface#
Bases:
ConnectionsFactory,ABCPersists connections, and creates them using its listeners and queues repositories.
Channel repositories inject it as connections_factory into the channels they build.
- abstract property queues_repository: QueueRepositoryInterface#
- abstract property listeners_repository: ListenerRepositoryInterface#
- abstract load_ids(channel_id)#
Ids of the connections managed by a given channel, without loading their listeners and queues.
- abstract load_all(channel_id)#
Loads all connections managed by a given channel
- Parameters:
channel_id (str)
- Return type:
- abstract load_one(channel_id, connection_id)#
Loads a connection given the connection and channel id it belongs to.
- Parameters:
- Return type:
- abstract persist(channel_id, connection)#
Persists a connection and assign it to a channel.
- Parameters:
channel_id (str)
connection (Connection)
- class ChannelRepositoryInterface#
-
Generic in the class of channels it handles.
- abstract property connections_repository: ConnectionRepositoryInterface#
Repository to be used to persist connections. It is also the connections factory injected into the channels it builds.
- abstract load_one(channel_id)#
Loads a channel given its it
- Parameters:
channel_id (str)
- Return type:
C
- abstract persist(channel)#
Persists a channel
- Parameters:
channel (C)