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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use super::EventLoop;
use crate::{
errors, internal::BoxFuture, prelude::*, types::parameters::Updates,
};
use futures::Stream;
use std::{
convert::TryInto,
num::NonZeroUsize,
sync::{Arc, Mutex},
time::Duration,
};
use tokio::{
timer::{self, timeout},
util::FutureExt,
};
mod schedule;
use schedule::Schedule;
type Error = timeout::Error<errors::MethodCall>;
type ErrorHandler = dyn FnMut(Error) + Send + Sync;
#[must_use = "polling does nothing unless `start` is called"]
pub struct Polling<C> {
event_loop: EventLoop<C>,
limit: Option<u8>,
timeout: Option<u64>,
allowed_updates: Option<&'static [Updates]>,
poll_interval: Duration,
error_handler: Mutex<Box<ErrorHandler>>,
request_timeout: Option<Duration>,
offset: Option<isize>,
}
impl<C> Polling<C> {
pub(crate) fn new(event_loop: EventLoop<C>) -> Self {
Self {
event_loop,
limit: None,
timeout: None,
allowed_updates: None,
poll_interval: Duration::from_millis(25),
error_handler: Mutex::new(Box::new(|err| {
panic!("\n[tbot] Polling error: {:#?}", err);
})),
request_timeout: None,
offset: None,
}
}
pub fn limit(mut self, limit: u8) -> Self {
self.limit = Some(limit);
self
}
pub fn timeout(mut self, timeout: u64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn allowed_updates(
mut self,
allowed_updates: &'static [Updates],
) -> Self {
self.allowed_updates = Some(allowed_updates);
self
}
pub fn error_handler(
mut self,
handler: impl FnMut(Error) + Send + Sync + 'static,
) -> Self {
self.error_handler = Mutex::new(Box::new(handler));
self
}
pub const fn poll_interval(mut self, poll_interval: Duration) -> Self {
self.poll_interval = poll_interval;
self
}
pub const fn request_timeout(mut self, timeout: Duration) -> Self {
self.request_timeout = Some(timeout);
self
}
pub fn last_n_updates(mut self, n: NonZeroUsize) -> Self {
let n: isize = n.get().try_into().unwrap();
self.offset = Some(-n);
self
}
}
impl<C> Polling<C>
where
C: hyper::client::connect::Connect + Clone + Sync + 'static,
C::Transport: 'static,
C::Future: 'static,
{
pub fn start(self) -> ! {
let future = self
.delete_webhook()
.map_err(|err| {
eprintln!(
"[tbot] Error while deleting previous webhook. \
Starting polling anyway. {:#?}",
err,
);
})
.then(|_| {
self.into_future().map_err(|err| {
eprintln!("[tbot] Poll schedule error: {:#?}", err);
})
});
crate::run(future);
panic!(
"\n[tbot] Polling event loop unexpected returned. \
An error should be printed above.\n"
);
}
pub fn delete_webhook(&self) -> impl Future<Item = (), Error = Error> {
let request_timeout = self.request_timeout.unwrap_or_else(|| {
Duration::from_secs(self.timeout.unwrap_or(0) + 60)
});
self.event_loop
.bot
.delete_webhook()
.into_future()
.timeout(request_timeout)
}
}
impl<C> IntoFuture for Polling<C>
where
C: hyper::client::connect::Connect + Clone + Sync + 'static,
C::Transport: 'static,
C::Future: 'static,
{
type Future = BoxFuture<Self::Item, Self::Error>;
type Item = ();
type Error = timer::Error;
fn into_future(self) -> Self::Future {
let Self {
event_loop,
poll_interval,
limit,
timeout,
allowed_updates,
error_handler,
request_timeout,
offset,
} = self;
let request_timeout = request_timeout
.unwrap_or_else(|| Duration::from_secs(timeout.unwrap_or(0) + 60));
let bot = Arc::new(event_loop.bot.clone());
let event_loop = Arc::new(event_loop);
let error_handler = Arc::new(error_handler);
let stream = Schedule::new(offset, poll_interval).into_stream();
let stream = stream.for_each(move |(last_offset, schedule)| {
let bot = Arc::clone(&bot);
let event_loop = Arc::clone(&event_loop);
let error_handler = Arc::clone(&error_handler);
let on_error_schedule = Arc::clone(&schedule);
let handler = bot
.get_updates(last_offset, limit, timeout, allowed_updates)
.into_future()
.map(move |updates| {
let mut schedule = schedule.lock().unwrap();
if let Some(update) = updates.last() {
schedule.last_offset = Some(update.id.0 + 1);
}
schedule.schedule_next_tick();
std::mem::drop(schedule);
for update in updates {
event_loop.handle_update(Arc::clone(&bot), update);
}
})
.timeout(request_timeout)
.map_err(move |error| {
(&mut *(*error_handler).lock().unwrap())(error);
on_error_schedule.lock().unwrap().schedule_next_tick();
});
crate::spawn(handler);
Ok(())
});
Box::new(stream)
}
}