diff rust/src/types.rs @ 609:7bda01659426 rust

not building, paramwaiter work
author Matt Johnston <matt@ucc.asn.au>
date Sat, 18 Feb 2017 00:21:10 +0800
parents 278f1002b5c7
children af0dac00d40b
line wrap: on
line diff
--- a/rust/src/types.rs	Fri Feb 17 23:07:33 2017 +0800
+++ b/rust/src/types.rs	Sat Feb 18 00:21:10 2017 +0800
@@ -1,7 +1,8 @@
 use std::collections::HashMap;
-use std::time::Duration;
+use std::time::{Duration,Instant};
 use std::error::Error;
 use std::fmt;
+use std::cell::Cell;
 
 use serde::{Deserialize,Serialize};
 
@@ -35,7 +36,7 @@
 #[derive(Debug)]
 pub struct ParamHolder {
     pub p: Params,
-    epoch: String, // XXX or a byte array?
+    epoch: String,
 }
 
 impl ParamHolder {
@@ -45,6 +46,11 @@
             epoch: String::new(),
         }
     }
+
+    pub fn receive(&mut self, p: &Params, epoch: &String)
+    {
+
+    }
 }
 
 #[derive(Debug)]
@@ -96,4 +102,28 @@
 }
 
 
+/// Call closures with a rate limit. Useful for log message ratelimiting
+pub struct NotTooOften {
+    last: Cell<Instant>,
+    limit: Duration,
+}
 
+impl NotTooOften {
+    pub fn new(limit_secs: u64) -> Self {
+        NotTooOften {
+            limit: Duration::new(limit_secs, 0),
+            last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)),
+        }
+    }
+
+    pub fn and_then<F>(&self, op: F)
+        where F: Fn() {
+        let now = Instant::now();
+        if now - self.last.get() > self.limit {
+            self.last.set(now);
+            op();
+        }
+    }
+}
+
+