+static _Bool drop_metric(void) {
+ _Bool drop = 0;
+ int wq_len = write_queue_length;
+ /* We store write_queue_length in a local variable because other threads may update write_queue_length.
+ * Having this in a local variable (like a cache) is better : we do not need a lock */
+
+ if(wq_len < write_limit_low) return(0);
+
+ if((write_limit_high > 0) && (wq_len > write_limit_low)) {
+ if(wq_len >= write_limit_high) {
+ /* if high == low, we come here too */
+ drop = 1;
+ } else {
+ /* here, high != low */
+ long probability_to_drop;
+ long n;
+
+ probability_to_drop = (wq_len - write_limit_low);
+
+ n = cdrand_range(write_limit_low, write_limit_high);
+
+ /* Let's have X = high - low.
+ * n is in range [0..X]
+ * probability_to_drop is in range [1..X[
+ * probability_to_drop gets bigger when wq_len gets bigger.
+ */
+ if(n <= probability_to_drop) {
+ drop = 1;
+ }
+ }
+ }
+ if(drop) {
+ cdtime_t now = cdtime();
+ if((now - last_drop_time) > TIME_T_TO_CDTIME_T (60)) {
+ last_drop_time = now;
+ /* If you want to count dropped metrics, don't forget to add a lock here */
+ /* dropped_metrics++; */
+ ERROR ("plugin_dispatch_values : Low water mark reached, dropping a metric");
+ }
+ }
+ return(drop);
+}
+