mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Enforce a minimum SSE reconnect wait to avoid a busy loop (#2592)
SSEClient::wait_for_reconnect() sleeps in 100ms steps until the reconnect interval has elapsed. With an interval of 0 (for example "retry: 0" from the server, or set_reconnect_interval(0)) it never slept at all, so a server that sends "retry: 0" and closes the stream made the client reconnect in a tight loop. set_max_reconnect_attempts() does not stop this either, because each successful connection resets the attempt counter. Always wait at least one step (100ms). Intervals of 1-99ms already waited 100ms because of the step size, so only 0 and negative values change behavior.
This commit is contained in:
1 parent
3330d0eb06
commit
0715c2739e
3 files changed
+12
-5
No files matched your search
@@ -5051,11 +5051,15 @@ inline bool SSEClient::should_reconnect(int count) const {
|
||||
}
|
||||
|
||||
inline void SSEClient::wait_for_reconnect() {
|
||||
// Use small increments to check running_ flag frequently
|
||||
// Use small increments to check running_ flag frequently.
|
||||
// Always wait at least one increment, so that a zero interval (e.g.
|
||||
// "retry: 0" from the server) cannot cause a busy reconnect loop.
|
||||
const auto step_ms = 100;
|
||||
auto interval_ms = (std::max)(reconnect_interval_ms_, step_ms);
|
||||
auto waited = 0;
|
||||
while (running_.load() && waited < reconnect_interval_ms_) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
waited += 100;
|
||||
while (running_.load() && waited < interval_ms) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(step_ms));
|
||||
waited += step_ms;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user