1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::{
api::Api,
methods::GetUpdates,
types::{AllowedUpdate, Integer, ResponseError, Update},
};
use failure::Error;
use futures::{task, Async, Future, Poll, Stream};
use log::error;
use std::{
cmp::max,
collections::{HashSet, VecDeque},
time::Duration,
};
use tokio_timer::sleep;
const DEFAULT_LIMIT: Integer = 100;
const DEFAULT_POLL_TIMEOUT: Integer = 10;
const DEFAULT_ERROR_TIMEOUT: u64 = 5;
pub struct UpdatesStream {
api: Api,
options: UpdatesStreamOptions,
items: VecDeque<Update>,
request: Option<Box<Future<Item = Option<Vec<Update>>, Error = Error> + Send>>,
}
impl UpdatesStream {
pub fn new(api: Api) -> Self {
UpdatesStream {
api,
options: UpdatesStreamOptions::default(),
items: VecDeque::new(),
request: None,
}
}
pub fn options(mut self, options: UpdatesStreamOptions) -> Self {
self.options = options;
self
}
}
impl From<Api> for UpdatesStream {
fn from(api: Api) -> UpdatesStream {
UpdatesStream::new(api)
}
}
impl Stream for UpdatesStream {
type Item = Update;
type Error = Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
if let Some(update) = self.items.pop_front() {
return Ok(Async::Ready(Some(update)));
}
let options = &mut self.options;
let should_request = match self.request {
Some(ref mut request) => match request.poll() {
Ok(Async::Ready(Some(items))) => {
for i in items {
options.offset = max(options.offset, i.id);
self.items.push_back(i);
}
Ok(())
}
Ok(Async::Ready(None)) => Ok(()),
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(err) => Err(err),
},
None => Ok(()),
};
match should_request {
Ok(()) => {
self.request = Some(Box::new(
self.api
.execute(
&GetUpdates::default()
.offset(options.offset + 1)
.limit(options.limit)
.timeout(options.poll_timeout)
.allowed_updates(options.allowed_updates.clone()),
)
.map(Some),
));
}
Err(err) => {
error!("An error has occurred while getting updates: {:?}", err);
options.error_timeout = Duration::from_secs(
err.downcast::<ResponseError>()
.ok()
.and_then(|err| {
err.parameters
.and_then(|parameters| parameters.retry_after.map(|count| count as u64))
})
.unwrap_or(DEFAULT_ERROR_TIMEOUT),
);
self.request = Some(Box::new(sleep(options.error_timeout).from_err().map(|()| None)));
}
};
task::current().notify();
Ok(Async::NotReady)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct UpdatesStreamOptions {
offset: Integer,
limit: Integer,
poll_timeout: Integer,
error_timeout: Duration,
allowed_updates: HashSet<AllowedUpdate>,
}
impl UpdatesStreamOptions {
pub fn limit(mut self, limit: Integer) -> Self {
self.limit = limit;
self
}
pub fn poll_timeout(mut self, poll_timeout: Integer) -> Self {
self.poll_timeout = poll_timeout;
self
}
pub fn error_timeout(mut self, error_timeout: u64) -> Self {
self.error_timeout = Duration::from_secs(error_timeout);
self
}
pub fn allowed_update(mut self, allowed_update: AllowedUpdate) -> Self {
self.allowed_updates.insert(allowed_update);
self
}
}
impl Default for UpdatesStreamOptions {
fn default() -> Self {
UpdatesStreamOptions {
offset: 0,
limit: DEFAULT_LIMIT,
poll_timeout: DEFAULT_POLL_TIMEOUT,
error_timeout: Duration::from_secs(DEFAULT_ERROR_TIMEOUT),
allowed_updates: HashSet::new(),
}
}
}