Skip to content

Commit

Permalink
[#263] Use a macro to perform the process-sleep.
Browse files Browse the repository at this point in the history
Introduces two macros:
- `SLEEP` to perform a nanosleep call with the specified amount of
time expressed as nanoseconds;
- `SLEEP_AND_GOTO` does the same of the above but adds also a `goto`
to the specified label.

Example of usage:

      SLEEP_AND_GOTO(1000000L,retry)

is the same as

      {
         /* Sleep for 1ms */
         struct timespec ts;
         ts.tv_sec = 0;
         ts.tv_nsec = 1000000L;
         nanosleep(&ts, NULL);

         goto retry;
     }

Close #263
  • Loading branch information
fluca1978 authored and jesperpedersen committed Jun 15, 2022
1 parent d751766 commit d0d7c2b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 83 deletions.
41 changes: 41 additions & 0 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,47 @@ extern "C" {
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })

/*
* Common piece of code to perform a sleeping.
*
* @param zzz the amount of time to
* sleep, expressed as nanoseconds.
*
* Example
SLEEP(5000000L)
*
*/
#define SLEEP(zzz) \
{ \
struct timespec ts_private; \
ts_private.tv_sec = 0; \
ts_private.tv_nsec = zzz; \
nanosleep(&ts_private, NULL); \
}

/*
* Commonly used block of code to sleep
* for a specified amount of time and
* then jump back to a specified label.
*
* @param zzz how much time to sleep (as long nanoseconds)
* @param goto_to the label to which jump to
*
* Example:
*
...
else
SLEEP_AND_GOTO(100000L,retru)
*/
#define SLEEP_AND_GOTO(zzz, goto_to) \
{ \
struct timespec ts_private; \
ts_private.tv_sec = 0; \
ts_private.tv_nsec = zzz; \
nanosleep(&ts_private, NULL); \
goto goto_to; \
}

/**
* The shared memory segment
*/
Expand Down
20 changes: 2 additions & 18 deletions src/libpgagroal/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,7 @@ pgagroal_log_line(int level, char* file, int line, char* fmt, ...)
atomic_store(&config->log_lock, STATE_FREE);
}
else
{
/* Sleep for 1ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000000L;
nanosleep(&ts, NULL);

goto retry;
}
SLEEP_AND_GOTO(1000000L,retry)
}
}

Expand Down Expand Up @@ -496,14 +488,6 @@ pgagroal_log_mem(void* data, size_t size)
atomic_store(&config->log_lock, STATE_FREE);
}
else
{
/* Sleep for 1ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000000L;
nanosleep(&ts, NULL);

goto retry;
}
SLEEP_AND_GOTO(1000000L,retry)
}
}
6 changes: 1 addition & 5 deletions src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,11 +1281,7 @@ read_complete(SSL* ssl, int socket, void* buf, size_t size)
}
else if (r < needs)
{
/* Sleep for 10ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 10000000L;
nanosleep(&ts, NULL);
SLEEP(10000000L)

pgagroal_log_trace("Got: %ld, needs: %ld", r, needs);

Expand Down
6 changes: 2 additions & 4 deletions src/libpgagroal/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -1278,17 +1278,15 @@ ssl_read_message(SSL* ssl, int timeout, struct message** msg)
case SSL_ERROR_ZERO_RETURN:
if (timeout > 0)
{
struct timespec ts;

if (difftime(time(NULL), start_time) >= timeout)
{
return MESSAGE_STATUS_ZERO;
}

/* Sleep for 100ms */
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
SLEEP(100000000L)

}
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
Expand Down
5 changes: 1 addition & 4 deletions src/libpgagroal/pipeline_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ transaction_start(struct ev_loop* loop, struct worker_io* w)
if (is_new)
{
/* Sleep for 5ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 5000000L;
nanosleep(&ts, NULL);
SLEEP(5000000L)
}

return;
Expand Down
14 changes: 2 additions & 12 deletions src/libpgagroal/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,8 @@ pgagroal_get_connection(char* username, char* database, bool reuse, bool transac
retry2:
if (config->blocking_timeout > 0)
{

/* Sleep for 500ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 500000000L;
nanosleep(&ts, NULL);
SLEEP(500000000L)

double diff = difftime(time(NULL), start_time);
if (diff >= (double)config->blocking_timeout)
Expand Down Expand Up @@ -334,15 +330,9 @@ pgagroal_get_connection(char* username, char* database, bool reuse, bool transac
}
}
else
{
/* Sleep for 1000 nanos */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000L;
nanosleep(&ts, NULL);
SLEEP_AND_GOTO(1000L,start)

goto start;
}
}
}

Expand Down
46 changes: 6 additions & 40 deletions src/libpgagroal/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -1653,15 +1653,8 @@ client_password(SSL* c_ssl, int client_fd, char* username, char* password, int s
if (difftime(time(NULL), start_time) < config->authentication_timeout)
{
if (pgagroal_socket_isvalid(client_fd))
{
/* Sleep for 100ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);

goto retry;
}
SLEEP_AND_GOTO(100000000L,retry)
}
}

Expand Down Expand Up @@ -1742,15 +1735,9 @@ client_md5(SSL* c_ssl, int client_fd, char* username, char* password, int slot)
if (difftime(time(NULL), start_time) < config->authentication_timeout)
{
if (pgagroal_socket_isvalid(client_fd))
{
/* Sleep for 100ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
SLEEP_AND_GOTO(100000000L,retry)

goto retry;
}
}
}

Expand Down Expand Up @@ -1875,15 +1862,9 @@ client_scram256(SSL* c_ssl, int client_fd, char* username, char* password, int s
if (difftime(time(NULL), start_time) < config->authentication_timeout)
{
if (pgagroal_socket_isvalid(client_fd))
{
/* Sleep for 100ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
SLEEP_AND_GOTO(100000000L,retry)

goto retry;
}
}
}

Expand Down Expand Up @@ -4809,10 +4790,7 @@ auth_query_get_connection(char* username, char* password, char* database, int* s
{

/* Sleep for 100ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
SLEEP(100000000L)

double diff = difftime(time(NULL), start_time);
if (diff >= (double)config->blocking_timeout)
Expand Down Expand Up @@ -5421,15 +5399,9 @@ auth_query_client_md5(SSL* c_ssl, int client_fd, char* username, char* hash, int
if (difftime(time(NULL), start_time) < config->authentication_timeout)
{
if (pgagroal_socket_isvalid(client_fd))
{
/* Sleep for 100ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
SLEEP_AND_GOTO(100000000L,retry)

goto retry;
}
}
}

Expand Down Expand Up @@ -5545,15 +5517,9 @@ auth_query_client_scram256(SSL* c_ssl, int client_fd, char* username, char* shad
if (difftime(time(NULL), start_time) < config->authentication_timeout)
{
if (pgagroal_socket_isvalid(client_fd))
{
/* Sleep for 100ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000L;
nanosleep(&ts, NULL);
SLEEP_AND_GOTO(100000000L,retry)

goto retry;
}
}
}

Expand Down

0 comments on commit d0d7c2b

Please sign in to comment.