Mercurial > templog
annotate rust/src/params.rs @ 639:89818a14648b rust tip
- switch to using anyhow for errors, surf for http
runs but surf has problems
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 28 Nov 2019 23:57:00 +0800 |
parents | a9f353f488d0 |
children |
rev | line source |
---|---|
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
1 use anyhow::{Result,bail}; |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
2 |
592 | 3 use std::time::Duration; |
634 | 4 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
5 use std::str; |
634 | 6 |
7 use std::cell::{RefCell}; | |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
8 use std::fs::File; |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
9 use std::io::Read; |
627 | 10 |
633 | 11 use serde::{Serialize,Deserialize}; |
592 | 12 |
634 | 13 use rand::rngs::{OsRng}; |
14 use rand::{RngCore}; | |
15 | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
16 use async_std::task; |
633 | 17 |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
18 use surf; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
19 |
633 | 20 use riker::actors::*; |
21 | |
22 use super::types::*; | |
23 use super::config::Config; | |
592 | 24 |
615 | 25 #[derive(Deserialize, Serialize, Debug, Clone)] |
26 pub struct Params { | |
27 pub fridge_setpoint: f32, | |
28 pub fridge_difference: f32, | |
621 | 29 pub overshoot_delay: u64, |
615 | 30 pub overshoot_factor: f32, |
31 pub disabled: bool, | |
32 pub nowort: bool, | |
33 pub fridge_range_lower: f32, | |
34 pub fridge_range_upper: f32, | |
35 } | |
36 | |
37 impl Params { | |
38 pub fn defaults() -> Params { | |
39 Params { | |
40 fridge_setpoint: 16.0, | |
41 fridge_difference: 0.2, | |
42 overshoot_delay: 720, // 12 minutes | |
43 overshoot_factor: 1.0, | |
634 | 44 disabled: true, |
615 | 45 nowort: false, |
46 fridge_range_lower: 3.0, | |
47 fridge_range_upper: 3.0, | |
48 } | |
49 } | |
50 | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
51 fn try_load(filename: &str) -> Result<Params> { |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
52 let mut s = String::new(); |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
53 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
|
54 Ok(serde_json::from_str(&s)?) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
55 } |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
56 |
615 | 57 pub fn load(config: &Config) -> Params { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
58 Self::try_load(&config.params_file) |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
59 .unwrap_or_else(|_| Params::defaults()) |
615 | 60 } |
633 | 61 |
615 | 62 } |
63 | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
64 pub struct ParamWaiter { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
65 limitlog: NotTooOften, |
616 | 66 // last_etag is used for long-polling. |
67 last_etag: RefCell<String>, | |
68 epoch: String, | |
638 | 69 notify: ChannelRef<Params>, |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
70 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
71 config: Config, |
592 | 72 } |
73 | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
74 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
|
75 const MAX_RESPONSE_SIZE: usize = 10000; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
76 const TIMEOUT_MINUTES: u64 = 5; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
77 |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
78 impl ParamWaiter { |
638 | 79 pub fn new_actor((config, notify): (Config, ChannelRef<Params>)) -> Self { |
80 Self::new(config, notify) | |
81 } | |
633 | 82 |
638 | 83 pub fn new(config: Config, notify: ChannelRef<Params>) -> Self { |
633 | 84 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64 |
85 OsRng.fill_bytes(&mut b); | |
86 let epoch = base64::encode(&b); | |
87 | |
88 ParamWaiter { | |
89 limitlog: NotTooOften::new(LOG_MINUTES*60), | |
90 last_etag: RefCell::new(String::new()), | |
91 epoch: epoch, | |
92 config: config, | |
638 | 93 notify: notify, |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
94 } |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
95 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
96 |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
97 async fn wait_updates(uri: &str, etag: &str) -> Result<(Vec<u8>, u16)> { |
638 | 98 debug!("wait_updates {}", uri); |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
99 // XXX Workaround for https://github.com/dtolnay/anyhow/issues/35 |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
100 let mut resp = match surf::get(uri) |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
101 .set_header("etag", etag).await { |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
102 Ok(r) => r, |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
103 Err(e) => bail!(e), |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
104 }; |
633 | 105 |
634 | 106 // TODO timeout? |
107 let status = resp.status(); | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
108 let bytes = resp.body_bytes().await?; |
633 | 109 |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
110 Ok((bytes, status.into())) |
633 | 111 } |
112 | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
113 fn handle_response(&self, contents : &Vec<u8>, status: u16) -> Result<Params> { |
634 | 114 |
633 | 115 #[derive(Deserialize, Debug)] |
116 struct Response { | |
117 // sent as an opaque etag: header. Has format "epoch-nonce", | |
118 // responses where the epoch do not match ParamWaiter::epoch are dropped | |
119 etag: String, | |
120 params: Params, | |
121 } | |
122 | |
626 | 123 match status { |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
124 200 => { |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
125 // 200 OK |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
126 // new params |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
127 let r: Response = serde_json::from_slice(contents)?; |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
128 let mut le = self.last_etag.borrow_mut(); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
129 *le = r.etag; |
626 | 130 |
131 // update params if the epoch is correct | |
132 if let Some(e) = le.split('-').next() { | |
133 if e == &self.epoch { | |
134 self.write_params(&r.params); | |
135 return Ok(r.params); | |
136 } | |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
137 } |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
138 bail!("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
|
139 } |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
140 304 => { |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
141 // 304 Not Modified |
629 | 142 // XXX this isn't really an error. Should handle_response() return |
143 // Result<Option<Params>, TemplogError> instead? | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
144 bail!("304 unmodified (long polling timeout at the server)"); |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
145 }, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
146 _ => { |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
147 let text = String::from_utf8_lossy(contents); |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
148 bail!("Wrong server response code {}: {}", status, text); |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
149 }, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
150 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
151 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
152 |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
153 fn write_params(&self, params: &Params) { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
154 let p = atomicwrites::AtomicFile::new(&self.config.params_file, atomicwrites::AllowOverwrite); |
638 | 155 let r = p.write(|f| { |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
156 serde_json::to_writer(f, params) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
157 }); |
638 | 158 if let Err(e) = r { |
159 // XXX notify? | |
160 error!("Couldn't write to {}: {}", self.config.params_file, e) | |
161 }; | |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
162 } |
634 | 163 |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
164 fn do_poll(&mut self, ctx: &Context<<Self as Actor>::Msg>) -> Result<()> { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
165 let url = self.config.settings_url.clone(); |
634 | 166 let etag = self.last_etag.borrow().clone(); |
167 let h = ctx.run(async move { | |
168 Self::wait_updates(&url, &etag).await | |
169 }).expect("spawn failed"); // XXX error handling | |
639
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
170 let (contents, stat) = task::block_on(h)?; |
89818a14648b
- switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents:
638
diff
changeset
|
171 let new_params = self.handle_response(&contents, stat)?; |
638 | 172 self.notify.tell(Publish{msg: new_params, topic: "params".into()}, None); |
634 | 173 Ok(()) |
174 } | |
633 | 175 } |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
176 |
634 | 177 #[derive(Clone,Debug)] |
178 pub struct PollForParams; | |
179 | |
633 | 180 impl Actor for ParamWaiter { |
634 | 181 type Msg = PollForParams; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
182 |
634 | 183 fn recv(&mut self, |
184 ctx: &Context<Self::Msg>, | |
185 _msg: Self::Msg, | |
186 _sender: Sender) { | |
187 // schedule a retry once this iteration finishes | |
188 ctx.schedule_once(Duration::from_secs(1), ctx.myself(), None, PollForParams); | |
189 | |
190 if let Err(e) = self.do_poll(ctx) { | |
636
43eb3cfdf769
some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents:
635
diff
changeset
|
191 self.limitlog.and_then(|| { |
43eb3cfdf769
some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents:
635
diff
changeset
|
192 warn!("Problem fetching params: {}", e) |
43eb3cfdf769
some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents:
635
diff
changeset
|
193 }); |
634 | 194 } |
195 } | |
196 | |
197 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { | |
198 ctx.schedule_once(Duration::from_secs(1), ctx.myself(), None, PollForParams); | |
592 | 199 } |
200 } | |
201 | |
633 | 202 // pub fn stream(config: Config, handle: Handle) -> Result<(), TemplogError> { |
203 // let rcself = Rc::new(ParamWaiter::new(config, handle)); | |
204 | |
205 // let dur = Duration::from_millis(4000); | |
206 // for _ in Interval::new(dur, &rcself.handle).unwrap() { | |
207 // // fetch params | |
208 // // TODO - skip if inflight. | |
209 // let r = await!(rcself.make_request()).map_err(|e| TemplogError::new_hyper("response", e))?; | |
210 // let status = r.status(); | |
211 // let b = await!(r.body().concat2()).map_err(|e| TemplogError::new_hyper("body", e))?; | |
212 // if let Ok(params) = rcself.handle_response(b, status) { | |
213 // stream_yield!(params); | |
214 // } | |
215 // } | |
216 // Ok(()) | |
217 // } | |
218 |