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 persist when 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)#
Parameters:
channels(storage)#
Parameters:

storage (KvStorage) – Dedicated storage for channels.

Return type:

ChannelRepositoryBuilder[C]

connections(storage)#
Parameters:

storage (KvStorage) – Dedicated storage for connections.

Return type:

ChannelRepositoryBuilder[C]

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 to MessageQueueListener.

  • storage (KvStorage | None) – Dedicated storage for listeners.

Return type:

ChannelRepositoryBuilder[C]

listeners_repository(repository)#

Uses a custom listeners repository, that also decides which listeners are created.

Parameters:

repository (ListenerRepositoryInterface)

Return type:

ChannelRepositoryBuilder[C]

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:

ChannelRepositoryBuilder[C]

queues_repository(repository)#

Uses a custom queues repository, that also decides which queues are created.

Parameters:

repository (QueueRepositoryInterface)

Return type:

ChannelRepositoryBuilder[C]

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:

ChannelRepositoryBuilder[C]

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:

ChannelRepositoryBuilder[C]

build()#

Returns a new channel repository. The builder can be reused.

Return type:

ChannelRepository[C]

Base repositories#

class KvStorage#

Bases: ABC, Generic[T]

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.

Parameters:

prefix (str)

Return type:

Iterable[T]

abstract upsert(key, value)#

Updates or inserts a value given its corresponding key

Parameters:
  • key (str)

  • value (T)

abstract fetch_one(key)#

Return value correspondant to key

Parameters:

key (str)

Return type:

T

abstract delete(key)#

Idempotent deletion. Do not throw an error on invalid key

Parameters:

key (str)

class InMemoryStorage#

Bases: KvStorage[T]

In memory implementation

__init__(items=None)#
Parameters:

items (dict[str, T] | None)

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.

Parameters:

prefix (str)

Return type:

Iterable[T]

upsert(key, value)#

Updates or inserts a value given its corresponding key

Parameters:
  • key (str)

  • value (T)

fetch_one(key)#

Return value correspondant to key

Parameters:

key (str)

Return type:

T

delete(key)#

Idempotent deletion. Do not throw an error on invalid key

Parameters:

key (str)

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.

__init__(storage, prefix)#
Parameters:
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.

Parameters:

prefix (str)

Return type:

Iterable[T]

upsert(key, value)#

Updates or inserts a value given its corresponding key

Parameters:
  • key (str)

  • value (T)

fetch_one(key)#

Return value correspondant to key

Parameters:

key (str)

Return type:

T

delete(key)#

Idempotent deletion. Do not throw an error on invalid key

Parameters:

key (str)

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:
__init__(storage, listener_factory=<class 'eric_sse.listener.MessageQueueListener'>)#
Parameters:
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

persist(connection_id, listener)#

Persists listener and assign to connection.

Parameters:
  • connection_id (str)

  • listener (L)

delete(connection_id)#

Deleted a listener given the connection id it belongs to.

Parameters:

connection_id (str)

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. InMemoryQueue can’t be serialised: use it with a storage that keeps objects in process, such as InMemoryStorage.

Parameters:
__init__(storage, queue_factory=<class 'eric_sse.queues.InMemoryQueue'>)#
Parameters:
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

persist(connection_id, queue)#

Persists queue and assign to connection.

Parameters:
  • connection_id (str)

  • queue (Q)

delete(connection_id)#

Deletes a queue given the connection id it belongs to.

Parameters:

connection_id (str)

class ChannelRepository#

Bases: ChannelRepositoryInterface[C]

Persists channels of a given class, using their to_dict() and from_dict() methods.

Channels it builds get connections_repository as 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:
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

load_all()#

Loads all channels

Return type:

Iterable[C]

load_one(channel_id)#

Loads a channel given its it

Parameters:

channel_id (str)

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)

delete(channel_id)#

Deletes channel configuration and its connections. Idempotent.

Parameters:

channel_id (str)

class ConnectionRepository#

Bases: ConnectionRepositoryInterface

Concrete Connection Repository

Relies on KvStorage abstraction for final writes of connections data, and on correspondant repositories for related objects ones.

__init__(storage, listeners_repository, queues_repository)#
Parameters:
create(listener=None)#

Creates a connection. Listener, if not provided, and queue are created by the corresponding repositories.

Parameters:

listener (MessageQueueListener | None)

Return type:

Connection

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 a would match the connections of channel a:b.

Parameters:

channel_id (str)

Return type:

str

load_ids(channel_id)#

Ids of the connections managed by a given channel, without loading their listeners and queues.

Parameters:

channel_id (str)

Return type:

Iterable[str]

load_all(channel_id)#

Loads all connections managed by a given channel

Parameters:

channel_id (str)

Return type:

Iterable[Connection]

load_one(channel_id, connection_id)#

Loads a connection given the connection and channel id it belongs to.

Parameters:
  • channel_id (str)

  • connection_id (str)

Return type:

Connection

persist(channel_id, connection)#

Persists a connection and assign it to a channel.

Parameters:
delete(channel_id, connection_id)#

Deletes a connection given the connection and channel id it belongs to.

Parameters:
  • channel_id (str)

  • connection_id (str)

Interoperability#

class QueueRepositoryInterface#

Bases: ABC, Generic[Q]

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

abstract persist(connection_id, queue)#

Persists queue and assign to connection.

Parameters:
  • connection_id (str)

  • queue (Q)

abstract delete(connection_id)#

Deletes a queue given the connection id it belongs to.

Parameters:

connection_id (str)

class ListenerRepositoryInterface#

Bases: ABC, Generic[L]

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

abstract persist(connection_id, listener)#

Persists listener and assign to connection.

Parameters:
  • connection_id (str)

  • listener (L)

abstract delete(connection_id)#

Deleted a listener given the connection id it belongs to.

Parameters:

connection_id (str)

class ConnectionRepositoryInterface#

Bases: ConnectionsFactory, ABC

Persists 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.

Parameters:

channel_id (str)

Return type:

Iterable[str]

abstract load_all(channel_id)#

Loads all connections managed by a given channel

Parameters:

channel_id (str)

Return type:

Iterable[Connection]

abstract load_one(channel_id, connection_id)#

Loads a connection given the connection and channel id it belongs to.

Parameters:
  • channel_id (str)

  • connection_id (str)

Return type:

Connection

abstract persist(channel_id, connection)#

Persists a connection and assign it to a channel.

Parameters:
abstract delete(channel_id, connection_id)#

Deletes a connection given the connection and channel id it belongs to.

Parameters:
  • channel_id (str)

  • connection_id (str)

class ChannelRepositoryInterface#

Bases: ABC, Generic[C]

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_all()#

Loads all channels

Return type:

Iterable[C]

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)

abstract delete(channel_id)#

Deletes a channel given its it

Parameters:

channel_id (str)

abstract create(channel_data)#

Creates a new channel and configures it depending on channel_data.

Parameters:

channel_data (dict)

Return type:

C