Skip to content

Commit

Permalink
Replace ad-hoc code with RATE_LIMIT(...) macro
Browse files Browse the repository at this point in the history
So that messages are limited to once per second
  • Loading branch information
alandekok committed May 9, 2014
1 parent a626aef commit f7b0974
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 53 deletions.
10 changes: 10 additions & 0 deletions src/include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ int fr_logfile_unlock(fr_logfile_t *lf, int fd);

#define REMARKER(_m, _i, _e) _RMKR(L_DBG_ERR, L_DBG_LVL_1, _m, _i, _e)

/*
* Rate limit messages.
*/
#define RATE_LIMIT(_x) do {static time_t rl_last_complained = 0;time_t rl_now = time(NULL); \
if (rl_now != rl_last_complained) { \
rl_last_complained = rl_now; \
_x; \
} \
} while (0)

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 4 additions & 20 deletions src/main/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ struct fr_connection_pool_t {
time_t last_spawned; //!< Last time we spawned a connection.
time_t last_failed; //!< Last time we tried to spawn a
//!< a connection but failed.
time_t last_complained;//!< Last time we complained about
//!< configuration parameters.
time_t last_throttled; //!< Last time we refused to spawn a
//!< connection because the last
//!< connection failed, or we were
Expand Down Expand Up @@ -744,11 +742,8 @@ static int fr_connection_manage(fr_connection_pool_t *pool,
DEBUG("%s: Closing expired connection (%" PRIu64 "): Hit max_uses limit", pool->log_prefix,
this->number);
do_delete:
if ((pool->num <= pool->min) &&
(pool->last_complained < now)) {
WARN("%s: You probably need to lower \"min\"", pool->log_prefix);

pool->last_complained = now;
if (pool->num <= pool->min) {
RATE_LIMIT(WARN("%s: You probably need to lower \"min\"", pool->log_prefix));
}
fr_connection_close(pool, this);
return 0;
Expand Down Expand Up @@ -1107,14 +1102,6 @@ void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn)

new_conn = pool->create(pool->ctx);
if (!new_conn) {
time_t now = time(NULL);

if (pool->last_complained == now) {
now = 0;
} else {
pool->last_complained = now;
}

/*
* We can't create a new connection, so close
* this one.
Expand All @@ -1129,11 +1116,8 @@ void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn)
new_conn = fr_connection_get_internal(pool, false);
if (new_conn) return new_conn;

if (!now) return NULL;

ERROR("%s: Failed to reconnect (%" PRIu64 "), no free connections are available", pool->log_prefix,
conn_number);

RATE_LIMIT(ERROR("%s: Failed to reconnect (%" PRIu64 "), no free connections are available", pool->log_prefix,
conn_number));
return NULL;
}

Expand Down
10 changes: 2 additions & 8 deletions src/main/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2621,14 +2621,8 @@ rad_listen_t *proxy_new_listener(home_server_t *home, int src_port)

if ((home->limit.max_connections > 0) &&
(home->limit.num_connections >= home->limit.max_connections)) {
static time_t last_complained = 0;

now = time(NULL); /* we do this WAY too often */
if (last_complained == now) return NULL;
last_complained = now;

INFO("Home server %s has too many open connections (%d)",
home->name, home->limit.max_connections);
RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
home->name, home->limit.max_connections));
return NULL;
}

Expand Down
16 changes: 5 additions & 11 deletions src/main/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -1563,17 +1563,11 @@ int request_receive(rad_listen_t *listener, RADIUS_PACKET *packet,
*/
if (mainconfig.max_requests &&
((count = fr_packet_list_num_elements(pl)) > mainconfig.max_requests)) {
static time_t last_complained = 0;

if (last_complained == now.tv_sec) return 0;

last_complained = now.tv_sec;

ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
client->shortname,
packet->src_port, packet->id);
WARN("Please check the configuration file.\n"
"\tThe value for 'max_requests' is probably set too low.\n");
RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
client->shortname,
packet->src_port, packet->id);
WARN("Please check the configuration file.\n"
"\tThe value for 'max_requests' is probably set too low.\n"));

exec_trigger(NULL, NULL, "server.max_requests", true);
return 0;
Expand Down
16 changes: 2 additions & 14 deletions src/main/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,25 +412,13 @@ int request_enqueue(REQUEST *request)
thread_pool.request_count++;

if (thread_pool.num_queued >= thread_pool.max_queue_size) {
bool complain = false;
time_t now;
static time_t last_complained = 0;

now = time(NULL);
if (last_complained != now) {
last_complained = now;
complain = true;
}

pthread_mutex_unlock(&thread_pool.queue_mutex);

/*
* Mark the request as done.
*/
if (complain) {
ERROR("Something is blocking the server. There are %d packets in the queue, "
"waiting to be processed. Ignoring the new request.", thread_pool.num_queued);
}
RATE_LIMIT(ERROR("Something is blocking the server. There are %d packets in the queue, "
"waiting to be processed. Ignoring the new request.", thread_pool.num_queued));
return 0;
}
request->component = "<core>";
Expand Down

0 comments on commit f7b0974

Please sign in to comment.