chiark / gitweb /
server/servutil.c: Add utilities for simple leaky-bucket rate limiting.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 2 Sep 2017 13:59:53 +0000 (14:59 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 16 Jun 2018 18:13:41 +0000 (19:13 +0100)
server/servutil.c
server/tripe.h

index 707213968f9dd2be252e7485c313c3bbbce7e4e5..72de38a2f9d47ece2d6110f92db1b1c39c9c7ccd 100644 (file)
@@ -82,6 +82,55 @@ int seq_check(seqwin *s, uint32 q, const char *service)
   return (0);
 }
 
+/*----- Rate limiting -----------------------------------------------------*/
+
+/* --- @ratelim_init@ --- *
+ *
+ * Arguments:  @ratelim *r@ = rate-limiting state to fill in
+ *             @unsigned persec@ = credit to accumulate per second
+ *             @unsigned max@ = maximum credit to retain
+ *
+ * Returns:    ---
+ *
+ * Use:                Initialize a rate-limiting state.
+ */
+
+void ratelim_init(ratelim *r, unsigned persec, unsigned max)
+{
+  r->n = r->max = max;
+  r->persec = persec;
+  gettimeofday(&r->when, 0);
+}
+
+/* --- @ratelim_withdraw@ --- *
+ *
+ * Arguments:  @ratelim *r@ = rate-limiting state
+ *             @unsigned n@ = credit to withdraw
+ *
+ * Returns:    Zero if successful; @-1@ if there is unsufficient credit
+ *
+ * Use:                Updates the state with any accumulated credit.  Then, if
+ *             there there are more than @n@ credits available, withdraw @n@
+ *             and return successfully; otherwise, report failure.
+ */
+
+int ratelim_withdraw(ratelim *r, unsigned n)
+{
+  struct timeval now, delta;
+  unsigned long d;
+
+  gettimeofday(&now, 0);
+  TV_SUB(&delta, &now, &r->when);
+  d = (unsigned long)r->persec*delta.tv_sec +
+    (unsigned long)r->persec*delta.tv_usec/MILLION;
+  if (d < r->max - r->n) r->n += d;
+  else r->n = r->max;
+  r->when = now;
+
+  if (n > r->n) return (-1);
+  else { r->n -= n; return (0); }
+}
+
 /*----- Random odds and sods ----------------------------------------------*/
 
 /* --- @timestr@ --- *
index 494aa182584dbba6ea6dc4f631c49beda907cc08..5550c1fd1d276d14e106a8a519f0433fb86ba682 100644 (file)
@@ -1768,6 +1768,39 @@ extern void seq_reset(seqwin */*s*/);
 
 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
 
+typedef struct ratelim {
+  unsigned n, max, persec;
+  struct timeval when;
+} ratelim;
+
+/* --- @ratelim_init@ --- *
+ *
+ * Arguments:  @ratelim *r@ = rate-limiting state to fill in
+ *             @unsigned persec@ = credit to accumulate per second
+ *             @unsigned max@ = maximum credit to retain
+ *
+ * Returns:    ---
+ *
+ * Use:                Initialize a rate-limiting state.
+ */
+
+extern void ratelim_init(ratelim */*r*/,
+                        unsigned /*persec*/, unsigned /*max*/);
+
+/* --- @ratelim_withdraw@ --- *
+ *
+ * Arguments:  @ratelim *r@ = rate-limiting state
+ *             @unsigned n@ = credit to withdraw
+ *
+ * Returns:    Zero if successful; @-1@ if there is unsufficient credit
+ *
+ * Use:                Updates the state with any accumulated credit.  Then, if
+ *             there there are more than @n@ credits available, withdraw @n@
+ *             and return successfully; otherwise, report failure.
+ */
+
+extern int ratelim_withdraw(ratelim */*r*/, unsigned /*n*/);
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus