Skip to content

Commit 9a91e67

Browse files
authored
fix: Reconnect should use both reconnect and retry_initial for reconnection (#99)
1 parent c7433f8 commit 9a91e67

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

eventsource-client/src/client.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ pub struct ReconnectingRequest<C> {
337337
redirect_count: u32,
338338
event_parser: EventParser,
339339
last_event_id: Option<String>,
340+
#[pin]
341+
initial_connection: bool,
340342
}
341343

342344
impl<C> ReconnectingRequest<C> {
@@ -364,6 +366,7 @@ impl<C> ReconnectingRequest<C> {
364366
current_url: url,
365367
event_parser: EventParser::new(),
366368
last_event_id,
369+
initial_connection: true,
367370
}
368371
}
369372

@@ -454,7 +457,11 @@ where
454457
*self.as_mut().project().event_parser = EventParser::new();
455458
match self.send_request() {
456459
Ok(resp) => {
457-
let retry = self.props.reconnect_opts.retry_initial;
460+
let retry = if self.initial_connection {
461+
self.props.reconnect_opts.retry_initial
462+
} else {
463+
self.props.reconnect_opts.reconnect
464+
};
458465
self.as_mut()
459466
.project()
460467
.state
@@ -483,6 +490,7 @@ where
483490
.project()
484491
.state
485492
.set(State::Connected(resp.into_body()));
493+
self.as_mut().project().initial_connection.set(false);
486494

487495
return Poll::Ready(Some(Ok(SSE::Connected(ConnectionDetails::new(
488496
Response::new(status, headers),
@@ -518,8 +526,7 @@ where
518526
))));
519527
}
520528
Err(e) => {
521-
// This seems basically impossible. AFAIK we can only get this way if we
522-
// poll after it was already ready
529+
// This happens when the server is unreachable, e.g. connection refused.
523530
warn!("request returned an error: {}", e);
524531
if !*retry {
525532
self.as_mut().project().state.set(State::New);

0 commit comments

Comments
 (0)