]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: Documented the rate limiters.
authorMartin Mares <mj@ucw.cz>
Sat, 21 Feb 2009 19:54:03 +0000 (20:54 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 21 Feb 2009 19:54:03 +0000 (20:54 +0100)
ucw/doc/log.txt
ucw/tbf.h

index 2da50e1e6c9e3a3b5d597c5dbe63a63755b21137..4eb15769e5a3a9f07dcb70c207b9fade63c02cd5 100644 (file)
@@ -125,3 +125,30 @@ log streams or to syslog is therefore not supported.
 ucw/log.h
 ---------
 !!ucw/log.h
+
+Limiting rate: ucw/tbf.h
+------------------------
+
+LibUCW also offers simple means of limiting the rate of log messages (or of any other
+events) by means of a so called 'Token Bucket Filter.' The idea behind this filter is
+simple: To log a message, we need a token. The available tokens are accumulated in
+a bucket which has a fixed 'filling rate' (the number of tokens arriving in the bucket
+per second, which may be a fractional number) and fixed 'maximum capacity.' The
+bucket receives the tokens continuously with the given rate and when it reaches
+the maximum capacity, the extra tokens are dropped on the floor. When a message
+has to be sent, we take a single token from the bucket and if there wasn't any,
+we drop the message.
+
+The filling rate therefore describes the maximum sustained rate of messages,
+while the bucket capacity tells the filter the maximum length of a short burst,
+which can temporarily exceed the rate.
+
+A general bucket filter is available in `ucw/tbf.h`. The usual way of using it
+to limit logging is to set up a filter hook of a stream which asks the TBF for
+every message. (Remember, though, that if your program is multithreaded, the
+filter hook can be run in multiple threads in parallel, so it has to guard the
+TBF by a lock.) The configuration interface for log streams described above
+is able to attach rate limiters to streams per user's request, so you usually
+need not take any extra care.
+
+!!ucw/tbf.h
index 5610090bf38b111d9784d958f9d6523ff15786e4..7f0d09cf048f0490ffba098eb96cb53aa2c51c68 100644 (file)
--- a/ucw/tbf.h
+++ b/ucw/tbf.h
 #ifndef _UCW_TBF_H_
 #define _UCW_TBF_H_
 
+/** A data structure describing a single TBF. **/
 struct token_bucket_filter {
-  double rate;                         // Number of tokens per second
+  double rate;                         // Number of tokens received per second
   uns burst;                           // Capacity of the bucket
   timestamp_t last_hit;                        // Internal state...
   double bucket;
   uns drop_count;
 };
 
+/** Initialize the bucket. **/
 void tbf_init(struct token_bucket_filter *f);
+
+/**
+ * Ask the filter to process a single event. Returns a negative number
+ * if the event exceeds the rate (and should be dropped) and a non-negative
+ * number if the event passes the filter.
+ * The absolute value of the result is the number of dropped events
+ * since the last passed event.
+ **/
 int tbf_limit(struct token_bucket_filter *f, timestamp_t now);
 
 #endif