+/* Rate-limiter for log messages. */
+struct vlog_rate_limit {
+ /* Configuration settings. */
+ unsigned int rate; /* Tokens per second. */
+ unsigned int burst; /* Max cumulative tokens credit. */
+
+ /* Current status. */
+ unsigned int tokens; /* Current number of tokens. */
+ time_t last_fill; /* Last time tokens added. */
+ time_t first_dropped; /* Time first message was dropped. */
+ unsigned int n_dropped; /* Number of messages dropped. */
+};
+
+/* Number of tokens to emit a message. We add 'rate' token per second, which
+ * is 60 times the unit used for 'rate', thus 60 tokens are required to emit
+ * one message. */
+#define VLOG_MSG_TOKENS 60
+
+/* Initializer for a struct vlog_rate_limit, to set up a maximum rate of RATE
+ * messages per minute and a maximum burst size of BURST messages. */
+#define VLOG_RATE_LIMIT_INIT(RATE, BURST) \
+ { \
+ RATE, /* rate */ \
+ (MIN(BURST, UINT_MAX / VLOG_MSG_TOKENS) \
+ * VLOG_MSG_TOKENS), /* burst */ \
+ 0, /* tokens */ \
+ 0, /* last_fill */ \
+ 0, /* n_dropped */ \
+ }
+