-
Notifications
You must be signed in to change notification settings - Fork 281
/
lib.rs
321 lines (273 loc) · 8.05 KB
/
lib.rs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Buffer requests when the inner service is out of capacity.
//!
//! Buffering works by spawning a new task that is dedicated to pulling requests
//! out of the buffer and dispatching them to the inner service. By adding a
//! buffer and a dedicated task, the `Buffer` layer in front of the service can
//! be `Clone` even if the inner service is not.
//!
//! Currently, `Buffer` uses an unbounded buffering strategy, which is not a
//! good thing to put in production situations. However, it illustrates the idea
//! and capabilities around adding buffering to an arbitrary `Service`.
#[macro_use]
extern crate futures;
extern crate tower_service;
use futures::{Future, Stream, Poll, Async};
use futures::future::Executor;
use futures::sync::oneshot;
use futures::sync::mpsc::{self, UnboundedSender, UnboundedReceiver};
use tower_service::Service;
use std::{error, fmt};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
/// Adds a buffer in front of an inner service.
///
/// See crate level documentation for more details.
pub struct Buffer<T>
where T: Service,
{
tx: UnboundedSender<Message<T>>,
state: Arc<State>,
}
/// Future eventually completed with the response to the original request.
pub struct ResponseFuture<T>
where T: Service,
{
state: ResponseState<T::Future>,
}
/// Errors produced by `Buffer`.
#[derive(Debug)]
pub enum Error<T> {
Inner(T),
Closed,
}
/// Task that handles processing the buffer. This type should not be used
/// directly, instead `Buffer` requires an `Executor` that can accept this task.
pub struct Worker<T>
where T: Service,
{
current_message: Option<Message<T>>,
rx: UnboundedReceiver<Message<T>>,
service: T,
state: Arc<State>,
}
/// Error produced when spawning the worker fails
#[derive(Debug)]
pub struct SpawnError<T> {
inner: T,
}
/// Message sent over buffer
#[derive(Debug)]
struct Message<T: Service> {
request: T::Request,
tx: oneshot::Sender<T::Future>,
}
/// State shared between `Buffer` and `Worker`
struct State {
open: AtomicBool,
}
enum ResponseState<T> {
Rx(oneshot::Receiver<T>),
Poll(T),
}
impl<T> Buffer<T>
where T: Service,
{
/// Creates a new `Buffer` wrapping `service`.
///
/// `executor` is used to spawn a new `Worker` task that is dedicated to
/// draining the buffer and dispatching the requests to the internal
/// service.
pub fn new<E>(service: T, executor: &E) -> Result<Self, SpawnError<T>>
where E: Executor<Worker<T>>,
{
let (tx, rx) = mpsc::unbounded();
let state = Arc::new(State {
open: AtomicBool::new(true),
});
let worker = Worker {
current_message: None,
rx,
service,
state: state.clone(),
};
// TODO: handle error
executor.execute(worker)
.ok().unwrap();
Ok(Buffer {
tx,
state: state,
})
}
}
impl<T> Service for Buffer<T>
where T: Service,
{
type Request = T::Request;
type Response = T::Response;
type Error = Error<T::Error>;
type Future = ResponseFuture<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
// If the inner service has errored, then we error here.
if !self.state.open.load(Ordering::Acquire) {
return Err(Error::Closed);
} else {
// Ideally we could query if the `mpsc` is closed, but this is not
// currently possible.
Ok(().into())
}
}
fn call(&mut self, request: Self::Request) -> Self::Future {
let (tx, rx) = oneshot::channel();
let sent = self.tx.unbounded_send(Message {
request,
tx,
});
if sent.is_err() {
self.state.open.store(false, Ordering::Release);
}
ResponseFuture { state: ResponseState::Rx(rx) }
}
}
impl<T> Clone for Buffer<T>
where T: Service
{
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
state: self.state.clone(),
}
}
}
// ===== impl ResponseFuture =====
impl<T> Future for ResponseFuture<T>
where T: Service
{
type Item = T::Response;
type Error = Error<T::Error>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
use self::ResponseState::*;
loop {
let fut;
match self.state {
Rx(ref mut rx) => {
match rx.poll() {
Ok(Async::Ready(f)) => fut = f,
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(_) => return Err(Error::Closed),
}
}
Poll(ref mut fut) => {
return fut.poll().map_err(Error::Inner);
}
}
self.state = Poll(fut);
}
}
}
// ===== impl Worker =====
impl<T> Worker<T>
where T: Service
{
/// Return the next queued Message that hasn't been canceled.
fn poll_next_msg(&mut self) -> Poll<Option<Message<T>>, ()> {
if let Some(mut msg) = self.current_message.take() {
// poll_cancel returns Async::Ready is the receiver is dropped.
// Returning NotReady means it is still alive, so we should still
// use it.
if msg.tx.poll_cancel()?.is_not_ready() {
return Ok(Async::Ready(Some(msg)));
}
}
// Get the next request
while let Some(mut msg) = try_ready!(self.rx.poll()) {
if msg.tx.poll_cancel()?.is_not_ready() {
return Ok(Async::Ready(Some(msg)));
}
// Otherwise, request is canceled, so pop the next one.
}
Ok(Async::Ready(None))
}
}
impl<T> Future for Worker<T>
where T: Service,
{
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<(), ()> {
while let Some(msg) = try_ready!(self.poll_next_msg()) {
// Wait for the service to be ready
match self.service.poll_ready() {
Ok(Async::Ready(())) => {
let response = self.service.call(msg.request);
// Send the response future back to the sender.
//
// An error means the request had been canceled in-between
// our calls, the response future will just be dropped.
let _ = msg.tx.send(response);
}
Ok(Async::NotReady) => {
// Put out current message back in its slot.
self.current_message = Some(msg);
return Ok(Async::NotReady);
}
Err(_) => {
self.state.open.store(false, Ordering::Release);
return Ok(().into())
}
}
}
// All senders are dropped... the task is no longer needed
Ok(().into())
}
}
// ===== impl Error =====
impl<T> fmt::Display for Error<T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Inner(ref why) => fmt::Display::fmt(why, f),
Error::Closed => f.pad("buffer closed"),
}
}
}
impl<T> error::Error for Error<T>
where
T: error::Error,
{
fn cause(&self) -> Option<&error::Error> {
if let Error::Inner(ref why) = *self {
Some(why)
} else {
None
}
}
fn description(&self) -> &str {
match *self {
Error::Inner(ref e) => e.description(),
Error::Closed => "buffer closed",
}
}
}
// ===== impl SpawnError =====
impl<T> fmt::Display for SpawnError<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error spawning buffer task: {:?}", self.inner)
}
}
impl<T> error::Error for SpawnError<T>
where
T: error::Error,
{
fn cause(&self) -> Option<&error::Error> {
Some(&self.inner)
}
fn description(&self) -> &str {
"error spawning buffer task"
}
}