annotate rust/src/params.rs @ 629:3e5e52d50af5 rust

comment
author Matt Johnston <matt@ucc.asn.au>
date Fri, 19 Apr 2019 13:58:30 +0800
parents d5075136442f
children c57821a60e51
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 extern crate tokio_core;
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
2 extern crate futures_await as futures;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 extern crate rand;
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
4 extern crate serde_json;
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
5 extern crate base64;
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
6 extern crate atomicwrites;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 use std::time::Duration;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 use std::io;
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
10 use std::str;
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
11 use std::rc::Rc;
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
12 use std::sync::{Arc,Mutex};
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
13 use std::error::Error;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
14 use std::cell::{Cell,RefCell};
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
15 use std::fs::File;
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
16 use std::io::Read;
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
17 use futures::prelude::*;
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
18
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 use tokio_core::reactor::Interval;
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 use tokio_core::reactor::Handle;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
22 use futures::{Stream,Future,future};
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
23 use self::rand::Rng;
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
24 use std::str::FromStr;
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
25 use hyper;
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
26 use hyper::header::{Headers, ETag, EntityTag};
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
27 use hyper::client::Client;
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
28
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 use types::*;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
30 use ::Config;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
32 #[derive(Deserialize, Serialize, Debug, Clone)]
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
33 pub struct Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
34 pub fridge_setpoint: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
35 pub fridge_difference: f32,
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 618
diff changeset
36 pub overshoot_delay: u64,
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
37 pub overshoot_factor: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
38 pub disabled: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
39 pub nowort: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
40 pub fridge_range_lower: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
41 pub fridge_range_upper: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
42 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
43
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
44 impl Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
45 pub fn defaults() -> Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
46 Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
47 fridge_setpoint: 16.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
48 fridge_difference: 0.2,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
49 overshoot_delay: 720, // 12 minutes
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
50 overshoot_factor: 1.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
51 disabled: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
52 nowort: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
53 fridge_range_lower: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
54 fridge_range_upper: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
55 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
56 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
57
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
58 fn try_load(filename: &str) -> Result<Params, TemplogError> {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
59 let mut s = String::new();
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
60 File::open(filename)?.read_to_string(&mut s)?;
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
61 Ok(serde_json::from_str(&s)?)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
62 }
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
63
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
64 pub fn load(config: &Config) -> Params {
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
65 Self::try_load(&config.PARAMS_FILE)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
66 .unwrap_or_else(|_| Params::defaults())
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
67 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
68 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
69
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
70 #[derive(Deserialize, Debug)]
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
71 struct Response {
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
72 // sent as an opaque etag: header. Has format "epoch-nonce",
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
73 // responses where the epoch do not match ParamWaiter::epoch are dropped
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
74 etag: String,
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
75 params: Params,
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
76 }
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
77
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
78 #[derive(Clone)]
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
79 pub struct ParamWaiter {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
80 limitlog: NotTooOften,
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
81 // last_etag is used for long-polling.
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
82 last_etag: RefCell<String>,
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
83 epoch: String,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
84
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
85 config: Config,
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
86 handle: Handle,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
89 const LOG_MINUTES: u64 = 15;
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
90 const MAX_RESPONSE_SIZE: usize = 10000;
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
91 const TIMEOUT_MINUTES: u64 = 5;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
92
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
93 impl ParamWaiter {
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
94 fn make_request(&self) -> hyper::client::FutureResponse {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
95 let uri = self.config.SETTINGS_URL.parse().expect("Bad SETTINGS_URL in config");
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
96 let mut req = hyper::client::Request::new(hyper::Method::Get, uri);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
97 {
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
98 let headers = req.headers_mut();
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
99 headers.set(hyper::header::ETag(EntityTag::new(false, self.last_etag.borrow().clone())));
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
100 }
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
101 hyper::client::Client::new(&self.handle).request(req)
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
102 // XXX how do we do this?
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
103 // req.timeout(Duration::new(TIMEOUT_MINUTES * 60, 0)).unwrap();
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
104 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
105
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
106
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
107 fn handle_response(&self, buf : hyper::Chunk, status: hyper::StatusCode) -> Result<Params, TemplogError> {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
108 let text = String::from_utf8_lossy(buf.as_ref());
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
109 match status {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
110 hyper::StatusCode::Ok => {
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
111 // new params
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
112 let r: Response = serde_json::from_str(&text)?;
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
113 let mut le = self.last_etag.borrow_mut();
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
114 *le = r.etag;
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
115
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
116 // update params if the epoch is correct
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
117 if let Some(e) = le.split('-').next() {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
118 if e == &self.epoch {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
119 self.write_params(&r.params);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
120 return Ok(r.params);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
121 }
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
122 }
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
123 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch)))
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
124 }
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
125 hyper::StatusCode::NotModified => {
629
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
126 // XXX this isn't really an error. Should handle_response() return
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
127 // Result<Option<Params>, TemplogError> instead?
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
128
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
129 Err(TemplogError::new("304 unmodified (long polling timeout at the server)"))
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
130 },
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
131 _ => {
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
132 Err(TemplogError::new(&format!("Wrong server response code {}: {}", status.as_u16(), text)))
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
133 },
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
134 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
135 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
136
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
137 fn write_params(&self, params: &Params) {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
138 let p = atomicwrites::AtomicFile::new(&self.config.PARAMS_FILE, atomicwrites::AllowOverwrite);
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
139 p.write(|f| {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
140 serde_json::to_writer(f, params)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
141 });
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
142 }
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
143
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
144 #[async_stream(item = Params)]
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
145 pub fn stream(config: Config, handle: Handle) -> Result<(), TemplogError> {
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
146 let rcself = Rc::new(ParamWaiter::new(config, handle));
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
147
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
148 let dur = Duration::from_millis(4000);
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
149 #[async]
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
150 for _ in Interval::new(dur, &rcself.handle).unwrap() {
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
151 // fetch params
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
152 // TODO - skip if inflight.
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
153 let r = await!(rcself.make_request()).map_err(|e| TemplogError::new_hyper("response", e))?;
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
154 let status = r.status();
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
155 let b = await!(r.body().concat2()).map_err(|e| TemplogError::new_hyper("body", e))?;
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
156 if let Ok(params) = rcself.handle_response(b, status) {
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
157 stream_yield!(params);
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
158 }
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
159 }
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
160 Ok(())
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
163 fn new(config: Config, handle: Handle) -> Self {
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
164 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
165 rand::OsRng::new().expect("Opening system RNG failed").fill_bytes(&mut b);
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
166 let epoch = base64::encode(&b);
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
167
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
168 ParamWaiter {
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
169 limitlog: NotTooOften::new(LOG_MINUTES*60),
616
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
170 last_etag: RefCell::new(String::new()),
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
171 epoch: epoch,
627
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
172 config: config,
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
173 handle: handle,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
174 }
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
175 }
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
176
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178