use std::time::Duration;
use crate::{Category, Environment, SensitiveString, ServerClock, Timestamp};
pub const DEFAULT_PING_INTERVAL: Duration = Duration::from_secs(20);
pub const DEFAULT_PONG_TIMEOUT: Duration = Duration::from_secs(10);
pub const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
pub const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
pub const DEFAULT_AUTH_RECV_WINDOW: Timestamp = 5_000;
#[derive(Debug, Clone)]
pub struct Config {
pub url: String,
pub command_queue_size: usize,
pub event_queue_size: usize,
pub max_reconnect_attempts: u32,
pub reconnect_base_delay: Duration,
pub reconnect_max_delay: Duration,
pub close_timeout: Duration,
pub connect_timeout: Duration,
pub ping_interval: Option<Duration>,
pub pong_timeout: Duration,
pub api_key: Option<SensitiveString>,
pub api_secret: Option<SensitiveString>,
pub auth_recv_window: Timestamp,
pub server_clock: ServerClock,
pub request_timeout: Duration,
}
impl Default for Config {
fn default() -> Self {
Self {
url: String::new(),
command_queue_size: 64,
event_queue_size: 256,
max_reconnect_attempts: 5,
reconnect_base_delay: Duration::from_millis(500),
reconnect_max_delay: Duration::from_secs(30),
close_timeout: Duration::from_secs(5),
connect_timeout: DEFAULT_CONNECT_TIMEOUT,
ping_interval: Some(DEFAULT_PING_INTERVAL),
pong_timeout: DEFAULT_PONG_TIMEOUT,
api_key: None,
api_secret: None,
auth_recv_window: DEFAULT_AUTH_RECV_WINDOW,
server_clock: ServerClock::new(),
request_timeout: DEFAULT_REQUEST_TIMEOUT,
}
}
}
impl Config {
pub fn new(url: impl Into<String>) -> Self {
Self {
url: url.into(),
..Default::default()
}
}
pub fn public(env: Environment, category: Category) -> Self {
Self::new(env.public_stream_url(category))
}
pub fn trade(env: Environment) -> Result<Self, crate::ws::Error> {
env.trade_stream_url().map(Self::new).ok_or_else(|| {
crate::ws::Error::InvalidUrl(format!("{env:?} has no order entry stream"))
})
}
pub fn private(env: Environment) -> Self {
Self::new(env.private_stream_url())
}
pub fn command_queue_size(mut self, n: usize) -> Self {
self.command_queue_size = n;
self
}
pub fn event_queue_size(mut self, n: usize) -> Self {
self.event_queue_size = n;
self
}
pub fn max_reconnect_attempts(mut self, n: u32) -> Self {
self.max_reconnect_attempts = n;
self
}
pub fn reconnect_base_delay(mut self, d: Duration) -> Self {
self.reconnect_base_delay = d;
self
}
pub fn reconnect_max_delay(mut self, d: Duration) -> Self {
self.reconnect_max_delay = d;
self
}
pub fn close_timeout(mut self, d: Duration) -> Self {
self.close_timeout = d;
self
}
pub fn connect_timeout(mut self, d: Duration) -> Self {
self.connect_timeout = d;
self
}
pub fn ping_interval(mut self, d: Option<Duration>) -> Self {
self.ping_interval = d;
self
}
pub fn pong_timeout(mut self, d: Duration) -> Self {
self.pong_timeout = d;
self
}
pub fn credentials(
mut self,
api_key: impl Into<SensitiveString>,
api_secret: impl Into<SensitiveString>,
) -> Self {
self.api_key = Some(api_key.into());
self.api_secret = Some(api_secret.into());
self
}
pub fn auth_recv_window(mut self, recv_window: Timestamp) -> Self {
self.auth_recv_window = recv_window;
self
}
pub fn request_timeout(mut self, d: Duration) -> Self {
self.request_timeout = d;
self
}
pub fn server_clock(mut self, clock: ServerClock) -> Self {
self.server_clock = clock;
self
}
}