Merge pull request #430 from CopernicaMarketingSoftware/timeout-issues

Timeout issues
This commit is contained in:
Emiel Bruijntjes 2021-09-20 14:29:01 +02:00 committed by GitHub
commit 24fcbc7248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -255,8 +255,9 @@ private:
// the server was inactive for a too long period of time, reset state // the server was inactive for a too long period of time, reset state
_next = _expire = 0.0; _timeout = 0; _next = _expire = 0.0; _timeout = 0;
// close the connection because server was inactive // close the connection because server was inactive (we close it with immediate effect,
return (void)_connection->close(); // because it was inactive so we cannot trust it to respect the AMQP close handshake)
return (void)_connection->close(true);
} }
else if (now >= _next) else if (now >= _next)
{ {
@ -365,8 +366,16 @@ private:
// because the server has just sent us some data, we will update the expire time too // because the server has just sent us some data, we will update the expire time too
_expire = now + _timeout * 1.5; _expire = now + _timeout * 1.5;
// stop the existing timer (we have to stop it and restart it, because ev_timer_set()
// on its own does not change the running timer) (note that we assume that the timer
// is already running and keeps on running, so no calls to ev_ref()/en_unref() here)
ev_timer_stop(_loop, &_timer);
// find the earliest thing that expires // find the earliest thing that expires
ev_timer_set(&_timer, std::min(_next, _expire) - now, 0.0); ev_timer_set(&_timer, std::min(_next, _expire) - now, 0.0);
// and start it again
ev_timer_start(_loop, &_timer);
// expose the accepted interval // expose the accepted interval
return _timeout; return _timeout;

View File

@ -120,6 +120,12 @@ private:
// turn socket into a non-blocking socket and set the close-on-exec bit // turn socket into a non-blocking socket and set the close-on-exec bit
fcntl(_socket, F_SETFL, O_NONBLOCK | O_CLOEXEC); fcntl(_socket, F_SETFL, O_NONBLOCK | O_CLOEXEC);
// we set the 'keepalive' option so that we automatically detect if the peer is dead
int keepalive = 1;
// set the keepalive option
setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive));
// try to connect non-blocking // try to connect non-blocking
if (connect(_socket, addresses[i]->ai_addr, addresses[i]->ai_addrlen) == 0) break; if (connect(_socket, addresses[i]->ai_addr, addresses[i]->ai_addrlen) == 0) break;