1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tracing::{error, info, warn};
use crate::config::model::VenueConfig;
use crate::domain::{ConnectionState, MarketDataEnvelope, MarketDataType, Timestamp};
use super::health_monitor::HealthMonitor;
use super::ports::{Subscription, VenueAdapter, VenueError};
use super::sequence_tracker::SequenceTracker;
/// Orchestrates venue connections and feeds normalized events to the publisher pipeline.
pub struct SubscriptionManager {
health_monitor: Arc<HealthMonitor>,
sequence_tracker: Arc<SequenceTracker>,
}
impl SubscriptionManager {
/// Creates a new `SubscriptionManager`.
#[must_use]
pub fn new(health_monitor: Arc<HealthMonitor>, sequence_tracker: Arc<SequenceTracker>) -> Self {
Self {
health_monitor,
sequence_tracker,
}
}
/// Spawns an async task for a venue adapter.
///
/// The task connects, subscribes, and forwards normalized events through the channel.
/// Handles reconnection with exponential backoff.
pub fn spawn_venue_task(
&self,
mut adapter: Box<dyn VenueAdapter>,
venue_config: VenueConfig,
tx: mpsc::Sender<MarketDataEnvelope>,
mut shutdown: tokio::sync::watch::Receiver<bool>,
) -> tokio::task::JoinHandle<()> {
let health_monitor = Arc::clone(&self.health_monitor);
let sequence_tracker = Arc::clone(&self.sequence_tracker);
tokio::spawn(async move {
let venue_id = adapter.venue_id().clone();
let subscriptions = build_subscriptions(&venue_config);
let mut backoff_ms = venue_config.connection.reconnect_delay_ms;
let max_backoff_ms = venue_config.connection.max_reconnect_delay_ms;
let max_attempts = venue_config.connection.max_reconnect_attempts;
let mut attempt: u64 = 0;
loop {
// Check shutdown before connecting.
if *shutdown.borrow() {
info!(venue = %venue_id, "shutdown signal received, stopping venue task");
break;
}
// Connect
info!(venue = %venue_id, attempt = attempt, "connecting to venue");
health_monitor.set_venue_state(&venue_id, ConnectionState::Reconnecting);
match adapter.connect().await {
Ok(()) => {
info!(venue = %venue_id, "connected to venue");
health_monitor.set_venue_state(&venue_id, ConnectionState::Connected);
backoff_ms = venue_config.connection.reconnect_delay_ms;
attempt = 0;
}
Err(e) => {
error!(venue = %venue_id, error = %e, "connection failed");
health_monitor.set_venue_state(&venue_id, ConnectionState::Disconnected);
if should_stop(max_attempts, attempt) {
error!(venue = %venue_id, "max reconnect attempts reached");
break;
}
backoff_ms =
wait_with_backoff(backoff_ms, max_backoff_ms, &mut shutdown).await;
attempt = attempt.saturating_add(1);
continue;
}
}
// Subscribe
if let Err(e) = adapter.subscribe(&subscriptions).await {
error!(venue = %venue_id, error = %e, "subscribe failed");
let _ = adapter.disconnect().await;
backoff_ms = wait_with_backoff(backoff_ms, max_backoff_ms, &mut shutdown).await;
attempt = attempt.saturating_add(1);
continue;
}
// Read loop
loop {
tokio::select! {
biased;
_ = shutdown.changed() => {
if *shutdown.borrow() {
info!(venue = %venue_id, "shutdown during read loop");
let _ = adapter.disconnect().await;
return;
}
}
result = adapter.next_events() => {
match result {
Ok(mut events) => {
for event in &mut events {
event.received_at = Timestamp::now();
match sequence_tracker.next_sequence(
&event.venue,
&event.instrument,
event.data_type,
) {
Ok(seq) => event.sequence = seq,
Err(e) => {
error!(
venue = %venue_id,
error = %e,
"sequence assignment failed"
);
continue;
}
}
}
for event in events {
if tx.send(event).await.is_err() {
warn!(venue = %venue_id, "publisher channel closed");
let _ = adapter.disconnect().await;
return;
}
}
}
Err(VenueError::CircuitBreakerOpen { .. }) => {
warn!(venue = %venue_id, "circuit breaker open");
health_monitor.set_venue_state(
&venue_id,
ConnectionState::CircuitOpen,
);
let _ = adapter.disconnect().await;
break;
}
Err(e) => {
error!(venue = %venue_id, error = %e, "receive failed");
health_monitor.set_venue_state(
&venue_id,
ConnectionState::Disconnected,
);
let _ = adapter.disconnect().await;
break;
}
}
}
}
}
// Back to reconnect loop
if *shutdown.borrow() {
break;
}
attempt = attempt.saturating_add(1);
if should_stop(max_attempts, attempt) {
error!(venue = %venue_id, "max reconnect attempts reached");
break;
}
backoff_ms = wait_with_backoff(backoff_ms, max_backoff_ms, &mut shutdown).await;
}
})
}
}
/// Builds subscription requests from venue config.
#[must_use]
fn build_subscriptions(config: &VenueConfig) -> Vec<Subscription> {
config
.subscriptions
.iter()
.map(|sub| {
let data_types = sub
.data_types
.iter()
.filter_map(|dt| MarketDataType::from_str_config(dt).ok())
.collect();
Subscription {
instrument: sub.instrument.clone(),
canonical_symbol: sub.canonical_symbol.clone(),
data_types,
}
})
.collect()
}
/// Returns whether we should stop reconnecting.
#[must_use]
#[inline]
fn should_stop(max_attempts: u64, current: u64) -> bool {
max_attempts > 0 && current >= max_attempts
}
/// Waits with exponential backoff, returning the next backoff value.
/// Returns early if shutdown is signaled.
async fn wait_with_backoff(
current_ms: u64,
max_ms: u64,
shutdown: &mut tokio::sync::watch::Receiver<bool>,
) -> u64 {
let delay = Duration::from_millis(current_ms);
tokio::select! {
biased;
_ = shutdown.changed() => {}
_ = tokio::time::sleep(delay) => {}
}
// Double backoff, capped at max
std::cmp::min(current_ms.saturating_mul(2), max_ms)
}