Router

Source code: event_collector.base.router.py

This module contains definition for the base router class and the simple router.

A Router is used to dispatch an event payload to collectors. The default router is SimpleRouter and is used by the collector queue to handle events if no custom router is supplied at initialization.

class event_collector.router.Router(collectors: CollectorDict | None = None)

This is the abstract base class for a router.

collectors: CollectorDict

The router should be initialized with a dict of Collector instances (with Collector key as the dict key and the Collector instance as the dict value). The collector dict will be used to handle event routing by the handle_event method.

abstractmethod async handle_event(payload: Payload) ResultList

The handle_event method should implement logic to dispatch an event payload to one or more collectors. The return value must be a ResultList.

class event_collector.router.SimpleRouter(collectors: CollectorDict | None = None)

The SimpleRouter is the default router used by the collector queue to handle payload dispatching. This router handles the 3 delivery modes implemented by the collector queue: direct key, topics, and broadcast. The handle_event ensures that all event payloads sent to the collectors return valid Result object(s).

collectors: CollectorDict

As with the base Router, collectors should be a dict of Collector instances (CollectorDict).

async handle_event(payload: Payload) ResultList

Handle incoming event payload by sending payload to collectors using the payload’s delivery mode.

This method will return a ResultList, because, depending on the delivery mode, the event payload will be sent to one or more collectors. So it’s valid to have multiple return values.