Skip to content
Open
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
16 changes: 16 additions & 0 deletions crates/rmcp/src/transport/common/client_side_sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ impl<E: std::error::Error + Send> SseStreamReconnect for NeverReconnect<E> {
}
}

/// Controls whether an SSE reconnection failure should follow the retry policy or stop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReconnectErrorDisposition {
Retry,
Terminate,
}

/// Abstraction for SSE reconnection logic. Implementors can hook into
/// [`handle_control_event`](Self::handle_control_event) to consume control
/// frames (e.g. `event: endpoint`) that arrive when a server restarts an SSE
Expand All @@ -278,6 +285,9 @@ pub(crate) trait SseStreamReconnect {
type Error: std::error::Error;
type Future: Future<Output = Result<BoxedSseResponse, Self::Error>> + Send;
fn retry_connection(&mut self, last_event_id: Option<&str>) -> Self::Future;
fn classify_reconnect_error(&self, _error: &Self::Error) -> ReconnectErrorDisposition {
ReconnectErrorDisposition::Retry
}
fn handle_control_event(&mut self, _event: &Sse) -> Result<(), Self::Error> {
Ok(())
}
Expand Down Expand Up @@ -503,6 +513,12 @@ where
}
Err(e) => {
tracing::debug!("retry sse stream error: {e}");
if this.connector.classify_reconnect_error(&e)
== ReconnectErrorDisposition::Terminate
{
this.state.set(SseAutoReconnectStreamState::Terminated);
return Poll::Ready(Some(Err(e)));
}
*retry_times += 1;
if let Some(interval) = this.retry_policy.retry(*retry_times) {
let interval = this
Expand Down
10 changes: 9 additions & 1 deletion crates/rmcp/src/transport/streamable_http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use tokio_util::sync::CancellationToken;
use tracing::debug;

use super::common::client_side_sse::{
DEFAULT_MAX_SSE_EVENT_SIZE, ExponentialBackoff, SseRetryPolicy, SseStreamReconnect,
DEFAULT_MAX_SSE_EVENT_SIZE, ExponentialBackoff, ReconnectErrorDisposition, SseRetryPolicy,
SseStreamReconnect,
};
use crate::{
RoleClient,
Expand Down Expand Up @@ -544,6 +545,13 @@ impl<C: StreamableHttpClient> SseStreamReconnect for StreamableHttpClientReconne
})
}

fn classify_reconnect_error(&self, error: &Self::Error) -> ReconnectErrorDisposition {
match error {
StreamableHttpError::ServerDoesNotSupportSse => ReconnectErrorDisposition::Terminate,
_ => ReconnectErrorDisposition::Retry,
}
}

fn map_fatal_stream_error(&mut self, error: SseError) -> Option<Self::Error> {
Some(StreamableHttpError::Sse(error))
}
Expand Down
Loading
Loading