comparison 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
comparison
equal deleted inserted replaced
608:71f045231a07 609:7bda01659426
1 use std::collections::HashMap; 1 use std::collections::HashMap;
2 use std::time::Duration; 2 use std::time::{Duration,Instant};
3 use std::error::Error; 3 use std::error::Error;
4 use std::fmt; 4 use std::fmt;
5 use std::cell::Cell;
5 6
6 use serde::{Deserialize,Serialize}; 7 use serde::{Deserialize,Serialize};
7 8
8 #[derive(Deserialize, Serialize, Debug)] 9 #[derive(Deserialize, Serialize, Debug)]
9 pub struct Params { 10 pub struct Params {
33 } 34 }
34 35
35 #[derive(Debug)] 36 #[derive(Debug)]
36 pub struct ParamHolder { 37 pub struct ParamHolder {
37 pub p: Params, 38 pub p: Params,
38 epoch: String, // XXX or a byte array? 39 epoch: String,
39 } 40 }
40 41
41 impl ParamHolder { 42 impl ParamHolder {
42 pub fn new() -> ParamHolder { 43 pub fn new() -> ParamHolder {
43 ParamHolder { 44 ParamHolder {
44 p: Params::defaults(), 45 p: Params::defaults(),
45 epoch: String::new(), 46 epoch: String::new(),
46 } 47 }
48 }
49
50 pub fn receive(&mut self, p: &Params, epoch: &String)
51 {
52
47 } 53 }
48 } 54 }
49 55
50 #[derive(Debug)] 56 #[derive(Debug)]
51 pub struct Readings { 57 pub struct Readings {
94 } 100 }
95 } 101 }
96 } 102 }
97 103
98 104
105 /// Call closures with a rate limit. Useful for log message ratelimiting
106 pub struct NotTooOften {
107 last: Cell<Instant>,
108 limit: Duration,
109 }
99 110
111 impl NotTooOften {
112 pub fn new(limit_secs: u64) -> Self {
113 NotTooOften {
114 limit: Duration::new(limit_secs, 0),
115 last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)),
116 }
117 }
118
119 pub fn and_then<F>(&self, op: F)
120 where F: Fn() {
121 let now = Instant::now();
122 if now - self.last.get() > self.limit {
123 self.last.set(now);
124 op();
125 }
126 }
127 }
128
129