Skip to content

Commit

Permalink
[#176] Add disconnect_client_force setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Sep 22, 2021
1 parent 9c3dce5 commit 7a75097
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ __Danger zone__
| Property | Default | Unit | Required | Description |
|----------|---------|------|----------|-------------|
| disconnect_client | 0 | Int | No | Disconnect clients that have been idle for more than the specified seconds. This setting __DOES NOT__ take long running transactions into account |
| disconnect_client_force | off | Bool | No | Disconnect clients that have been active for more than the specified seconds. This setting __DOES NOT__ take long running transactions into account |

## Server section

Expand Down
3 changes: 3 additions & 0 deletions doc/man/pgagroal.conf.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ Danger zone
disconnect_client
Disconnect clients that have been idle for more than the specified seconds. This setting DOES NOT take long running transactions into account. Default is 0

disconnect_client_force
Disconnect clients that have been active for more than the specified seconds. This setting DOES NOT take long running transactions into account. Default is off

The options for the PostgreSQL section are

host
Expand Down
17 changes: 9 additions & 8 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,15 @@ struct configuration
int max_connections; /**< The maximum number of connections */
bool allow_unknown_users; /**< Allow unknown users */

int blocking_timeout; /**< The blocking timeout in seconds */
int idle_timeout; /**< The idle timeout in seconds */
int validation; /**< Validation mode */
int background_interval; /**< Background validation timer in seconds */
int max_retries; /**< The maximum number of retries */
int authentication_timeout; /**< The authentication timeout in seconds */
int disconnect_client; /**< Disconnect client if idle for more than the specified seconds */
char pidfile[MISC_LENGTH]; /**< File containing the PID */
int blocking_timeout; /**< The blocking timeout in seconds */
int idle_timeout; /**< The idle timeout in seconds */
int validation; /**< Validation mode */
int background_interval; /**< Background validation timer in seconds */
int max_retries; /**< The maximum number of retries */
int authentication_timeout; /**< The authentication timeout in seconds */
int disconnect_client; /**< Disconnect client if idle for more than the specified seconds */
bool disconnect_client_force; /**< Force a disconnect client if active for more than the specified seconds */
char pidfile[MISC_LENGTH]; /**< File containing the PID */

char libev[MISC_LENGTH]; /**< Name of libev mode */
int buffer_size; /**< Socket buffer size */
Expand Down
16 changes: 16 additions & 0 deletions src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pgagroal_init_configuration(void* shm)
config->max_retries = 5;
config->authentication_timeout = 5;
config->disconnect_client = 0;
config->disconnect_client_force = false;

config->buffer_size = DEFAULT_BUFFER_SIZE;
config->keep_alive = true;
Expand Down Expand Up @@ -503,6 +504,20 @@ pgagroal_read_configuration(void* shm, char* filename)
unknown = true;
}
}
else if (!strcmp(key, "disconnect_client_force"))
{
if (!strcmp(section, "pgagroal"))
{
if (as_bool(value, &config->disconnect_client_force))
{
unknown = true;
}
}
else
{
unknown = true;
}
}
else if (!strcmp(key, "pidfile"))
{
if (!strcmp(section, "pgagroal"))
Expand Down Expand Up @@ -2489,6 +2504,7 @@ transfer_configuration(struct configuration* config, struct configuration* reloa
config->max_retries = reload->max_retries;
config->authentication_timeout = reload->authentication_timeout;
config->disconnect_client = reload->disconnect_client;
config->disconnect_client_force = reload->disconnect_client_force;
/* pidfile */
restart_string("pidfile", config->pidfile, reload->pidfile);

Expand Down
33 changes: 24 additions & 9 deletions src/libpgagroal/pipeline_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ session_destroy(void* pipeline_shmem, size_t pipeline_shmem_size)
static void
session_periodic(void)
{
signed char state;
signed char idle;
bool do_kill;
time_t now;
struct client_session* client;
struct configuration* config;
Expand All @@ -179,23 +181,36 @@ session_periodic(void)
{
client = pipeline_shmem + (i * sizeof(struct client_session));

idle = CLIENT_IDLE;

if (atomic_compare_exchange_strong(&client->state, &idle, CLIENT_CHECK))
if (difftime(now, client->timestamp) > config->disconnect_client)
{
if (difftime(now, client->timestamp) > config->disconnect_client)
if (config->connections[i].pid != 0)
{
if (config->connections[i].pid != 0)
state = atomic_load(&client->state);
do_kill = false;

if (config->disconnect_client_force)
{
do_kill = true;
}
else
{
idle = CLIENT_IDLE;

if (atomic_compare_exchange_strong(&client->state, &idle, CLIENT_CHECK))
{
do_kill = true;
}
}

if (do_kill)
{
pgagroal_log_info("Disconnect client %s/%s using slot %d (pid %d socket %d)",
config->connections[i].database, config->connections[i].username,
i, config->connections[i].pid, config->connections[i].fd);
kill(config->connections[i].pid, SIGQUIT);
}
}
else
{
atomic_store(&client->state, CLIENT_IDLE);

atomic_store(&client->state, state);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ main(int argc, char **argv)
if (config->disconnect_client > 0)
{
ev_periodic_init (&disconnect_client, disconnect_client_cb, 0.,
MAX(1. * config->disconnect_client / 2., 1.), 0);
MIN(300., MAX(1. * config->disconnect_client / 2., 1.)), 0);
ev_periodic_start (main_loop, &disconnect_client);
}

Expand Down

0 comments on commit 7a75097

Please sign in to comment.