borsa_types/
config.rs

1//! Configuration types shared across orchestrators and connectors.
2
3// no extra prelude imports
4use std::time::Duration;
5
6use crate::routing_policy::RoutingPolicy;
7use serde::{Deserialize, Serialize};
8
9/// Strategy for selecting among eligible data providers.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
11#[non_exhaustive]
12pub enum FetchStrategy {
13    /// Use priority order and fall back to the next provider on failure.
14    #[default]
15    PriorityWithFallback,
16    /// Race all eligible providers concurrently and return the first success.
17    Latency,
18}
19
20/// Strategy for merging history data from multiple providers.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
22#[non_exhaustive]
23pub enum MergeStrategy {
24    /// Fetch from all eligible providers concurrently and merge their data.
25    /// This produces the most complete dataset by backfilling gaps from lower-priority providers.
26    #[default]
27    Deep,
28    /// Iterate through providers sequentially and stop as soon as one returns a non-empty dataset.
29    /// This is more economical for API rate limits but may miss data from lower-priority providers.
30    Fallback,
31}
32
33/// Forced resampling mode for merged history series.
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
35#[non_exhaustive]
36pub enum Resampling {
37    /// Do not force resampling; preserve the effective interval unless auto-subdaily triggers.
38    #[default]
39    None,
40    /// Force resampling to daily cadence.
41    Daily,
42    /// Force resampling to weekly cadence.
43    Weekly,
44}
45
46/// Exponential backoff configuration for reconnecting streaming sessions.
47#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
48pub struct BackoffConfig {
49    /// Minimum backoff delay in milliseconds.
50    pub min_backoff_ms: u64,
51    /// Maximum backoff delay in milliseconds.
52    pub max_backoff_ms: u64,
53    /// Exponential factor to increase delay after each failure (>= 1).
54    pub factor: u32,
55    /// Random jitter percentage [0, 100] added to each delay.
56    pub jitter_percent: u8,
57}
58
59impl Default for BackoffConfig {
60    fn default() -> Self {
61        Self {
62            min_backoff_ms: 500,
63            max_backoff_ms: 30_000,
64            factor: 2,
65            jitter_percent: 20,
66        }
67    }
68}
69
70/// Global configuration for the `Borsa` orchestrator.
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct BorsaConfig {
73    /// Unified routing policy controlling provider and exchange ordering.
74    ///
75    /// - Provider rules select and order eligible connectors; unknown connector
76    ///   keys are rejected during `borsa`'s build step.
77    /// - Exchange preferences influence search de-duplication only.
78    pub routing_policy: RoutingPolicy,
79    /// Prefer adjusted history series when merging.
80    pub prefer_adjusted_history: bool,
81    /// Forced resampling mode for merged history.
82    pub resampling: Resampling,
83    /// If request interval is subdaily, resample to daily automatically.
84    pub auto_resample_subdaily_to_daily: bool,
85    /// Strategy for fetching from multiple providers.
86    pub fetch_strategy: FetchStrategy,
87    /// Strategy for merging history data from multiple providers.
88    pub merge_history_strategy: MergeStrategy,
89    /// Timeout for individual provider requests.
90    pub provider_timeout: Duration,
91    /// Optional overall request timeout for fan-out aggregations (e.g., history/search).
92    /// If set, operations that aggregate multiple provider calls are bounded by this deadline.
93    pub request_timeout: Option<Duration>,
94    /// Optional backoff configuration used by streaming.
95    pub backoff: Option<BackoffConfig>,
96}
97
98impl Default for BorsaConfig {
99    fn default() -> Self {
100        Self {
101            routing_policy: RoutingPolicy::default(),
102            prefer_adjusted_history: false,
103            resampling: Resampling::None,
104            auto_resample_subdaily_to_daily: false,
105            fetch_strategy: FetchStrategy::default(),
106            merge_history_strategy: MergeStrategy::default(),
107            provider_timeout: Duration::from_secs(5),
108            request_timeout: None,
109            backoff: None,
110        }
111    }
112}