Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 11.0.0 - 2025-07-29

### Changed (1)

- Enhanced WebSocket reconnection logic with intelligent error classification.

## 10.0.0 - 2025-07-23

**Spot**
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "binance-sdk"
version = "10.0.0"
version = "11.0.0"
authors = [ "Binance" ]
edition = "2024"
resolver = "3"
Expand Down
88 changes: 88 additions & 0 deletions src/common/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,93 @@
use thiserror::Error;

/// Represents different types of WebSocket connection failures and their reconnection eligibility
#[derive(Debug, Clone, Copy)]
pub enum WebsocketConnectionFailureReason {
/// Network-level interruption (should reconnect)
NetworkInterruption,
/// Connection reset by peer (should reconnect)
ConnectionReset,
/// Server temporary error (should reconnect)
ServerTemporaryError,
/// Unexpected connection close (should reconnect)
UnexpectedClose,
/// Stream ended unexpectedly (should reconnect)
StreamEnded,
/// Authentication/authorization failure (should not reconnect)
AuthenticationFailure,
/// Protocol violation (should not reconnect)
ProtocolViolation,
/// Configuration error (should not reconnect)
ConfigurationError,
/// User initiated close (should not reconnect)
UserInitiatedClose,
/// Permanent server error (should not reconnect)
PermanentServerError,
/// Normal close (should not reconnect)
NormalClose,
}

impl WebsocketConnectionFailureReason {
/// Classifies a tungstenite error into a failure reason
pub fn from_tungstenite_error(error: &tokio_tungstenite::tungstenite::Error) -> Self {
use tokio_tungstenite::tungstenite::Error;
match error {
Error::ConnectionClosed | Error::AlreadyClosed => Self::ConnectionReset,
Error::Io(io_error) => {
use std::io::ErrorKind;
match io_error.kind() {
ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted => {
Self::ConnectionReset
}
ErrorKind::UnexpectedEof => Self::StreamEnded,
ErrorKind::PermissionDenied => Self::AuthenticationFailure,
_ => Self::NetworkInterruption,
}
}
Error::Tls(_) | Error::Capacity(_) | Error::Url(_) => Self::ConfigurationError,
Error::Protocol(_) | Error::Utf8 | Error::HttpFormat(_) => Self::ProtocolViolation,
Error::Http(_) => Self::ServerTemporaryError,
_ => Self::NetworkInterruption,
}
}

/// Classifies a WebSocket close code into a failure reason
#[must_use]
pub fn from_close_code(code: u16, user_initiated: bool) -> Self {
if user_initiated {
return Self::UserInitiatedClose;
}
match code {
1000 => Self::NormalClose, // Normal closure
1001 | 1011 | 1012 | 1013 => Self::ServerTemporaryError, // Server temporary issues
1002 | 1003 | 1007 | 1009 | 1010 => Self::ProtocolViolation, // Protocol violations
1008 | 1014 | 4000..=4999 => Self::PermanentServerError, // Permanent server errors
1015 => Self::ConfigurationError, // TLS handshake failure
_ => Self::UnexpectedClose, // Unknown codes including 1006
}
}

/// Determines if this failure type warrants a reconnection attempt
#[must_use]
pub fn should_reconnect(&self) -> bool {
match self {
// Reconnectable failures
Self::NetworkInterruption
| Self::ConnectionReset
| Self::ServerTemporaryError
| Self::UnexpectedClose
| Self::StreamEnded => true,
// Non-reconnectable failures
Self::AuthenticationFailure
| Self::ProtocolViolation
| Self::ConfigurationError
| Self::UserInitiatedClose
| Self::PermanentServerError
| Self::NormalClose => false,
}
}
}

#[derive(Error, Debug)]
pub enum ConnectorError {
#[error("Connector client error: {0}")]
Expand Down
6 changes: 2 additions & 4 deletions src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ where
original.starts_with('/') && PLACEHOLDER_RE.find(body).is_some_and(|m| m.start() == 0);

// Lowercase only that first placeholder's value
let result = if should_lower_head {
if should_lower_head {
if let Some(caps) = PLACEHOLDER_RE.captures(body) {
let key = normalize_ws_streams_key(caps.get(2).unwrap().as_str());
let first_val = normalized.get(&key).cloned().unwrap_or_default();
Expand All @@ -960,9 +960,7 @@ where
}
} else {
stripped.clone()
};

result
}
}

/// Builds a WebSocket API message with optional authentication and signature generation.
Expand Down
Loading