Mongoose server Event Flow Architecture¶
Introduction¶
The event flow architecture is a core aspect of the Mongoose server framework. It defines how events are generated, routed, and processed throughout the system. This document provides a detailed explanation of the event flow architecture, including the components involved and their interactions.
Event Flow Components¶
The event flow architecture consists of the following key components:
- EventSource - Generates events from external or internal sources
- EventFlowManager - Routes events between sources and processors
- EventToQueuePublisher - Publishes events to queues
- EventQueueToEventProcessor - Consumes events from queues and forwards them to processors
- EventProcessor - Processes events according to business logic
Component Interaction Diagram¶
┌───────────────┐ ┌───────────────────────────────────────────┐
│ │ │ EventFlowManager │
│ EventSource │────▶│ │
│ │ │ ┌───────────────┐ ┌───────────────┐ │
└───────────────┘ │ │EventToQueue │───▶│EventQueue │ │
│ │Publisher │ │ │ │
│ └───────────────┘ └───────────────┘ │
│ │ │
└──────────────────────────────│────────────┘
│
▼
┌──────────────────────────────────────────┐
│ EventQueueToEventProcessor │
│ │
│ ┌───────────────┐ ┌───────────────┐ │
│ │EventToInvoke │───▶│EventProcessor │ │
│ │Strategy │ │ │ │
│ └───────────────┘ └───────────────┘ │
│ │
└──────────────────────────────────────────┘
Event Flow Process¶
The event flow process in Mongoose server follows these steps:
-
Event Generation: An EventSource generates an event, which could be from an external system, a timer, or another internal component.
-
Event Publication: The EventSource publishes the event to the EventFlowManager through the EventToQueuePublisher.
-
Event Routing: The EventFlowManager routes the event to the appropriate event queues based on subscriptions.
-
Event Consumption: The EventQueueToEventProcessor consumes events from the queue.
-
Event Processing: The EventToInvokeStrategy determines how to invoke the EventProcessor with the event, and the EventProcessor processes the event.
-
Result Handling: The result of the event processing may generate new events, which can be fed back into the system.
Subscription Model¶
Mongoose server uses a subscription model to connect event sources with event processors:
-
EventSubscriptionKey: Identifies a subscription between an event source and a processor.
-
Subscribe/Unsubscribe: Processors can subscribe to or unsubscribe from event sources.
-
Callback Types: Different callback types determine how events are processed (e.g., OnEvent, custom callbacks).
Subscription Diagram¶
┌───────────────┐ ┌───────────────┐
│ │ 1. Register │ │
│ EventSource │◀─────────────────────────│ MongooseServer│
│ │ │ │
└───────┬───────┘ └───────────────┘
│ ▲
│ │
│ │
│ │
│ ┌───────┴───────┐
│ │ │
│ 2. Subscribe │ EventProcessor│
└─────────────────────────────────▶│ │
└───────────────┘
Event Types and Wrapping¶
Mongoose server supports different event wrapping strategies:
- SUBSCRIPTION_NOWRAP: Events are passed directly to subscribers without wrapping.
- SUBSCRIPTION_NAMED_EVENT: Events are wrapped with source information before passing to subscribers.
- BROADCAST_NOWRAP: Events are broadcast to all subscribers without wrapping.
- BROADCAST_NAMED_EVENT: Events are wrapped and broadcast to all subscribers.
Slow Consumer Handling¶
Mongoose server provides strategies for handling slow consumers:
- DISCONNECT: Disconnect slow consumers to prevent system slowdown.
- EXIT_PROCESS: Exit the process if a consumer is too slow.
- BACKOFF: Implement backoff strategies to give slow consumers time to catch up.
Queue Implementation¶
The event queues in Mongoose server are implemented using:
- ManyToOneConcurrentArrayQueue: For multiple producers and a single consumer.
- OneToOneConcurrentArrayQueue: For a single producer and a single consumer.
These queue implementations provide thread-safe communication between components running in different threads.
Error Handling¶
Error handling in the event flow follows these patterns:
- Global Error Handler: Catches and handles errors at the system level.
- Retry Mechanisms: Implements exponential backoff for retrying failed operations.
- Error Events: Generates error events that can be processed by error handlers.
Conclusion¶
The event flow architecture of Mongoose server provides a flexible and efficient mechanism for routing events between sources and processors. Its subscription-based model allows for dynamic configuration of event flows, while its queue-based implementation ensures thread-safe communication between components.