pub mod api;
pub mod api_client;
pub mod auth;
pub mod connection;
pub mod heartbeat;
pub mod router;
pub mod store;
pub mod types;
use std::sync::Arc;
use futures_util::future::BoxFuture;
use tokio::sync::{broadcast, mpsc, watch, Mutex, Notify, RwLock};
use tracing::{error, info, warn};
use crate::error::{OkxError, OkxResult};
use crate::types::ws::channels::WsSubscriptionArg;
use crate::types::ws::events::{WsConnectionType, WsMessage};
use crate::types::ws::requests::WsSubRequest;
use self::api::PendingRequests;
use self::store::{ConnectionState, WsStore};
use self::types::WsConfig;
#[derive(Clone)]
pub struct WebsocketClient {
config: WsConfig,
store: Arc<RwLock<WsStore>>,
event_tx: broadcast::Sender<WsMessage>,
pending_requests: Arc<Mutex<PendingRequests>>,
authentication_signals: Arc<AuthenticationSignals>,
write_txs: Arc<RwLock<WriteChannels>>,
heartbeat_stops: Arc<RwLock<HeartbeatStops>>,
connection_attempts: Arc<Mutex<ConnectionAttempts>>,
reconnect_tasks: Arc<Mutex<ReconnectTasks>>,
}
#[derive(Clone, Debug)]
enum ConnectionFailure {
Authentication(String),
Configuration(String),
ConnectionLost,
WebSocket(String),
}
impl ConnectionFailure {
fn from_error(error: OkxError) -> Self {
match error {
OkxError::Auth(message) => Self::Authentication(message),
OkxError::Config(message) => Self::Configuration(message),
OkxError::Ws(message) => Self::WebSocket(message),
OkxError::WsConnectionLost => Self::ConnectionLost,
error => Self::WebSocket(error.to_string()),
}
}
fn into_error(self) -> OkxError {
match self {
Self::Authentication(message) => OkxError::Auth(message),
Self::Configuration(message) => OkxError::Config(message),
Self::ConnectionLost => OkxError::WsConnectionLost,
Self::WebSocket(message) => OkxError::Ws(message),
}
}
}
type ConnectionOutcome = Result<(), ConnectionFailure>;
type ConnectionAttemptReceiver = watch::Receiver<Option<ConnectionOutcome>>;
#[derive(Default)]
struct ConnectionAttempts {
public: Option<ConnectionAttemptReceiver>,
private: Option<ConnectionAttemptReceiver>,
business: Option<ConnectionAttemptReceiver>,
}
impl ConnectionAttempts {
fn get(&self, conn_type: WsConnectionType) -> Option<ConnectionAttemptReceiver> {
match conn_type {
WsConnectionType::Public => self.public.clone(),
WsConnectionType::Private => self.private.clone(),
WsConnectionType::Business => self.business.clone(),
}
}
fn set(&mut self, conn_type: WsConnectionType, receiver: ConnectionAttemptReceiver) {
match conn_type {
WsConnectionType::Public => self.public = Some(receiver),
WsConnectionType::Private => self.private = Some(receiver),
WsConnectionType::Business => self.business = Some(receiver),
}
}
fn remove(&mut self, conn_type: WsConnectionType) {
match conn_type {
WsConnectionType::Public => self.public = None,
WsConnectionType::Private => self.private = None,
WsConnectionType::Business => self.business = None,
}
}
}
struct ReconnectTask {
id: u64,
stop_tx: watch::Sender<bool>,
}
#[derive(Default)]
struct ReconnectTasks {
next_id: u64,
public: Option<ReconnectTask>,
private: Option<ReconnectTask>,
business: Option<ReconnectTask>,
}
impl ReconnectTasks {
fn get(&self, conn_type: WsConnectionType) -> Option<&ReconnectTask> {
match conn_type {
WsConnectionType::Public => self.public.as_ref(),
WsConnectionType::Private => self.private.as_ref(),
WsConnectionType::Business => self.business.as_ref(),
}
}
fn set(&mut self, conn_type: WsConnectionType, task: ReconnectTask) {
match conn_type {
WsConnectionType::Public => self.public = Some(task),
WsConnectionType::Private => self.private = Some(task),
WsConnectionType::Business => self.business = Some(task),
}
}
fn remove(&mut self, conn_type: WsConnectionType) -> Option<ReconnectTask> {
match conn_type {
WsConnectionType::Public => self.public.take(),
WsConnectionType::Private => self.private.take(),
WsConnectionType::Business => self.business.take(),
}
}
fn start(&mut self, conn_type: WsConnectionType) -> Option<(u64, watch::Receiver<bool>)> {
if self.get(conn_type).is_some() {
return None;
}
self.next_id = self.next_id.wrapping_add(1);
let id = self.next_id;
let (stop_tx, stop_rx) = watch::channel(false);
self.set(conn_type, ReconnectTask { id, stop_tx });
Some((id, stop_rx))
}
fn finish(&mut self, conn_type: WsConnectionType, id: u64) {
if self.get(conn_type).is_some_and(|task| task.id == id) {
self.remove(conn_type);
}
}
fn stop_all(&mut self) {
for task in [
self.public.take(),
self.private.take(),
self.business.take(),
]
.into_iter()
.flatten()
{
let _ = task.stop_tx.send(true);
}
}
}
#[derive(Default)]
struct AuthenticationSignals {
public: Notify,
private: Notify,
business: Notify,
}
impl AuthenticationSignals {
fn get(&self, conn_type: WsConnectionType) -> &Notify {
match conn_type {
WsConnectionType::Public => &self.public,
WsConnectionType::Private => &self.private,
WsConnectionType::Business => &self.business,
}
}
}
#[derive(Default, Clone)]
struct WriteChannels {
public: Option<mpsc::UnboundedSender<String>>,
private: Option<mpsc::UnboundedSender<String>>,
business: Option<mpsc::UnboundedSender<String>>,
}
impl WriteChannels {
fn get(&self, conn_type: WsConnectionType) -> Option<&mpsc::UnboundedSender<String>> {
match conn_type {
WsConnectionType::Public => self.public.as_ref(),
WsConnectionType::Private => self.private.as_ref(),
WsConnectionType::Business => self.business.as_ref(),
}
}
fn set(&mut self, conn_type: WsConnectionType, tx: mpsc::UnboundedSender<String>) {
match conn_type {
WsConnectionType::Public => self.public = Some(tx),
WsConnectionType::Private => self.private = Some(tx),
WsConnectionType::Business => self.business = Some(tx),
}
}
fn remove(&mut self, conn_type: WsConnectionType) {
match conn_type {
WsConnectionType::Public => self.public = None,
WsConnectionType::Private => self.private = None,
WsConnectionType::Business => self.business = None,
}
}
}
#[derive(Default)]
struct HeartbeatStops {
public: Option<watch::Sender<bool>>,
private: Option<watch::Sender<bool>>,
business: Option<watch::Sender<bool>>,
}
impl HeartbeatStops {
fn set(&mut self, conn_type: WsConnectionType, tx: watch::Sender<bool>) {
match conn_type {
WsConnectionType::Public => self.public = Some(tx),
WsConnectionType::Private => self.private = Some(tx),
WsConnectionType::Business => self.business = Some(tx),
}
}
fn stop_all(&mut self) {
for sender in [self.public.take(), self.private.take(), self.business.take()]
.into_iter()
.flatten()
{
let _ = sender.send(true);
}
}
fn stop(&mut self, conn_type: WsConnectionType) {
let sender = match conn_type {
WsConnectionType::Public => self.public.take(),
WsConnectionType::Private => self.private.take(),
WsConnectionType::Business => self.business.take(),
};
if let Some(sender) = sender {
let _ = sender.send(true);
}
}
}
fn partition_args(
args: Vec<WsSubscriptionArg>,
) -> (
Vec<WsSubscriptionArg>,
Vec<WsSubscriptionArg>,
Vec<WsSubscriptionArg>,
) {
let mut public = Vec::new();
let mut private = Vec::new();
let mut business = Vec::new();
for arg in args {
match router::route_subscription(&arg) {
WsConnectionType::Public => public.push(arg),
WsConnectionType::Private => private.push(arg),
WsConnectionType::Business => business.push(arg),
}
}
(public, private, business)
}
impl WebsocketClient {
pub fn new(config: WsConfig) -> Self {
let (event_tx, _) = broadcast::channel(1024);
Self {
config,
store: Arc::new(RwLock::new(WsStore::new())),
event_tx,
pending_requests: Arc::new(Mutex::new(PendingRequests::new())),
authentication_signals: Arc::new(AuthenticationSignals::default()),
write_txs: Arc::new(RwLock::new(WriteChannels::default())),
heartbeat_stops: Arc::new(RwLock::new(HeartbeatStops::default())),
connection_attempts: Arc::new(Mutex::new(ConnectionAttempts::default())),
reconnect_tasks: Arc::new(Mutex::new(ReconnectTasks::default())),
}
}
pub fn event_receiver(&self) -> broadcast::Receiver<WsMessage> {
self.event_tx.subscribe()
}
pub async fn subscribe(
&self,
args: Vec<WsSubscriptionArg>,
) -> OkxResult<broadcast::Receiver<WsMessage>> {
if args.iter().any(WsSubscriptionArg::requires_authentication)
&& self.config.client_config.credentials.is_none()
{
return Err(OkxError::Auth(
"Credentials required for authenticated WebSocket subscription".into(),
));
}
let (public_args, private_args, business_args) = partition_args(args);
if !public_args.is_empty() {
self.subscribe_to_connection(WsConnectionType::Public, public_args)
.await?;
}
if !private_args.is_empty() {
self.subscribe_to_connection(WsConnectionType::Private, private_args)
.await?;
}
if !business_args.is_empty() {
self.subscribe_to_connection(WsConnectionType::Business, business_args)
.await?;
}
Ok(self.event_tx.subscribe())
}
pub async fn unsubscribe(&self, args: Vec<WsSubscriptionArg>) -> OkxResult<()> {
let (public_args, private_args, business_args) = partition_args(args);
if !public_args.is_empty() {
self.send_unsubscribe(WsConnectionType::Public, public_args)
.await?;
}
if !private_args.is_empty() {
self.send_unsubscribe(WsConnectionType::Private, private_args)
.await?;
}
if !business_args.is_empty() {
self.send_unsubscribe(WsConnectionType::Business, business_args)
.await?;
}
Ok(())
}
pub async fn send_api_request(
&self,
op: &str,
args: Vec<serde_json::Value>,
) -> OkxResult<crate::types::ws::events::WsApiResponse> {
let conn_type = if op.starts_with("sprd-") {
WsConnectionType::Business
} else {
WsConnectionType::Private
};
if self.config.client_config.credentials.is_none() {
return Err(OkxError::Auth(
"Credentials required for WebSocket API request".into(),
));
}
self.ensure_connected(conn_type).await?;
self.wait_for_authentication(conn_type).await?;
let request = api::build_api_request(op, args);
let json = serde_json::to_string(&request)?;
let request_id = request.id.clone();
let rx = {
let mut pending = self.pending_requests.lock().await;
pending.register(request_id.clone(), conn_type)
};
let send_result = {
let write_txs = self.write_txs.read().await;
match write_txs.get(conn_type) {
Some(tx) => tx
.send(json)
.map_err(|_| OkxError::Ws("write channel closed".into())),
None => Err(OkxError::Ws(format!("no {conn_type} connection"))),
}
};
if let Err(error) = send_result {
self.pending_requests.lock().await.remove(&request_id);
return Err(error);
}
let response =
match tokio::time::timeout(self.config.client_config.request_timeout, rx).await {
Ok(Ok(response)) => response,
Ok(Err(_)) => return Err(OkxError::WsConnectionLost),
Err(_) => {
self.pending_requests.lock().await.remove(&request_id);
return Err(OkxError::WsApiTimeout {
id: request_id,
operation: op.to_string(),
});
}
};
if response.code == "0" {
Ok(response)
} else {
Err(OkxError::Api {
code: response.code,
msg: response.msg,
})
}
}
async fn ensure_connected(&self, conn_type: WsConnectionType) -> OkxResult<()> {
let is_connected = {
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
connection.reconnect_allowed = true;
matches!(
connection.state,
ConnectionState::Connected | ConnectionState::Authenticated
)
};
if is_connected {
return Ok(());
}
self.connect(conn_type).await
}
async fn subscribe_to_connection(
&self,
conn_type: WsConnectionType,
args: Vec<WsSubscriptionArg>,
) -> OkxResult<()> {
let requires_authentication = args.iter().any(WsSubscriptionArg::requires_authentication);
self.ensure_connected(conn_type).await?;
if requires_authentication {
self.start_authentication(conn_type).await?;
self.wait_for_authentication(conn_type).await?;
}
self.send_subscribe(conn_type, args).await
}
async fn start_authentication(&self, conn_type: WsConnectionType) -> OkxResult<()> {
let credentials = self
.config
.client_config
.credentials
.clone()
.ok_or_else(|| {
OkxError::Auth("Credentials required for WebSocket authentication".into())
})?;
{
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
if connection.is_authenticated || connection.authentication_started {
return Ok(());
}
if let Some(message) = &connection.authentication_error {
return Err(OkxError::Auth(message.clone()));
}
connection.authentication_started = true;
}
let result = async {
let login_request = auth::build_login_request(&credentials)?;
let json = serde_json::to_string(&login_request)?;
let write_txs = self.write_txs.read().await;
let tx = write_txs
.get(conn_type)
.ok_or_else(|| OkxError::Ws(format!("no {conn_type} connection")))?;
tx.send(json)
.map_err(|_| OkxError::Ws("write channel closed".into()))
}
.await;
if let Err(error) = result {
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
connection.authentication_started = false;
connection.authentication_error =
Some(format!("WebSocket authentication request failed: {error}"));
self.authentication_signals.get(conn_type).notify_waiters();
return Err(error);
}
Ok(())
}
async fn wait_for_authentication(&self, conn_type: WsConnectionType) -> OkxResult<()> {
let wait = async {
loop {
let notified = self.authentication_signals.get(conn_type).notified();
{
let store = self.store.read().await;
let connection = store.get(conn_type).ok_or_else(|| {
OkxError::Ws(format!("no {conn_type} connection state"))
})?;
if connection.is_authenticated {
return Ok(());
}
if let Some(message) = &connection.authentication_error {
return Err(OkxError::Auth(message.clone()));
}
if connection.state == ConnectionState::Disconnected {
return Err(OkxError::WsConnectionLost);
}
}
notified.await;
}
};
tokio::time::timeout(self.config.client_config.request_timeout, wait)
.await
.map_err(|_| OkxError::Auth("WebSocket authentication timed out".into()))?
}
async fn connect(&self, conn_type: WsConnectionType) -> OkxResult<()> {
let receiver = {
let mut attempts = self.connection_attempts.lock().await;
if self.is_connected(conn_type).await {
return Ok(());
}
if let Some(receiver) = attempts.get(conn_type) {
receiver
} else {
let (result_tx, result_rx) = watch::channel(None);
attempts.set(conn_type, result_rx.clone());
let client = self.clone();
tokio::spawn(async move {
client.run_connection_attempt(conn_type, result_tx).await;
});
result_rx
}
};
Self::wait_for_connection_attempt(receiver).await
}
async fn is_connected(&self, conn_type: WsConnectionType) -> bool {
let store = self.store.read().await;
store.get(conn_type).is_some_and(|connection| {
matches!(
connection.state,
ConnectionState::Connected | ConnectionState::Authenticated
)
})
}
async fn run_connection_attempt(
self,
conn_type: WsConnectionType,
result_tx: watch::Sender<Option<ConnectionOutcome>>,
) {
let outcome = self
.clone()
.connect_inner(conn_type)
.await
.map_err(ConnectionFailure::from_error);
if outcome.is_err() {
self.reset_failed_connection(conn_type).await;
}
result_tx.send_replace(Some(outcome));
self.connection_attempts.lock().await.remove(conn_type);
}
async fn wait_for_connection_attempt(
mut receiver: ConnectionAttemptReceiver,
) -> OkxResult<()> {
loop {
if let Some(outcome) = receiver.borrow().clone() {
return outcome.map_err(ConnectionFailure::into_error);
}
receiver.changed().await.map_err(|_| {
OkxError::Ws("WebSocket connection attempt was cancelled".into())
})?;
}
}
async fn reset_failed_connection(&self, conn_type: WsConnectionType) {
{
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
connection.state = ConnectionState::Disconnected;
connection.is_authenticated = false;
connection.authentication_started = false;
}
self.authentication_signals.get(conn_type).notify_waiters();
self.heartbeat_stops.write().await.stop(conn_type);
self.write_txs.write().await.remove(conn_type);
}
async fn reconnect_allowed(&self, conn_type: WsConnectionType) -> bool {
let store = self.store.read().await;
store
.get(conn_type)
.is_some_and(|connection| connection.reconnect_allowed)
}
async fn mark_reconnecting(&self, conn_type: WsConnectionType) -> bool {
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
if !self.config.auto_reconnect || !connection.reconnect_allowed {
connection.state = ConnectionState::Disconnected;
return false;
}
connection.state = ConnectionState::Reconnecting;
true
}
async fn start_reconnect(&self, conn_type: WsConnectionType) {
if !self.mark_reconnecting(conn_type).await {
return;
}
let task = self.reconnect_tasks.lock().await.start(conn_type);
let Some((id, stop_rx)) = task else {
return;
};
self.prepare_subscriptions_for_reconnect(conn_type).await;
let client = self.clone();
tokio::spawn(async move {
client.run_reconnect_loop(conn_type, id, stop_rx).await;
});
}
async fn prepare_subscriptions_for_reconnect(&self, conn_type: WsConnectionType) {
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
let authenticated_topics = connection
.subscribed_topics
.iter()
.filter(|topic| topic.requires_authentication())
.cloned()
.collect::<Vec<_>>();
for topic in authenticated_topics {
connection.subscribed_topics.remove(&topic);
connection.pending_topics.insert(topic);
}
}
async fn run_reconnect_loop(
self,
conn_type: WsConnectionType,
id: u64,
mut stop_rx: watch::Receiver<bool>,
) {
loop {
if !self.mark_reconnecting(conn_type).await {
break;
}
let delay = self.config.reconnect_delay;
info!("WS {conn_type} reconnecting in {delay:?}");
tokio::select! {
_ = tokio::time::sleep(delay) => {}
result = stop_rx.changed() => {
if result.is_err() || *stop_rx.borrow() {
break;
}
}
}
if !self.reconnect_allowed(conn_type).await {
break;
}
match self.connect(conn_type).await {
Ok(()) => {
if !self.reconnect_allowed(conn_type).await {
self.reset_failed_connection(conn_type).await;
break;
}
match self.restore_subscriptions(conn_type).await {
Ok(()) => break,
Err(error) => {
error!("WS {conn_type} resubscribe failed: {error}");
self.reset_failed_connection(conn_type).await;
}
}
}
Err(error) => {
error!("WS {conn_type} reconnect failed: {error}");
}
}
}
self.reconnect_tasks.lock().await.finish(conn_type, id);
}
async fn restore_subscriptions(&self, conn_type: WsConnectionType) -> OkxResult<()> {
let (topics, needs_authentication) = {
let store = self.store.read().await;
let connection = store
.get(conn_type)
.ok_or_else(|| OkxError::Ws(format!("no {conn_type} connection state")))?;
(
connection
.subscribed_topics
.iter()
.cloned()
.collect::<Vec<_>>(),
!connection.pending_topics.is_empty(),
)
};
if !topics.is_empty() {
self.send_subscribe(conn_type, topics).await?;
}
if needs_authentication {
self.start_authentication(conn_type).await?;
}
Ok(())
}
async fn send_subscribe(
&self,
conn_type: WsConnectionType,
mut args: Vec<WsSubscriptionArg>,
) -> OkxResult<()> {
let is_authenticated = {
let store = self.store.read().await;
store
.get(conn_type)
.is_some_and(|connection| connection.is_authenticated)
};
if !is_authenticated {
let (pending, ready): (Vec<_>, Vec<_>) = args
.into_iter()
.partition(WsSubscriptionArg::requires_authentication);
args = ready;
if !pending.is_empty() {
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
connection.pending_topics.extend(pending);
}
}
if args.is_empty() {
return Ok(());
}
let req = WsSubRequest::subscribe(args);
let json = serde_json::to_string(&req)?;
let write_txs = self.write_txs.read().await;
if let Some(tx) = write_txs.get(conn_type) {
tx.send(json)
.map_err(|_| OkxError::Ws("write channel closed".into()))?;
}
let mut store = self.store.write().await;
let conn = store.get_or_create(conn_type);
for arg in req.args {
conn.subscribed_topics.insert(arg);
}
Ok(())
}
async fn send_unsubscribe(
&self,
conn_type: WsConnectionType,
args: Vec<WsSubscriptionArg>,
) -> OkxResult<()> {
let req = WsSubRequest::unsubscribe(args);
let json = serde_json::to_string(&req)?;
let write_txs = self.write_txs.read().await;
if let Some(tx) = write_txs.get(conn_type) {
tx.send(json)
.map_err(|_| OkxError::Ws("write channel closed".into()))?;
}
let mut store = self.store.write().await;
let conn = store.get_or_create(conn_type);
for arg in &req.args {
conn.subscribed_topics.remove(arg);
}
Ok(())
}
fn connect_inner(self, conn_type: WsConnectionType) -> BoxFuture<'static, OkxResult<()>> {
Box::pin(async move {
let url = self.config.ws_url(conn_type).to_owned();
info!("Connecting WS {conn_type} to {url}");
{
let mut store = self.store.write().await;
let connection = store.get_or_create(conn_type);
connection.state = ConnectionState::Connecting;
connection.is_authenticated = false;
connection.authentication_started = false;
connection.authentication_error = None;
}
let ws = connection::connect(&url).await?;
let (write_tx, mut msg_rx, connection_event_tx) =
connection::spawn_io_tasks(ws, conn_type);
let (hb_stop_tx, hb_stop_rx) = watch::channel(false);
let (pong_tx, pong_rx) = mpsc::unbounded_channel();
let hb_tx = write_tx.clone();
let ping_interval = self.config.ping_interval;
let pong_timeout = self.config.pong_timeout;
tokio::spawn(async move {
heartbeat::heartbeat_loop(
hb_tx,
ping_interval,
pong_timeout,
pong_rx,
connection_event_tx,
conn_type,
hb_stop_rx,
)
.await;
});
{
let mut write_txs = self.write_txs.write().await;
write_txs.set(conn_type, write_tx.clone());
}
{
let mut heartbeat_stops = self.heartbeat_stops.write().await;
heartbeat_stops.set(conn_type, hb_stop_tx.clone());
}
let event_tx = self.event_tx.clone();
let client_for_reconnect = self.clone();
let store = self.store.clone();
let pending_requests = self.pending_requests.clone();
let authentication_signals = self.authentication_signals.clone();
let write_txs = self.write_txs.clone();
tokio::spawn(async move {
while let Some(msg) = msg_rx.recv().await {
match &msg {
WsMessage::Event(evt) if evt.event == "login" => {
if evt.code.as_deref() == Some("0") {
info!("WS {conn_type} authenticated");
let mut s = store.write().await;
let conn = s.get_or_create(conn_type);
conn.is_authenticated = true;
conn.authentication_started = false;
conn.authentication_error = None;
conn.state = ConnectionState::Authenticated;
authentication_signals.get(conn_type).notify_waiters();
let pending: Vec<_> = conn.pending_topics.drain().collect();
if !pending.is_empty() {
let req = WsSubRequest::subscribe(pending);
if let Ok(json) = serde_json::to_string(&req) {
let wt = write_txs.read().await;
if let Some(tx) = wt.get(conn_type) {
let _ = tx.send(json);
}
}
let conn = s.get_or_create(conn_type);
for topic in req.args {
conn.subscribed_topics.insert(topic);
}
}
} else {
error!("WS {conn_type} login failed: {:?}", evt.msg);
let mut s = store.write().await;
let conn = s.get_or_create(conn_type);
conn.authentication_started = false;
conn.authentication_error = Some(
evt.msg
.clone()
.unwrap_or_else(|| "WebSocket authentication failed".into()),
);
authentication_signals.get(conn_type).notify_waiters();
}
}
WsMessage::ApiResponse(resp) => {
let mut pending = pending_requests.lock().await;
pending.resolve(&resp.id, resp.clone());
}
WsMessage::Pong => {
let _ = pong_tx.send(());
}
WsMessage::Disconnected(_) => {
warn!("WS {conn_type} disconnected");
let _ = hb_stop_tx.send(true);
let should_reconnect = {
let mut s = store.write().await;
let conn = s.get_or_create(conn_type);
let should_reconnect = conn.reconnect_allowed
&& conn.state != ConnectionState::Disconnected
&& client_for_reconnect.config.auto_reconnect;
conn.state = if should_reconnect {
ConnectionState::Reconnecting
} else {
ConnectionState::Disconnected
};
conn.is_authenticated = false;
conn.authentication_started = false;
should_reconnect
};
authentication_signals.get(conn_type).notify_waiters();
{
let mut pending = pending_requests.lock().await;
pending.reject_connection(conn_type);
}
{
let mut wt = write_txs.write().await;
wt.remove(conn_type);
}
if should_reconnect {
client_for_reconnect.start_reconnect(conn_type).await;
}
break;
}
_ => {}
}
let _ = event_tx.send(msg);
}
let _ = hb_stop_tx.send(true);
});
{
let mut s = self.store.write().await;
let connection = s.get_or_create(conn_type);
if !connection.reconnect_allowed {
return Err(OkxError::Ws("WebSocket connection was closed".into()));
}
connection.state = ConnectionState::Connected;
}
if conn_type != WsConnectionType::Public
&& self.config.client_config.credentials.is_some()
{
self.start_authentication(conn_type).await?;
}
let _ = self.event_tx.send(WsMessage::Connected(conn_type));
info!("WS {conn_type} connected");
Ok(())
})
}
pub async fn close_all(&self) {
{
let mut store = self.store.write().await;
if let Some(conn) = &mut store.public {
conn.state = ConnectionState::Disconnected;
conn.is_authenticated = false;
conn.authentication_started = false;
conn.reconnect_allowed = false;
}
if let Some(conn) = &mut store.private {
conn.state = ConnectionState::Disconnected;
conn.is_authenticated = false;
conn.authentication_started = false;
conn.reconnect_allowed = false;
}
if let Some(conn) = &mut store.business {
conn.state = ConnectionState::Disconnected;
conn.is_authenticated = false;
conn.authentication_started = false;
conn.reconnect_allowed = false;
}
}
for conn_type in [
WsConnectionType::Public,
WsConnectionType::Private,
WsConnectionType::Business,
] {
self.authentication_signals.get(conn_type).notify_waiters();
}
{
let mut pending = self.pending_requests.lock().await;
pending.reject_connection(WsConnectionType::Private);
pending.reject_connection(WsConnectionType::Business);
}
self.reconnect_tasks.lock().await.stop_all();
self.heartbeat_stops.write().await.stop_all();
let mut write_txs = self.write_txs.write().await;
write_txs.public = None;
write_txs.private = None;
write_txs.business = None;
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use futures_util::{SinkExt, StreamExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{oneshot, Barrier};
use tokio_tungstenite::tungstenite::Message as TungsteniteMessage;
use tokio_tungstenite::{accept_async, WebSocketStream};
use super::*;
use crate::config::ClientConfigBuilder;
const CONCURRENT_CONNECTIONS: usize = 8;
async fn receive_json(websocket: &mut WebSocketStream<TcpStream>) -> serde_json::Value {
loop {
let message = tokio::time::timeout(Duration::from_secs(1), websocket.next())
.await
.expect("WebSocket message should arrive")
.expect("WebSocket should remain open")
.expect("WebSocket message should be valid");
if let TungsteniteMessage::Text(text) = message {
return serde_json::from_str(&text).expect("message should contain valid JSON");
}
}
}
async fn wait_for_reconnect_task(client: &WebsocketClient, conn_type: WsConnectionType) {
tokio::time::timeout(Duration::from_secs(1), async {
loop {
if client.reconnect_tasks.lock().await.get(conn_type).is_some() {
return;
}
tokio::task::yield_now().await;
}
})
.await
.expect("reconnect task should start");
}
fn api_test_config(request_timeout: Duration) -> WsConfig {
let client_config = ClientConfigBuilder::new()
.credentials("key", "secret", "passphrase")
.request_timeout(request_timeout)
.build();
WsConfig::new(client_config)
}
async fn set_authenticated_connection(
client: &WebsocketClient,
conn_type: WsConnectionType,
write_tx: mpsc::UnboundedSender<String>,
) {
client.write_txs.write().await.set(conn_type, write_tx);
let mut store = client.store.write().await;
let connection = store.get_or_create(conn_type);
connection.state = ConnectionState::Authenticated;
connection.is_authenticated = true;
}
#[tokio::test]
async fn api_request_send_failure_removes_pending_request() {
let client = WebsocketClient::new(api_test_config(Duration::from_secs(1)));
let (write_tx, write_rx) = mpsc::unbounded_channel();
drop(write_rx);
set_authenticated_connection(&client, WsConnectionType::Private, write_tx).await;
let error = client
.send_api_request("order", Vec::new())
.await
.expect_err("request should fail");
assert!(matches!(error, OkxError::Ws(message) if message == "write channel closed"));
assert_eq!(client.pending_requests.lock().await.pending_count(), 0);
}
#[tokio::test]
async fn api_request_timeout_removes_pending_request() {
let client = WebsocketClient::new(api_test_config(Duration::from_millis(10)));
let (write_tx, _write_rx) = mpsc::unbounded_channel();
set_authenticated_connection(&client, WsConnectionType::Private, write_tx).await;
let error = client
.send_api_request("order", Vec::new())
.await
.expect_err("request should time out");
assert!(matches!(
error,
OkxError::WsApiTimeout { operation, .. } if operation == "order"
));
assert_eq!(client.pending_requests.lock().await.pending_count(), 0);
}
#[tokio::test]
async fn api_response_removes_pending_request() {
let client = WebsocketClient::new(api_test_config(Duration::from_secs(1)));
let (write_tx, mut write_rx) = mpsc::unbounded_channel();
set_authenticated_connection(&client, WsConnectionType::Private, write_tx).await;
let request_task = {
let client = client.clone();
tokio::spawn(async move { client.send_api_request("order", Vec::new()).await })
};
let request: serde_json::Value =
serde_json::from_str(&write_rx.recv().await.expect("request should be written"))
.expect("request should contain valid JSON");
let id = request["id"]
.as_str()
.expect("request should have an ID")
.to_string();
assert_eq!(client.pending_requests.lock().await.pending_count(), 1);
client.pending_requests.lock().await.resolve(
&id,
crate::types::ws::events::WsApiResponse {
id: id.clone(),
op: "order".into(),
code: "0".into(),
msg: String::new(),
data: Vec::new(),
in_time: None,
out_time: None,
},
);
let response = request_task
.await
.expect("request task should complete")
.expect("request should succeed");
assert_eq!(response.id, id);
assert_eq!(client.pending_requests.lock().await.pending_count(), 0);
}
#[test]
fn reconnect_tasks_start_once_per_connection() {
let mut tasks = ReconnectTasks::default();
let (id, _) = tasks
.start(WsConnectionType::Public)
.expect("first reconnect task should start");
assert!(tasks.start(WsConnectionType::Public).is_none());
assert!(tasks.start(WsConnectionType::Private).is_some());
tasks.finish(WsConnectionType::Public, id.wrapping_add(1));
assert!(tasks.get(WsConnectionType::Public).is_some());
tasks.finish(WsConnectionType::Public, id);
assert!(tasks.get(WsConnectionType::Public).is_none());
}
#[tokio::test]
async fn reconnect_retries_and_restores_public_subscriptions() {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have an address");
let accept_count = Arc::new(AtomicUsize::new(0));
let server_accept_count = accept_count.clone();
let (recovered_tx, recovered_rx) = oneshot::channel();
let (release_tx, release_rx) = oneshot::channel();
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await.expect("connection should arrive");
server_accept_count.fetch_add(1, Ordering::SeqCst);
let mut websocket = accept_async(stream)
.await
.expect("WebSocket handshake should succeed");
let initial_request = receive_json(&mut websocket).await;
websocket
.close(None)
.await
.expect("connection should close");
for _ in 0..2 {
let (stream, _) = listener.accept().await.expect("retry should arrive");
server_accept_count.fetch_add(1, Ordering::SeqCst);
drop(stream);
}
let (stream, _) = listener.accept().await.expect("reconnect should arrive");
server_accept_count.fetch_add(1, Ordering::SeqCst);
let mut websocket = accept_async(stream)
.await
.expect("WebSocket handshake should succeed");
let recovered_request = receive_json(&mut websocket).await;
recovered_tx
.send((initial_request, recovered_request))
.expect("test should receive requests");
let _ = release_rx.await;
let _ = websocket.close(None).await;
});
let mut config = WsConfig::default().with_url_override(format!("ws://{address}"));
config.reconnect_delay = Duration::from_millis(10);
config.ping_interval = Duration::from_secs(60);
let client = WebsocketClient::new(config);
client
.subscribe(vec![WsSubscriptionArg::with_inst_id("tickers", "BTC-USDT")])
.await
.expect("subscription should succeed");
let (initial_request, recovered_request) =
tokio::time::timeout(Duration::from_secs(2), recovered_rx)
.await
.expect("reconnect should complete")
.expect("server should send requests");
assert_eq!(initial_request["op"], "subscribe");
assert_eq!(recovered_request, initial_request);
assert_eq!(accept_count.load(Ordering::SeqCst), 4);
client.close_all().await;
release_tx.send(()).expect("server should still be running");
server.await.expect("server should complete");
}
#[tokio::test]
async fn reconnect_authenticates_before_restoring_private_subscriptions() {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have an address");
let (recovered_tx, recovered_rx) = oneshot::channel();
let (release_tx, release_rx) = oneshot::channel();
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await.expect("connection should arrive");
let mut websocket = accept_async(stream)
.await
.expect("WebSocket handshake should succeed");
let initial_login = receive_json(&mut websocket).await;
websocket
.send(TungsteniteMessage::Text(
r#"{"event":"login","code":"0","msg":""}"#.into(),
))
.await
.expect("login response should send");
let initial_subscription = receive_json(&mut websocket).await;
websocket
.close(None)
.await
.expect("connection should close");
let (stream, _) = listener.accept().await.expect("reconnect should arrive");
let mut websocket = accept_async(stream)
.await
.expect("WebSocket handshake should succeed");
let reconnect_login = receive_json(&mut websocket).await;
assert!(
tokio::time::timeout(Duration::from_millis(30), websocket.next())
.await
.is_err()
);
websocket
.send(TungsteniteMessage::Text(
r#"{"event":"login","code":"0","msg":""}"#.into(),
))
.await
.expect("login response should send");
let recovered_subscription = receive_json(&mut websocket).await;
recovered_tx
.send((
initial_login,
initial_subscription,
reconnect_login,
recovered_subscription,
))
.expect("test should receive requests");
let _ = release_rx.await;
let _ = websocket.close(None).await;
});
let client_config = ClientConfigBuilder::new()
.credentials("key", "secret", "passphrase")
.build();
let mut config = WsConfig::new(client_config).with_url_override(format!("ws://{address}"));
config.reconnect_delay = Duration::from_millis(10);
config.ping_interval = Duration::from_secs(60);
let client = WebsocketClient::new(config);
client
.subscribe(vec![WsSubscriptionArg::channel_only("account")])
.await
.expect("subscription should succeed");
let requests = tokio::time::timeout(Duration::from_secs(2), recovered_rx)
.await
.expect("reconnect should complete")
.expect("server should send requests");
assert_eq!(requests.0["op"], "login");
assert_eq!(requests.1["op"], "subscribe");
assert_eq!(requests.2["op"], "login");
assert_eq!(requests.3, requests.1);
client.close_all().await;
release_tx.send(()).expect("server should still be running");
server.await.expect("server should complete");
}
#[tokio::test]
async fn close_all_stops_pending_reconnect() {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have an address");
let (connected_tx, connected_rx) = oneshot::channel();
let (disconnect_tx, disconnect_rx) = oneshot::channel();
let (result_tx, result_rx) = oneshot::channel();
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await.expect("connection should arrive");
let mut websocket = accept_async(stream)
.await
.expect("WebSocket handshake should succeed");
connected_tx
.send(())
.expect("test should receive connection");
disconnect_rx.await.expect("disconnect should be requested");
websocket
.close(None)
.await
.expect("connection should close");
let reconnected = tokio::time::timeout(Duration::from_millis(300), listener.accept())
.await
.is_ok();
result_tx
.send(reconnected)
.expect("test should receive reconnect result");
});
let mut config = WsConfig::default().with_url_override(format!("ws://{address}"));
config.reconnect_delay = Duration::from_millis(150);
config.ping_interval = Duration::from_secs(60);
let client = WebsocketClient::new(config);
client
.ensure_connected(WsConnectionType::Public)
.await
.expect("connection should succeed");
connected_rx.await.expect("server should accept connection");
disconnect_tx
.send(())
.expect("server should still be running");
wait_for_reconnect_task(&client, WsConnectionType::Public).await;
client.close_all().await;
assert!(!result_rx
.await
.expect("server should report reconnect result"));
server.await.expect("server should complete");
}
#[tokio::test]
async fn concurrent_connection_callers_share_success() {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have an address");
let accept_count = Arc::new(AtomicUsize::new(0));
let server_accept_count = accept_count.clone();
let (release_tx, release_rx) = oneshot::channel();
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await.expect("connection should arrive");
server_accept_count.fetch_add(1, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(50)).await;
let mut websocket = accept_async(stream)
.await
.expect("WebSocket handshake should succeed");
let _ = release_rx.await;
websocket
.close(None)
.await
.expect("connection should close");
});
let mut config = WsConfig::default().with_url_override(format!("ws://{address}"));
config.auto_reconnect = false;
let client = WebsocketClient::new(config);
let barrier = Arc::new(Barrier::new(CONCURRENT_CONNECTIONS + 1));
let mut callers = Vec::new();
for _ in 0..CONCURRENT_CONNECTIONS {
let client = client.clone();
let barrier = barrier.clone();
callers.push(tokio::spawn(async move {
barrier.wait().await;
client.ensure_connected(WsConnectionType::Public).await
}));
}
barrier.wait().await;
for caller in callers {
caller
.await
.expect("caller should complete")
.expect("connection should succeed");
}
assert_eq!(accept_count.load(Ordering::SeqCst), 1);
assert!(client.is_connected(WsConnectionType::Public).await);
client.close_all().await;
release_tx.send(()).expect("server should still be running");
server.await.expect("server should complete");
}
#[tokio::test]
async fn concurrent_connection_callers_share_failure() {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have an address");
let accept_count = Arc::new(AtomicUsize::new(0));
let server_accept_count = accept_count.clone();
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await.expect("connection should arrive");
server_accept_count.fetch_add(1, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(50)).await;
drop(stream);
});
let mut config = WsConfig::default().with_url_override(format!("ws://{address}"));
config.auto_reconnect = false;
let client = WebsocketClient::new(config);
let barrier = Arc::new(Barrier::new(CONCURRENT_CONNECTIONS + 1));
let mut callers = Vec::new();
for _ in 0..CONCURRENT_CONNECTIONS {
let client = client.clone();
let barrier = barrier.clone();
callers.push(tokio::spawn(async move {
barrier.wait().await;
client.ensure_connected(WsConnectionType::Public).await
}));
}
barrier.wait().await;
let mut errors = Vec::new();
for caller in callers {
let error = caller
.await
.expect("caller should complete")
.expect_err("connection should fail");
errors.push(error.to_string());
}
server.await.expect("server should complete");
assert_eq!(accept_count.load(Ordering::SeqCst), 1);
assert!(errors.windows(2).all(|pair| pair[0] == pair[1]));
let store = client.store.read().await;
assert_eq!(
store
.get(WsConnectionType::Public)
.expect("connection state should exist")
.state,
ConnectionState::Disconnected
);
}
#[test]
fn heartbeat_stops_signal_all_connections() {
let (public_tx, public_rx) = watch::channel(false);
let (private_tx, private_rx) = watch::channel(false);
let (business_tx, business_rx) = watch::channel(false);
let mut stops = HeartbeatStops::default();
stops.set(WsConnectionType::Public, public_tx);
stops.set(WsConnectionType::Private, private_tx);
stops.set(WsConnectionType::Business, business_tx);
stops.stop_all();
assert!(*public_rx.borrow());
assert!(*private_rx.borrow());
assert!(*business_rx.borrow());
assert!(stops.public.is_none());
assert!(stops.private.is_none());
assert!(stops.business.is_none());
}
#[tokio::test]
async fn api_request_requires_credentials_before_connecting() {
let client = WebsocketClient::new(WsConfig::default());
let error = client
.send_api_request("order", Vec::new())
.await
.expect_err("request should fail");
assert!(matches!(error, OkxError::Auth(_)));
assert!(client.store.read().await.private.is_none());
}
#[tokio::test]
async fn authenticated_subscription_requires_credentials_before_connecting() {
let cases = [
("books-l2-tbt", WsConnectionType::Public),
("orders-algo", WsConnectionType::Business),
("account", WsConnectionType::Private),
];
for (channel, conn_type) in cases {
let client = WebsocketClient::new(WsConfig::default());
let error = client
.subscribe(vec![WsSubscriptionArg::channel_only(channel)])
.await
.expect_err("subscription should fail");
assert!(matches!(error, OkxError::Auth(_)));
assert!(client.store.read().await.get(conn_type).is_none());
}
}
#[tokio::test]
async fn public_connection_authentication_starts_once() {
let config = ClientConfigBuilder::new()
.credentials("key", "secret", "passphrase")
.build();
let client = WebsocketClient::new(WsConfig::new(config));
let (write_tx, mut write_rx) = tokio::sync::mpsc::unbounded_channel();
client
.write_txs
.write()
.await
.set(WsConnectionType::Public, write_tx);
client
.store
.write()
.await
.get_or_create(WsConnectionType::Public)
.state = ConnectionState::Connected;
client
.start_authentication(WsConnectionType::Public)
.await
.expect("authentication should start");
client
.start_authentication(WsConnectionType::Public)
.await
.expect("authentication should remain in progress");
let request: serde_json::Value = serde_json::from_str(
&write_rx
.try_recv()
.expect("login request should be written"),
)
.expect("login request should be valid JSON");
assert_eq!(request["op"], "login");
assert!(write_rx.try_recv().is_err());
}
#[tokio::test]
async fn business_public_subscription_does_not_wait_for_authentication() {
let client = WebsocketClient::new(WsConfig::default());
let (write_tx, mut write_rx) = tokio::sync::mpsc::unbounded_channel();
client
.write_txs
.write()
.await
.set(WsConnectionType::Business, write_tx);
client
.store
.write()
.await
.get_or_create(WsConnectionType::Business)
.state = ConnectionState::Connected;
client
.send_subscribe(
WsConnectionType::Business,
vec![WsSubscriptionArg::channel_only("candle1m")],
)
.await
.expect("subscription should be written");
let request: serde_json::Value =
serde_json::from_str(&write_rx.try_recv().expect("subscription should be written"))
.expect("subscription should be valid JSON");
assert_eq!(request["args"][0]["channel"], "candle1m");
}
#[tokio::test]
async fn authentication_waiter_completes_after_login() {
let client = WebsocketClient::new(WsConfig::default());
{
let mut store = client.store.write().await;
store.get_or_create(WsConnectionType::Private).state = ConnectionState::Connected;
}
let waiter = {
let client = client.clone();
tokio::spawn(async move {
client
.wait_for_authentication(WsConnectionType::Private)
.await
})
};
{
let mut store = client.store.write().await;
let connection = store.get_or_create(WsConnectionType::Private);
connection.is_authenticated = true;
connection.state = ConnectionState::Authenticated;
}
client
.authentication_signals
.get(WsConnectionType::Private)
.notify_waiters();
waiter
.await
.expect("waiter should complete")
.expect("authentication should succeed");
}
#[tokio::test]
async fn authentication_waiter_times_out() {
let config = ClientConfigBuilder::new()
.request_timeout(Duration::from_millis(10))
.build();
let client = WebsocketClient::new(WsConfig::new(config));
{
let mut store = client.store.write().await;
store.get_or_create(WsConnectionType::Private).state = ConnectionState::Connected;
}
let error = client
.wait_for_authentication(WsConnectionType::Private)
.await
.expect_err("authentication should time out");
assert!(matches!(error, OkxError::Auth(message) if message.contains("timed out")));
}
}