Skip to content

Commit

Permalink
ratelimit start requests
Browse files Browse the repository at this point in the history
  • Loading branch information
poettering committed Jan 29, 2010
1 parent c20cae3 commit 1e2e813
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ COMMON= \
socket.o \
timer.o \
load-dropin.o \
execute.o
execute.o \
ratelimit.o

all: systemd test-engine test-job-type systemd-logger

Expand Down
43 changes: 43 additions & 0 deletions ratelimit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <assert.h>

#include "ratelimit.h"
#include "log.h"

/* Modelled after Linux' lib/ratelimit.c by Dave Young
* <[email protected]>, which is licensed GPLv2. */

bool ratelimit_test(RateLimit *r) {
usec_t timestamp;

timestamp = now(CLOCK_MONOTONIC);

assert(r);
assert(r->interval > 0);
assert(r->burst > 0);

if (r->begin <= 0 ||
r->begin + r->interval < timestamp) {

if (r->n_missed > 0)
log_warning("%u events suppressed", r->n_missed);

r->begin = timestamp;

/* Reset counters */
r->n_printed = 0;
r->n_missed = 0;
goto good;
}

if (r->n_printed <= r->burst)
goto good;

r->n_missed++;
return false;

good:
r->n_printed++;
return true;
}
36 changes: 36 additions & 0 deletions ratelimit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#ifndef fooratelimithfoo
#define fooratelimithfoo

#include "util.h"

typedef struct RateLimit {
usec_t interval;
unsigned burst;
unsigned n_printed, n_missed;
usec_t begin;
} RateLimit;

#define RATELIMIT_DEFINE(_name, _interval, _burst) \
RateLimit _name = { \
.interval = (_interval), \
.burst = (_burst), \
.n_printed = 0, \
.n_missed = 0, \
.begin = 0 \
}

#define RATELIMIT_INIT(v, _interval, _burst) \
do { \
RateLimit *r = &(v); \
r->interval = (_interval); \
r->burst = (_burst); \
r->n_printed = 0; \
r->n_missed = 0; \
r->begin = 0; \
} while (false);

bool ratelimit_test(RateLimit *r);

#endif
8 changes: 8 additions & 0 deletions service.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ static int service_init(Unit *u) {

s->state = SERVICE_DEAD;

RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);

/* Load a .service file */
if ((r = unit_load_fragment(u)) < 0) {
service_done(u);
Expand Down Expand Up @@ -734,6 +736,12 @@ static int service_start(Unit *u) {

assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);

/* Make sure we don't enter a busy loop of some kind. */
if (!ratelimit_test(&s->ratelimit)) {
log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
return -EAGAIN;
}

s->failure = false;
s->main_pid_known = false;

Expand Down
3 changes: 3 additions & 0 deletions service.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
typedef struct Service Service;

#include "unit.h"
#include "ratelimit.h"

typedef enum ServiceState {
SERVICE_DEAD,
Expand Down Expand Up @@ -71,6 +72,8 @@ struct Service {

bool failure:1; /* if we shut down, remember why */
Watch timer_watch;

RateLimit ratelimit;
};

const UnitVTable service_vtable;
Expand Down

0 comments on commit 1e2e813

Please sign in to comment.