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
use std::fmt;
use futures::{AsyncSink, Stream, StartSend, Poll, Async};
use futures::sync::mpsc::{self, channel, Sender};
use futures::sink::Sink;
use futures::stream::Fuse;
use futures::future::Future;
use tokio_core::reactor::Handle;
use metrics::Collect;
use error_log::{ErrorLog, ShutdownReason};
use config::{Queue, DefaultQueue, private};
#[derive(Debug)]
pub struct Pool<V, M> {
channel: Sender<V>,
metrics: M,
}
pub struct QueueError<V>(V);
#[derive(Debug)]
struct ForwardFuture<S, M, E>
where S: Sink
{
receiver: Fuse<mpsc::Receiver<S::SinkItem>>,
buffer: Option<S::SinkItem>,
metrics: M,
errors: E,
sink: S,
}
impl<I: 'static, M> private::NewQueue<I, M> for DefaultQueue {
type Pool = Pool<I, M>;
fn spawn_on<S, E>(self, pool: S, err: E, metrics: M, handle: &Handle)
-> Self::Pool
where S: Sink<SinkItem=I, SinkError=private::Done> + 'static,
E: ErrorLog + 'static,
M: Collect + 'static,
{
Queue(100).spawn_on(pool, err, metrics, handle)
}
}
impl<I: 'static, M> private::NewQueue<I, M> for Queue {
type Pool = Pool<I, M>;
fn spawn_on<S, E>(self, pool: S, e: E, metrics: M, handle: &Handle)
-> Self::Pool
where S: Sink<SinkItem=I, SinkError=private::Done> + 'static,
E: ErrorLog + 'static,
M: Collect + 'static,
{
let buf_size = self.0.saturating_sub(1);
let (tx, rx) = channel(buf_size);
handle.spawn(ForwardFuture {
receiver: rx.fuse(),
metrics: metrics.clone(),
errors: e,
sink: pool,
buffer: None,
});
return Pool {
channel: tx,
metrics,
};
}
}
trait AssertTraits: Clone + Send + Sync {}
impl<V: Send, M: Collect> AssertTraits for Pool<V, M> {}
impl<V, M: Clone> Clone for Pool<V, M> {
fn clone(&self) -> Self {
Pool {
channel: self.channel.clone(),
metrics: self.metrics.clone(),
}
}
}
impl<S, M, E> ForwardFuture<S, M, E>
where S: Sink<SinkError=private::Done>,
M: Collect,
E: ErrorLog,
{
fn poll_forever(&mut self) -> Async<()> {
if let Some(item) = self.buffer.take() {
match self.sink.start_send(item) {
Ok(AsyncSink::Ready) => {
self.metrics.request_forwarded();
}
Ok(AsyncSink::NotReady(item)) => {
self.buffer = Some(item);
return Async::NotReady;
}
Err(private::Done) => return Async::Ready(()),
}
}
let was_done = self.receiver.is_done();
loop {
match self.receiver.poll() {
Ok(Async::Ready(Some(item))) => {
match self.sink.start_send(item) {
Ok(AsyncSink::Ready) => {
self.metrics.request_forwarded();
continue;
}
Ok(AsyncSink::NotReady(item)) => {
self.buffer = Some(item);
return Async::NotReady;
}
Err(private::Done) => return Async::Ready(()),
}
}
Ok(Async::Ready(None)) => {
if !was_done {
self.errors.pool_shutting_down(
ShutdownReason::RequestStreamClosed);
}
match self.sink.close() {
Ok(Async::NotReady) => {
return Async::NotReady;
}
Ok(Async::Ready(())) | Err(private::Done) => {
return Async::Ready(());
}
}
}
Ok(Async::NotReady) => return Async::NotReady,
Err(()) => unreachable!(),
}
}
}
}
impl<S, M, E> Future for ForwardFuture<S, M, E>
where S: Sink<SinkError=private::Done>,
M: Collect,
E: ErrorLog,
{
type Item = ();
type Error = ();
fn poll(&mut self) -> Result<Async<()>, ()> {
match self.poll_forever() {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(()) => {
self.errors.pool_closed();
self.metrics.pool_closed();
Ok(Async::Ready(()))
}
}
}
}
impl<V, M> Sink for Pool<V, M>
where M: Collect,
{
type SinkItem=V;
type SinkError=QueueError<V>;
fn start_send(&mut self, item: Self::SinkItem)
-> StartSend<Self::SinkItem, Self::SinkError>
{
match self.channel.start_send(item) {
Ok(AsyncSink::Ready) => {
self.metrics.request_queued();
Ok(AsyncSink::Ready)
}
Ok(AsyncSink::NotReady(item)) => Ok(AsyncSink::NotReady(item)),
Err(e) => Err(QueueError(e.into_inner())),
}
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
self.channel.poll_complete()
.map_err(|_| {
unreachable!();
})
}
fn close(&mut self) -> Poll<(), Self::SinkError> {
self.channel.close()
.map_err(|_| {
unreachable!();
})
}
}
impl<T> QueueError<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> fmt::Display for QueueError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("connection pool is closed")
}
}
impl<T> fmt::Debug for QueueError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("QueueError(_)")
}
}
impl<T> ::std::error::Error for QueueError<T> {
fn description(&self) -> &str {
"QueueError"
}
fn cause(&self) -> Option<&::std::error::Error> {
None
}
}