Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
zeromqsend plugin: improve connection timeout resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
waht committed Aug 18, 2018
1 parent ffb88ad commit a773bdf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/plugins/zeromqsend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This plugin supports the following configuration options when mounting:
[`ipc`](http://api.zeromq.org/4-2:zmq-ipc) and
[`tcp`](http://api.zeromq.org/4-2:zmq-tcp) ZeroMQ transports are recommended.
The default value is "tcp://localhost:6000".
- **connectTimeout**: Timeout for establishing connections in seconds. The default value is "2".
- **connectTimeout**: Timeout for establishing connections in miliseconds. The default value is "1000".
- **subscribeTimeout**: Timeout for waiting for subscribers in miliseconds. The default value is "200".

# Notification Format
Expand Down
30 changes: 28 additions & 2 deletions src/plugins/zeromqsend/publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,23 @@ static struct timespec ts_diff (struct timespec now, struct timespec start)
*/
static int waitForConnection (void * monitorSocket, long connectTimeout)
{
time_t start = time (NULL);
struct timespec wait;
struct timespec start;
struct timespec now;
struct timespec diff;
time_t startFallback = -1;
long timeoutSec = (connectTimeout / (1000));
long timeoutNsec = (connectTimeout % (1000)) * (1000 * 1000);
if (clock_gettime (CLOCK_MONOTONIC, &start) == -1)
{
ELEKTRA_LOG_WARNING ("Using slower fallback for timeout detection");
startFallback = time (NULL);
// minimum timeout is 1 second when using the fallback
if (timeoutSec == 0)
{
timeoutSec = 1;
}
}

// wait for connection established event
int connected = 0;
Expand All @@ -107,7 +122,18 @@ static int waitForConnection (void * monitorSocket, long connectTimeout)

int event = getMonitorEvent (monitorSocket);

if (time (NULL) - start > connectTimeout)
int timeout = 0;
if (startFallback == -1)
{
clock_gettime (CLOCK_MONOTONIC, &now);
diff = ts_diff (now, start);
timeout = diff.tv_sec >= timeoutSec && diff.tv_nsec >= timeoutNsec;
}
else
{
timeout = time (NULL) - startFallback >= timeoutSec;
}
if (timeout)
{
ELEKTRA_LOG_WARNING ("connection timed out. could not publish notification");
zmq_close (monitorSocket);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/zeromqsend/testmod_zeromqsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void * context;
#define TEST_ENDPOINT "tcp://127.0.0.1:6002"

/** extended timeouts for tests */
#define TESTCONFIG_CONNECT_TIMEOUT "5"
#define TESTCONFIG_CONNECT_TIMEOUT "5000"
#define TESTCONFIG_SUBSCRIBE_TIMEOUT "5000"

/**
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/zeromqsend/zeromqsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define ELEKTRA_ZEROMQ_DEFAULT_PUB_ENDPOINT "tcp://localhost:6000"

/** default connection timeout for plugin */
#define ELEKTRA_ZEROMQ_DEFAULT_CONNECT_TIMEOUT 2
#define ELEKTRA_ZEROMQ_DEFAULT_CONNECT_TIMEOUT 1000

/** default subscription timeout for plugin */
#define ELEKTRA_ZEROMQ_DEFAULT_SUBSCRIBE_TIMEOUT 200
Expand Down

0 comments on commit a773bdf

Please sign in to comment.