chiark / gitweb /
ratelimit start requests
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Jan 2010 03:42:57 +0000 (04:42 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 29 Jan 2010 03:42:57 +0000 (04:42 +0100)
Makefile
ratelimit.c [new file with mode: 0644]
ratelimit.h [new file with mode: 0644]
service.c
service.h

index d35620e0c17e556e93783dd70d17c34cd6314a7a..e5cb6b5daa57f06e3f99513aabe776eb14d9a44d 100644 (file)
--- a/Makefile
+++ b/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
 
diff --git a/ratelimit.c b/ratelimit.c
new file mode 100644 (file)
index 0000000..d9eb493
--- /dev/null
@@ -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;
+}
diff --git a/ratelimit.h b/ratelimit.h
new file mode 100644 (file)
index 0000000..7f01838
--- /dev/null
@@ -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
index 70299dd065f472d1f0a52435b982f41d8632a990..2943da6e80405be1c5157fb173c487404430382f 100644 (file)
--- a/service.c
+++ b/service.c
@@ -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;
 
index f9dbfea5e68325cf344a4c09db2a00d3d89a3de0..38b5438d4e2b4c4cc2a4d20b448dc418204dcfc6 100644 (file)
--- a/service.h
+++ b/service.h
@@ -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;