mirror of
https://github.com/systemd/systemd.git
synced 2025-01-21 22:04:01 +03:00
ratelimit start requests
This commit is contained in:
parent
c20cae324d
commit
1e2e81336b
3
Makefile
3
Makefile
@ -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
|
||||
|
||||
|
43
ratelimit.c
Normal file
43
ratelimit.c
Normal file
@ -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
|
||||
* <hidave.darkstar@gmail.com>, 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
ratelimit.h
Normal file
36
ratelimit.h
Normal file
@ -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
|
@ -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);
|
||||
@ -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;
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
typedef struct Service Service;
|
||||
|
||||
#include "unit.h"
|
||||
#include "ratelimit.h"
|
||||
|
||||
typedef enum ServiceState {
|
||||
SERVICE_DEAD,
|
||||
@ -71,6 +72,8 @@ struct Service {
|
||||
|
||||
bool failure:1; /* if we shut down, remember why */
|
||||
Watch timer_watch;
|
||||
|
||||
RateLimit ratelimit;
|
||||
};
|
||||
|
||||
const UnitVTable service_vtable;
|
||||
|
Loading…
x
Reference in New Issue
Block a user