#![allow(dead_code)]
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use futures::{SinkExt, StreamExt};
use std::sync::LazyLock;
use tokio::sync::{mpsc, watch};
use tokio_tungstenite::tungstenite::Message;
use crate::Value;
pub struct ClientState {
pub url: String,
gen_rx: watch::Receiver<u64>,
}
struct ClientCore {
handle: Arc<ClientState>,
outgoing: mpsc::UnboundedSender<Message>,
pending_rx: Option<mpsc::UnboundedReceiver<Message>>,
connecting: bool,
incoming: VecDeque<Value>,
resolved: HashMap<String, Value>,
rejections: HashMap<String, Value>,
subscriptions: HashMap<String, Value>,
futures: HashSet<String>,
flights: HashMap<String, FlightSlot>,
connected: bool,
closed: bool,
last_pong_ms: i64,
mock: bool,
mock_sent: Vec<Value>,
ws_test_completed: bool,
gen_tx: watch::Sender<u64>,
}
#[derive(Clone)]
struct FlightSlot {
open: bool,
settled: Option<Result<Value, Value>>,
}
#[derive(Default)]
struct WsClientStore {
clients: HashMap<String, ClientCore>,
}
type ClientJob = Box<dyn FnOnce(&mut WsClientStore) + Send + 'static>;
thread_local! {
static IN_CLIENT_JOB: std::cell::Cell<bool> = std::cell::Cell::new(false);
}
struct InClientJob;
impl InClientJob {
fn enter() -> Self {
IN_CLIENT_JOB.with(|f| f.set(true));
InClientJob
}
}
impl Drop for InClientJob {
fn drop(&mut self) { IN_CLIENT_JOB.with(|f| f.set(false)); }
}
enum ConnectClaim {
Connected,
Wait,
Claimed,
}
fn bump(c: &mut ClientCore) {
let next = *c.gen_tx.borrow() + 1;
let _ = c.gen_tx.send(next);
}
fn core_flight_settle(c: &mut ClientCore, hash: &str, res: Result<Value, Value>) -> bool {
match c.flights.get_mut(hash) {
Some(slot) if slot.open => {
slot.open = false;
slot.settled = Some(res);
}
_ => return false,
}
c.futures.remove(hash);
true
}
fn client_op<R, F>(f: F) -> R
where
R: Send + 'static,
F: FnOnce(&mut WsClientStore) -> R + Send + 'static,
{
if IN_CLIENT_JOB.with(|flag| flag.get()) {
panic!("ccxt: ws client job called back into the client store (would deadlock)");
}
let (tx, rx) = std::sync::mpsc::sync_channel::<Result<R, Box<dyn std::any::Any + Send>>>(1);
let job: ClientJob = Box::new(move |store: &mut WsClientStore| {
let _ = tx.send(std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(store))));
});
if WS_CLIENTS_TX.send(job).is_err() {
panic!("ccxt: ws client owner is gone");
}
match rx.recv() {
Ok(Ok(v)) => v,
Ok(Err(payload)) => std::panic::resume_unwind(payload),
Err(_) => panic!("ccxt: ws client owner dropped the reply"),
}
}
static WS_CLIENTS_TX: LazyLock<mpsc::UnboundedSender<ClientJob>> = LazyLock::new(|| {
let (tx, mut rx) = mpsc::unbounded_channel::<ClientJob>();
std::thread::Builder::new()
.name("ccxt-ws-clients".to_string())
.spawn(move || {
let mut store = WsClientStore::default();
while let Some(job) = rx.blocking_recv() {
let _in_job = InClientJob::enter();
job(&mut store);
}
})
.expect("ccxt: cannot start the ws client owner");
tx
});
fn now_ms() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}
fn parse_text(t: &str) -> Value {
match serde_json::from_str::<serde_json::Value>(t) {
Ok(j) => Value::from_json(&j),
Err(_) => Value::Str(t.to_string().into()),
}
}
fn parse_binary(b: &[u8]) -> Value {
use std::io::Read;
{
let mut d = flate2::read::GzDecoder::new(b);
let mut s = String::new();
if d.read_to_string(&mut s).is_ok() && !s.is_empty() {
return parse_text(&s);
}
}
{
let mut d = flate2::read::DeflateDecoder::new(b);
let mut s = String::new();
if d.read_to_string(&mut s).is_ok() && !s.is_empty() {
return parse_text(&s);
}
}
if let Ok(s) = std::str::from_utf8(b) {
return parse_text(s);
}
crate::exchange_stubs::bytes_to_value(b)
}
fn parse_raw_binary(b: &[u8]) -> Value {
match serde_json::from_slice::<serde_json::Value>(b) {
Ok(j) => Value::from_json(&j),
Err(_) => crate::exchange_stubs::bytes_to_value(b),
}
}
impl ClientState {
fn with<R, F>(&self, f: F) -> R
where
R: Send + 'static,
F: FnOnce(&mut ClientCore) -> R + Send + 'static,
{
let url = self.url.clone();
client_op(move |s| match s.clients.get_mut(&url) {
Some(c) => f(c),
None => panic!("ccxt: no ws client state for {url}"),
})
}
fn with_mut<R, F>(&self, f: F) -> R
where
R: Send + 'static,
F: FnOnce(&mut ClientCore) -> R + Send + 'static,
{
let url = self.url.clone();
client_op(move |s| {
let c = match s.clients.get_mut(&url) {
Some(c) => c,
None => panic!("ccxt: no ws client state for {url}"),
};
let r = f(c);
bump(c);
r
})
}
fn waiter(&self) -> watch::Receiver<u64> {
let mut rx = self.gen_rx.clone();
rx.borrow_and_update();
rx
}
fn push_incoming(&self, v: Value) {
self.with_mut(move |c| c.incoming.push_back(v));
}
pub async fn next_message(&self) -> Option<Value> {
let mock = self.with(|c| c.mock);
let mut rx = self.waiter();
loop {
let next = self.with(|c| c.incoming.pop_front());
if next.is_some() {
return next;
}
if self.with(|c| c.closed) {
return None;
}
if mock {
if tokio::time::timeout(std::time::Duration::from_millis(1500), rx.changed()).await.is_err() {
return None;
}
} else if rx.changed().await.is_err() {
return None;
}
}
}
pub fn resolve(&self, hash: &str, value: Value) {
let h = hash.to_string();
self.with_mut(move |c| {
core_flight_settle(c, &h, Ok(value.clone()));
c.resolved.insert(h.clone(), value);
});
}
pub fn reject(&self, hash: &str, err: Value) {
let h = hash.to_string();
self.with_mut(move |c| {
core_flight_settle(c, &h, Err(err.clone()));
c.rejections.insert(h.clone(), err);
});
}
pub fn take_settled(&self, hashes: &[String]) -> Option<Result<Value, Value>> {
let hs = hashes.to_vec();
self.with(move |c| {
for h in &hs {
if let Some(e) = c.rejections.remove(h) {
return Some(Err(e));
}
}
for h in &hs {
if let Some(v) = c.resolved.remove(h) {
return Some(Ok(v));
}
}
None
})
}
pub fn note_futures(&self, hashes: &[String]) {
let hs = hashes.to_vec();
self.with(move |c| {
for h in hs {
c.futures.insert(h);
}
});
}
pub fn flight_begin(&self, hash: &str) -> bool {
let h = hash.to_string();
self.with(move |c| match c.flights.get_mut(&h) {
Some(slot) if slot.open => false,
Some(slot) => {
slot.open = true;
c.futures.insert(h.clone());
true
}
None => {
c.flights.insert(h.clone(), FlightSlot { open: true, settled: None });
c.futures.insert(h);
true
}
})
}
pub fn flight_peek(&self, hash: &str) -> Option<Result<Value, Value>> {
let h = hash.to_string();
self.with(move |c| c.flights.get(&h).and_then(|s| s.settled.clone()))
}
pub fn flight_is_open(&self, hash: &str) -> bool {
let h = hash.to_string();
self.with(move |c| c.flights.get(&h).map(|s| s.open).unwrap_or(false))
}
pub fn flight_settle(&self, hash: &str, res: Result<Value, Value>) -> bool {
let h = hash.to_string();
self.with_mut(move |c| core_flight_settle(c, &h, res))
}
pub fn subscribe_once(&self, subscribe_hash: &str, subscription: Value) -> bool {
let sh = subscribe_hash.to_string();
self.with(move |c| {
if c.subscriptions.contains_key(&sh) {
return false;
}
let stored = if matches!(subscription, Value::Null) {
Value::Bool(true)
} else {
subscription
};
c.subscriptions.insert(sh, stored);
true
})
}
pub fn set_subscription(&self, subscribe_hash: &str, subscription: Value) {
let sh = subscribe_hash.to_string();
self.with(move |c| {
c.subscriptions.insert(sh, subscription);
});
}
pub fn is_subscribed(&self, subscribe_hash: &str) -> bool {
let sh = subscribe_hash.to_string();
self.with(move |c| c.subscriptions.contains_key(&sh))
}
pub fn send_text(&self, s: String) -> bool {
if std::env::var("CCXT_WS_DEBUG").is_ok() { eprintln!("[wssend] {}", s.chars().take(200).collect::<String>()); }
self.with(move |c| {
if c.mock {
c.mock_sent.push(parse_text(&s));
return true;
}
c.outgoing.send(Message::Text(s)).is_ok()
})
}
fn send_ping(&self) -> bool {
self.with(|c| c.outgoing.send(Message::Ping(Vec::new())).is_ok())
}
pub fn mock_enable(&self) {
self.with_mut(|c| {
c.mock = true;
c.connected = true;
});
}
pub fn mock_inject(&self, msg: Value) {
self.with_mut(move |c| c.incoming.push_back(msg));
}
pub fn is_mock(&self) -> bool { self.with(|c| c.mock) }
pub fn mock_sent_value(&self) -> Value {
self.with(|c| Value::Array(c.mock_sent.clone()))
}
pub fn has_pending_futures(&self) -> bool {
self.with(|c| !c.futures.is_empty())
}
pub fn has_queued_messages(&self) -> bool {
self.with(|c| !c.incoming.is_empty())
}
pub fn pop_queued_message(&self) -> Option<Value> {
self.with(|c| c.incoming.pop_front())
}
pub fn mark_ws_test_completed(&self) { self.with(|c| c.ws_test_completed = true); }
pub fn is_ws_test_completed(&self) -> bool { self.with(|c| c.ws_test_completed) }
pub fn reject_pending_futures(&self, err: Value) {
self.with_mut(move |c| {
let hashes: Vec<String> = c.futures.iter().cloned().collect();
for h in hashes {
core_flight_settle(c, &h, Err(err.clone()));
c.rejections.insert(h, err.clone());
}
});
}
pub fn is_closed(&self) -> bool {
self.with(|c| c.closed)
}
pub fn is_connected(&self) -> bool {
self.with(|c| c.connected)
}
pub fn on_pong(&self) {
self.with(|c| c.last_pong_ms = now_ms());
}
pub fn reset(&self) {
self.with(|c| {
c.resolved.clear();
c.rejections.clear();
c.subscriptions.clear();
c.futures.clear();
c.flights.clear();
});
}
pub fn reset_with_error(&self, error: Value) {
self.with_mut(move |c| {
let hashes: Vec<String> = c.futures.iter().cloned().collect();
for h in &hashes {
if c.resolved.contains_key(h) {
continue;
}
core_flight_settle(c, h, Err(error.clone()));
c.rejections.insert(h.clone(), error.clone());
}
c.futures.clear();
});
}
pub fn subscriptions_value(&self) -> Value {
let url = self.url.clone();
self.with(move |c| {
let mut m = indexmap::IndexMap::new();
m.insert("__ws_subs_url".to_string(), Value::Str(url.clone().into()));
for (h, sub) in c.subscriptions.iter() {
let tagged = match sub {
Value::Dict(d) => {
let mut inner = (**d).clone();
inner.insert("__ws_sub_ref".to_string(),
Value::Str(format!("{}\u{1}{}", url, h).into()));
Value::Dict(std::sync::Arc::new(inner))
}
other => other.clone(),
};
m.insert(h.clone(), tagged);
}
Value::Map(m)
})
}
pub fn set_subscription_field(&self, hash: &str, key: &str, val: Value) {
let h = hash.to_string();
let k = key.to_string();
self.with(move |c| {
if matches!(c.subscriptions.get(&h), Some(Value::Dict(_))) {
if let Some(Value::Dict(d)) = c.subscriptions.get_mut(&h) {
std::sync::Arc::make_mut(d).insert(k, val);
}
} else {
let mut inner = indexmap::IndexMap::new();
inner.insert(k, val);
c.subscriptions.insert(h, Value::Dict(std::sync::Arc::new(inner)));
}
});
}
pub fn futures_value(&self) -> Value {
let url = self.url.clone();
self.with(move |c| {
let mut m = indexmap::IndexMap::new();
for h in c.futures.iter() {
let mut fh = indexmap::IndexMap::new();
fh.insert("url".to_string(), Value::Str(url.clone().into()));
fh.insert("__ws_future_hash".to_string(), Value::Str(h.clone().into()));
m.insert(h.clone(), Value::Map(fh));
}
Value::Map(m)
})
}
}
pub fn get_client(url: &str) -> Option<Arc<ClientState>> {
let u = url.to_string();
client_op(move |s| {
s.clients.get(&u).filter(|c| !c.closed).map(|c| c.handle.clone())
})
}
pub fn value_subs_insert(url: &str, key: &str, val: Value) {
if let Some(c) = get_client(url) { c.set_subscription(key, val); }
}
pub fn value_subs_remove(url: &str, key: &str) {
let k = key.to_string();
if let Some(c) = get_client(url) { c.with(move |core| { core.subscriptions.remove(&k); }); }
}
pub fn value_sub_field_write(subref: &str, key: &str, val: Value) {
if let Some((url, hash)) = subref.split_once('\u{1}') {
if let Some(c) = get_client(url) { c.set_subscription_field(hash, key, val); }
}
}
fn upgrade_request(
url: &str,
headers: &[(String, String)],
) -> Result<tokio_tungstenite::tungstenite::handshake::client::Request, String> {
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::{HeaderName, HeaderValue};
let mut request = url
.into_client_request()
.map_err(|e| format!("[NetworkError] ws url {url}: {e}"))?;
for (key, value) in headers {
let name = HeaderName::from_bytes(key.as_bytes())
.map_err(|e| format!("[BadRequest] ws header {key}: {e}"))?;
let value = HeaderValue::from_str(value)
.map_err(|e| format!("[BadRequest] ws header {key}: {e}"))?;
request.headers_mut().insert(name, value);
}
Ok(request)
}
async fn connect_via_proxy(
url: &str,
headers: &[(String, String)],
proxy: &str,
) -> Result<tokio_tungstenite::WebSocketStream<
tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>, String> {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let target = url::Url::parse(url).map_err(|e| format!("[NetworkError] ws url {url}: {e}"))?;
let host = target.host_str().ok_or_else(|| format!("[NetworkError] ws url {url} has no host"))?;
let port = target.port_or_known_default().unwrap_or(443);
let p = url::Url::parse(proxy).map_err(|e| format!("[NetworkError] ws proxy {proxy}: {e}"))?;
let p_host = p.host_str().ok_or_else(|| format!("[NetworkError] ws proxy {proxy} has no host"))?;
let p_port = p.port_or_known_default().unwrap_or(8080);
let mut stream = tokio::net::TcpStream::connect((p_host, p_port))
.await
.map_err(|e| format!("[NetworkError] ws proxy connect {proxy}: {e}"))?;
let connect = format!(
"CONNECT {host}:{port} HTTP/1.1\r\nHost: {host}:{port}\r\nProxy-Connection: Keep-Alive\r\n\r\n"
);
stream
.write_all(connect.as_bytes())
.await
.map_err(|e| format!("[NetworkError] ws proxy CONNECT {proxy}: {e}"))?;
let mut head = Vec::new();
let mut byte = [0u8; 1];
while !head.ends_with(b"\r\n\r\n") {
let n = stream
.read(&mut byte)
.await
.map_err(|e| format!("[NetworkError] ws proxy CONNECT {proxy}: {e}"))?;
if n == 0 {
return Err(format!("[NetworkError] ws proxy {proxy} closed during CONNECT"));
}
head.push(byte[0]);
if head.len() > 8192 {
return Err(format!("[NetworkError] ws proxy {proxy} sent an oversized CONNECT reply"));
}
}
let status = String::from_utf8_lossy(&head);
let first = status.lines().next().unwrap_or_default();
if !first.contains(" 200") {
return Err(format!("[NetworkError] ws proxy {proxy} refused CONNECT: {first}"));
}
let (ws, _resp) = tokio_tungstenite::client_async_tls(upgrade_request(url, headers)?, stream)
.await
.map_err(|e| format!("[NetworkError] ws connect {url} via {proxy}: {e}"))?;
Ok(ws)
}
pub async fn ensure_client(url: &str, proxy: Option<String>, decompress_binary: bool) -> Result<Arc<ClientState>, String> {
ensure_client_with_headers(url, &[], proxy, decompress_binary).await
}
pub async fn ensure_client_with_headers(
url: &str,
headers: &[(String, String)],
proxy: Option<String>,
decompress_binary: bool,
) -> Result<Arc<ClientState>, String> {
let state = ensure_slot(url);
loop {
let mut rx = state.waiter();
match state.with(|c| {
if c.connected {
ConnectClaim::Connected
} else if c.connecting {
ConnectClaim::Wait
} else {
c.connecting = true;
ConnectClaim::Claimed
}
}) {
ConnectClaim::Connected => return Ok(state),
ConnectClaim::Claimed => break,
ConnectClaim::Wait => {
if rx.changed().await.is_err() {
return Err(format!("[NetworkError] ws {url} client owner is gone"));
}
}
}
}
let dialled = match proxy.as_deref().filter(|p| !p.is_empty()) {
Some(p) => connect_via_proxy(url, headers, p).await,
None => match upgrade_request(url, headers) {
Ok(req) => tokio_tungstenite::connect_async(req)
.await
.map(|(ws, _resp)| ws)
.map_err(|e| format!("[NetworkError] ws connect {url}: {e}")),
Err(e) => Err(e),
},
};
let ws = match dialled {
Ok(ws) => ws,
Err(e) => {
state.with_mut(|c| c.connecting = false);
return Err(e);
}
};
let (mut write, mut read) = ws.split();
let maybe_rx = state.with(|c| c.pending_rx.take());
let mut rx = match maybe_rx {
Some(rx) => rx,
None => {
let e = format!("[NetworkError] ws {url} slot has no writer channel");
state.with_mut(|c| c.connecting = false);
return Err(e);
}
};
state.with_mut(|c| c.last_pong_ms = now_ms());
tokio::spawn(async move {
while let Some(msg) = rx.recv().await {
if write.send(msg).await.is_err() {
break;
}
}
let _ = write.close().await;
});
let st = state.clone();
tokio::spawn(async move {
while let Some(frame) = read.next().await {
match frame {
Ok(Message::Text(t)) => st.push_incoming(parse_text(&t)),
Ok(Message::Binary(b)) => st.push_incoming(if decompress_binary { parse_binary(&b) } else { parse_raw_binary(&b) }),
Ok(Message::Pong(_)) => st.on_pong(),
Ok(Message::Ping(_)) => {}
Ok(Message::Close(_)) | Err(_) => break,
_ => {}
}
}
st.with_mut(|c| {
c.connected = false;
c.closed = true;
});
});
let st2 = state.clone();
tokio::spawn(async move {
let mut iv = tokio::time::interval(std::time::Duration::from_secs(30));
iv.tick().await; loop {
iv.tick().await;
if st2.is_closed() {
break;
}
if !st2.send_ping() {
break;
}
}
});
state.with_mut(|c| {
c.connected = true;
c.connecting = false;
});
Ok(state)
}
pub fn client_field_live(url: &str, field: &str) -> Value {
match get_client(url) {
Some(c) if field == "futures" => c.futures_value(),
Some(c) => c.subscriptions_value(),
None => Value::Map(indexmap::IndexMap::new()),
}
}
fn ensure_slot(url: &str) -> Arc<ClientState> {
let u = url.to_string();
client_op(move |s| {
if let Some(c) = s.clients.get(&u) {
if !c.closed {
return c.handle.clone();
}
}
let (out_tx, out_rx) = mpsc::unbounded_channel::<Message>();
let (gen_tx, gen_rx) = watch::channel(0u64);
let handle = Arc::new(ClientState { url: u.clone(), gen_rx });
s.clients.insert(u.clone(), ClientCore {
handle: handle.clone(),
outgoing: out_tx,
pending_rx: Some(out_rx),
connecting: false,
incoming: VecDeque::new(),
resolved: HashMap::new(),
rejections: HashMap::new(),
subscriptions: HashMap::new(),
futures: HashSet::new(),
flights: HashMap::new(),
connected: false,
closed: false,
last_pong_ms: now_ms(),
mock: false,
mock_sent: Vec::new(),
ws_test_completed: false,
gen_tx,
});
handle
})
}
pub fn drop_client(url: &str) {
let u = url.to_string();
client_op(move |s| {
if let Some(c) = s.clients.get_mut(&u) {
c.closed = true;
c.connected = false;
bump(c);
}
});
}
pub fn mock_setup(url: &str) {
let c = ensure_slot(url);
c.with_mut(|core| {
core.resolved.clear();
core.rejections.clear();
core.subscriptions.clear();
core.futures.clear();
core.flights.clear();
core.mock = true;
core.connected = true;
core.mock_sent.clear();
core.incoming.clear();
core.ws_test_completed = false;
});
}
pub fn mock_inject(url: &str, msg: Value) {
if let Some(c) = get_client(url) { c.mock_inject(msg); }
}
pub fn mock_sent_messages(url: &str) -> Value {
match get_client(url) { Some(c) => c.mock_sent_value(), None => Value::Array(vec![]) }
}
pub fn mock_has_pending_futures(url: &str) -> bool {
get_client(url).map(|c| c.has_pending_futures()).unwrap_or(false)
}
pub fn mock_has_queued_messages(url: &str) -> bool {
get_client(url).map(|c| c.has_queued_messages()).unwrap_or(false)
}
pub fn mock_pop_message(url: &str) -> Option<Value> {
get_client(url).and_then(|c| c.pop_queued_message())
}
pub fn mock_mark_completed(url: &str) {
if let Some(c) = get_client(url) { c.mark_ws_test_completed(); }
}
pub fn mock_is_completed(url: &str) -> bool {
get_client(url).map(|c| c.is_ws_test_completed()).unwrap_or(true)
}
pub fn mock_reject_futures(url: &str) {
if let Some(c) = get_client(url) {
c.reject_pending_futures(Value::Str(
"[ExchangeError] static ws test: the injected messages did not resolve the watch future".to_string().into()));
}
}
pub fn client_value(url: &str) -> Value {
let c = ensure_slot(url);
let mut m = indexmap::IndexMap::new();
m.insert("url".to_string(), Value::Str(url.to_string().into()));
m.insert("subscriptions".to_string(), c.subscriptions_value());
m.insert("futures".to_string(), c.futures_value());
Value::Map(m)
}
#[allow(dead_code)]
#[derive(Clone)]
pub struct ClientHandle {
pub url: Value,
pub subscriptions: Value,
pub futures: Value,
pub rejections: Value,
value: Value,
}
#[allow(dead_code)]
impl ClientHandle {
pub fn from_value(value: Value) -> ClientHandle {
let read = |k: &str| crate::get_value(&value, &Value::Str(k.to_string().into()));
ClientHandle {
url: read("url"),
subscriptions: read("subscriptions"),
futures: read("futures"),
rejections: read("rejections"),
value,
}
}
pub fn url(&self) -> Option<String> {
match &self.url { Value::Str(s) => Some(s.to_string()), _ => None }
}
pub fn as_value(&self) -> Value { self.value.clone() }
pub fn resolve(&self, args: &[Value]) -> Value { value_resolve(&self.value, args) }
pub fn reject(&self, args: &[Value]) -> Value { value_reject(&self.value, args) }
pub fn send(&self, args: &[Value]) -> Value { value_send(&self.value, args) }
pub fn reset(&self, error: Value) -> Value { value_reset(&self.value, error) }
pub fn on_pong(&self) -> Value { value_on_pong(&self.value) }
pub fn as_map(&self) -> Option<&indexmap::IndexMap<String, Value>> {
self.value.as_map()
}
pub fn remove_subscription(&mut self, key: &Value) {
if let (Value::Dict(m), Value::Str(k)) = (&mut self.subscriptions, key) {
crate::value::try_ws_subs_remove(m, k);
Arc::make_mut(m).shift_remove(k.as_ref());
}
}
pub fn add_subscription(&mut self, key: &Value, val: Value) {
match (&mut self.subscriptions, key) {
(Value::Dict(m), Value::Str(k)) => {
crate::value::try_ws_subs_write(m, k, &val);
Arc::make_mut(m).insert(k.to_string(), val);
}
(other, _) => crate::runtime::add_element_to_object(other, key, val),
}
}
pub fn has_subscription(&self, key: &Value) -> bool { crate::runtime::in_op(&self.subscriptions, key) }
pub fn get_subscription(&self, key: &Value) -> Value { crate::get_value(&self.subscriptions, key) }
pub fn subscription_keys(&self) -> Vec<Value> {
crate::runtime::object_keys(&self.subscriptions).as_array().cloned().unwrap_or_default()
}
pub fn has_future(&self, key: &Value) -> bool { crate::runtime::in_op(&self.futures, key) }
pub fn get_future(&self, key: &Value) -> Value { crate::get_value(&self.futures, key) }
pub fn future_keys(&self) -> Vec<Value> {
crate::runtime::object_keys(&self.futures).as_array().cloned().unwrap_or_default()
}
pub fn future(&self, args: &[Value]) -> Value {
self.value.future(args)
}
pub fn reusable_future(&self, msg_hash: Value) -> Value {
self.value.reusable_future(msg_hash)
}
}
pub fn begin_flight(url: &str, hash: &str) -> bool {
ensure_slot(url).flight_begin(hash)
}
pub fn flight_handle(url: &str, hash: &str, led: bool) -> Value {
let mut m = indexmap::IndexMap::new();
m.insert("url".to_string(), Value::Str(url.to_string().into()));
m.insert("__ws_flight_hash".to_string(), Value::Str(hash.to_string().into()));
m.insert("__ws_flight_led".to_string(), Value::Bool(led));
Value::Map(m)
}
fn flight_hash_of(handle: &Value) -> Option<String> {
for key in ["__ws_flight_hash", "__ws_future_hash"] {
if let Value::Str(h) = crate::get_value(handle, &Value::Str(key.to_string().into())) {
return Some(h.to_string());
}
}
None
}
pub async fn ws_await_flight(handle: &Value) -> Value {
let (url, hash) = match (url_of(handle), flight_hash_of(handle)) {
(Some(u), Some(h)) => (u, h),
_ => return Value::Null,
};
let client = match get_client(&url) {
Some(c) => c,
None => return Value::Null,
};
let settled = client.flight_peek(&hash);
let led = matches!(
crate::get_value(handle, &Value::Str("__ws_flight_led".into())),
Value::Bool(true)
);
if led || !client.flight_is_open(&hash) {
return match settled {
Some(Ok(v)) => v,
Some(Err(e)) => panic!(
"{}",
match &e {
Value::Str(s) => s.clone(),
v => crate::runtime::stringify_param(v).into(),
}
),
None => Value::Null,
};
}
let budget = if client.is_mock() {
std::time::Duration::from_millis(1500)
} else {
std::time::Duration::from_secs(60)
};
let deadline = tokio::time::Instant::now() + budget;
loop {
let mut rx = client.waiter();
if let Some(res) = client.flight_peek(&hash) {
return match res {
Ok(v) => v,
Err(e) => panic!(
"{}",
match &e {
Value::Str(s) => s.clone(),
v => crate::runtime::stringify_param(v).into(),
}
),
};
}
if !client.flight_is_open(&hash) {
return Value::Null;
}
if tokio::time::timeout_at(deadline, rx.changed()).await.is_err() {
panic!(
"[NetworkError] timed out waiting for the in-flight '{}' on {}",
hash, url
);
}
}
}
pub fn url_of(client: &Value) -> Option<String> {
match crate::get_value(client, &Value::Str("url".into())) {
Value::Str(s) => Some(s.to_string()),
_ => None,
}
}
fn hash_str(v: &Value) -> Option<String> {
match v {
Value::Str(s) => Some(s.to_string()),
_ => None,
}
}
pub fn value_resolve(client: &Value, args: &[Value]) -> Value {
let value = args.get(0).cloned().unwrap_or(Value::Null);
if let Some(hash) = future_hash_of(client) {
if let Some(url) = url_of(client) {
if let Some(c) = get_client(&url) { c.resolve(&hash, value.clone()); }
}
return value;
}
if let (Some(url), Some(hash)) = (url_of(client), args.get(1).and_then(hash_str)) {
if let Some(c) = get_client(&url) {
c.resolve(&hash, value.clone());
}
}
value
}
fn future_hash_of(client: &Value) -> Option<String> {
match crate::get_value(client, &Value::Str("__ws_future_hash".into())) {
Value::Str(s) => Some(s.to_string()),
_ => None,
}
}
pub fn value_reject(client: &Value, args: &[Value]) -> Value {
let err = args.get(0).cloned().unwrap_or(Value::Null);
if let Some(hash) = future_hash_of(client) {
if let Some(url) = url_of(client) {
if let Some(c) = get_client(&url) { c.reject(&hash, err.clone()); }
}
return err;
}
if let Some(url) = url_of(client) {
if let Some(c) = get_client(&url) {
match args.get(1).and_then(hash_str) {
Some(hash) => c.reject(&hash, err.clone()),
None => c.reject_pending_futures(err.clone()),
}
}
}
err
}
pub fn value_send(client: &Value, args: &[Value]) -> Value {
if let Some(url) = url_of(client) {
if let Some(c) = get_client(&url) {
let payload = match args.get(0) {
Some(Value::Str(s)) => s.to_string(),
Some(v) => v.to_json().to_string(),
None => String::new().into(),
};
c.send_text(payload);
}
}
Value::Null
}
pub fn value_reset(client: &Value, error: Value) -> Value {
if let Some(url) = url_of(client) {
if let Some(c) = get_client(&url) {
if matches!(error, Value::Null) {
c.reset();
} else {
c.reset_with_error(error);
}
}
}
Value::Null
}
pub fn value_on_pong(client: &Value) -> Value {
if let Some(url) = url_of(client) {
if let Some(c) = get_client(&url) {
c.on_pong();
}
}
Value::Null
}
#[cfg(test)]
mod tests {
use super::*;
use tokio_tungstenite::tungstenite::Message as WsMessage;
async fn spawn_mock_server() -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
if let Ok((stream, _)) = listener.accept().await {
let mut ws = tokio_tungstenite::accept_async(stream).await.unwrap();
if let Some(Ok(WsMessage::Text(sub))) = ws.next().await {
assert!(sub.contains("subscribe"), "expected subscribe, got {sub}");
}
for px in ["100.5", "101.0", "101.5"] {
let msg = format!("{{\"channel\":\"ticker\",\"symbol\":\"BTC/USDT\",\"last\":\"{px}\"}}");
ws.send(WsMessage::Text(msg)).await.unwrap();
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
let _ = ws.close(None).await;
}
});
format!("ws://{addr}")
}
#[test]
fn value_reset_rejects_pending_futures() {
let url = "ws://reset-with-error.test";
mock_setup(url);
let client = get_client(url).expect("mock client");
client.note_futures(&["pending".to_string(), "delivered".to_string()]);
client.resolve("delivered", Value::Str("payload".into()));
let handle = client_value(url);
let error = Value::Str("[InvalidNonce] gap detected".into());
value_reset(&handle, error.clone());
assert_eq!(
client.take_settled(&["pending".to_string()]),
Some(Err(error))
);
assert_eq!(
client.take_settled(&["delivered".to_string()]),
Some(Ok(Value::Str("payload".into())))
);
assert!(!client.has_pending_futures());
client.note_futures(&["pending2".to_string()]);
value_reset(&handle, Value::Null);
assert_eq!(client.take_settled(&["pending2".to_string()]), None);
assert!(!client.has_pending_futures());
drop_client(url);
}
#[tokio::test]
async fn upgrade_request_carries_option_headers() {
use tokio_tungstenite::tungstenite::handshake::server::{Request, Response};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = tokio::sync::oneshot::channel::<Option<String>>();
tokio::spawn(async move {
if let Ok((stream, _)) = listener.accept().await {
let mut seen = None;
let callback = |req: &Request, resp: Response| {
seen = req.headers().get("X-Gate-Size-Decimal").map(|v| v.to_str().unwrap().to_string());
Ok(resp)
};
let ws = tokio_tungstenite::accept_hdr_async(stream, callback).await.unwrap();
let _ = tx.send(seen);
drop(ws);
}
});
let url = format!("ws://{addr}");
let headers = vec![("X-Gate-Size-Decimal".to_string(), "1".to_string())];
ensure_client_with_headers(&url, &headers, None, true).await.expect("connect");
assert_eq!(rx.await.unwrap().as_deref(), Some("1"));
drop_client(&url);
}
#[tokio::test]
async fn transport_roundtrip() {
let url = spawn_mock_server().await;
let client = ensure_client(&url, None, true).await.expect("connect");
assert!(client.subscribe_once("ticker:BTC/USDT", Value::Null));
assert!(!client.subscribe_once("ticker:BTC/USDT", Value::Null));
assert!(client.send_text("{\"op\":\"subscribe\",\"channel\":\"ticker\"}".to_string()));
let mut lasts = Vec::new();
while lasts.len() < 3 {
let msg = client.next_message().await.expect("message before close");
let last = crate::get_value(&msg, &Value::Str("last".into()));
if let Value::Str(s) = &last {
client.resolve("ticker", Value::Str(s.clone()));
}
if let Some(Ok(Value::Str(v))) = client.take_settled(&["ticker".to_string()]) {
lasts.push(v);
}
}
assert_eq!(lasts, vec!["100.5", "101.0", "101.5"]);
let subs = client.subscriptions_value();
assert!(crate::runtime::is_true(&crate::get_value(
&subs,
&Value::Str("ticker:BTC/USDT".into())
)));
drop_client(&url);
}
struct TestWsCore {
exchange: crate::exchange::Exchange,
}
impl std::ops::Deref for TestWsCore {
type Target = crate::exchange::Exchange;
fn deref(&self) -> &Self::Target {
&self.exchange
}
}
impl std::ops::DerefMut for TestWsCore {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.exchange
}
}
impl crate::exchange::DerivedExchange for TestWsCore {}
impl crate::exchange_generated::ExchangeBase for TestWsCore {
fn call_dynamic<'a>(
&'a mut self,
method: &'a str,
args: Vec<Value>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Value> + Send + 'a>> {
Box::pin(async move {
match method {
"handle_message" => {
let client = args.get(0).cloned().unwrap_or(Value::Null);
let message = args.get(1).cloned().unwrap_or(Value::Null);
let last = crate::get_value(&message, &Value::Str("last".into()));
client.resolve(&[last, Value::Str("ticker".into())]);
Value::Null
}
_ => self.call_dynamic_base(method, args).await,
}
})
}
}
#[tokio::test]
async fn watch_drive_loop_end_to_end() {
use crate::exchange::ExchangeRuntime;
let url = spawn_mock_server().await;
let mut core = TestWsCore {
exchange: crate::exchange::Exchange::new(None),
};
let result = ExchangeRuntime::watch(
&mut core,
Value::Str(url.clone().into()),
Value::Str("ticker".into()),
&[
Value::Str("{\"op\":\"subscribe\",\"channel\":\"ticker\"}".into()),
Value::Str("ticker".into()),
Value::Null,
],
)
.await;
assert_eq!(result, Value::Str("100.5".into()));
drop_client(&url);
}
#[tokio::test]
async fn flight_elects_one_leader_and_registers_it() {
let url = "flight-test-election";
let c = ensure_slot(url);
assert!(c.flight_begin("authenticate:future"), "first caller leads");
assert!(!c.flight_begin("authenticate:future"), "second caller follows");
assert!(in_op_futures(&c, "authenticate:future"));
drop_client(url);
}
#[tokio::test]
async fn flight_settle_wakes_waiters_and_reopens() {
let url = "flight-test-settle";
let c = ensure_slot(url);
assert!(c.flight_begin("auth"));
c.resolve("auth", Value::Str("listen-key".into()));
assert_eq!(
ws_await_flight(&flight_handle(url, "auth", false)).await,
Value::Str("listen-key".into())
);
assert_eq!(
ws_await_flight(&flight_handle(url, "auth", false)).await,
Value::Str("listen-key".into())
);
assert!(!in_op_futures(&c, "auth"));
assert!(c.flight_begin("auth"), "a settled flight re-elects");
drop_client(url);
}
#[tokio::test]
async fn flight_follower_waits_for_the_leader() {
let url = "flight-test-wait";
let c = ensure_slot(url);
assert!(c.flight_begin("auth"));
let settler = {
let c = c.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(60)).await;
c.resolve("auth", Value::Str("late".into()));
})
};
let started = std::time::Instant::now();
let got = ws_await_flight(&flight_handle(url, "auth", false)).await;
assert_eq!(got, Value::Str("late".into()));
assert!(started.elapsed() >= std::time::Duration::from_millis(50), "waited for the leader");
settler.await.unwrap();
drop_client(url);
}
#[tokio::test]
async fn a_leader_never_blocks_on_its_own_flight() {
let url = "flight-test-leader";
let c = ensure_slot(url);
let led = c.flight_begin("authenticated");
assert!(led);
let started = std::time::Instant::now();
assert_eq!(
ws_await_flight(&flight_handle(url, "authenticated", led)).await,
Value::Null
);
assert!(started.elapsed() < std::time::Duration::from_millis(200), "returned at once");
c.resolve("authenticated", Value::Bool(true));
let led2 = c.flight_begin("authenticated");
assert_eq!(
ws_await_flight(&flight_handle(url, "authenticated", led2)).await,
Value::Bool(true),
"a re-lead with no new work reports the last value"
);
drop_client(url);
}
#[tokio::test]
async fn flight_handle_without_a_flight_returns_null() {
let url = "flight-test-missing";
ensure_slot(url);
assert_eq!(ws_await_flight(&flight_handle(url, "nope", false)).await, Value::Null);
drop_client(url);
}
fn in_op_futures(c: &Arc<ClientState>, hash: &str) -> bool {
!matches!(
crate::get_value(&c.futures_value(), &Value::Str(hash.to_string().into())),
Value::Null
)
}
#[tokio::test]
async fn parse_helpers() {
assert!(matches!(parse_text("{\"a\":1}"), Value::Dict(_)));
assert_eq!(parse_text("pong"), Value::Str("pong".into()));
use std::io::Write;
let mut e = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
e.write_all(b"{\"x\":true}").unwrap();
let gz = e.finish().unwrap();
assert!(matches!(parse_binary(&gz), Value::Dict(_)));
}
#[test]
fn raw_binary_frames_stay_bytes() {
let frame: Vec<u8> = vec![0x10, 0x00, 0x28, 0x00, 0x01, 0x00, 0x02, 0x00, 0x41, 0x42, 0x43, 0x01];
assert!(std::str::from_utf8(&frame).is_ok());
assert_eq!(parse_raw_binary(&frame), crate::exchange_stubs::bytes_to_value(&frame));
assert!(matches!(parse_raw_binary(b"{\"x\":true}"), Value::Dict(_)));
}
}