Skip to content

Commit 8dfc923

Browse files
committed
GetConn: move queue_id up one level, remove duplicated code
1 parent b6f3cb3 commit 8dfc923

File tree

2 files changed

+19
-58
lines changed

2 files changed

+19
-58
lines changed

src/conn/pool/futures/get_conn.rs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use crate::{
2626
/// States of the GetConn future.
2727
pub(crate) enum GetConnInner {
2828
New,
29-
Queued(QueueId),
3029
Done,
3130
// TODO: one day this should be an existential
3231
Connecting(crate::BoxFuture<'static, Conn>),
@@ -38,10 +37,6 @@ impl fmt::Debug for GetConnInner {
3837
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3938
match self {
4039
GetConnInner::New => f.debug_tuple("GetConnInner::New").finish(),
41-
GetConnInner::Queued(queue_id) => f
42-
.debug_tuple("GetConnInner::Queued")
43-
.field(queue_id)
44-
.finish(),
4540
GetConnInner::Done => f.debug_tuple("GetConnInner::Done").finish(),
4641
GetConnInner::Connecting(_) => f
4742
.debug_tuple("GetConnInner::Connecting")
@@ -66,13 +61,15 @@ impl GetConnInner {
6661
#[derive(Debug)]
6762
#[must_use = "futures do nothing unless you `.await` or poll them"]
6863
pub struct GetConn {
64+
pub(crate) queue_id: Option<QueueId>,
6965
pub(crate) pool: Option<Pool>,
7066
pub(crate) inner: GetConnInner,
7167
}
7268

7369
impl GetConn {
7470
pub(crate) fn new(pool: &Pool) -> GetConn {
7571
GetConn {
72+
queue_id: None,
7673
pool: Some(pool.clone()),
7774
inner: GetConnInner::New,
7875
}
@@ -100,40 +97,11 @@ impl Future for GetConn {
10097
loop {
10198
match self.inner {
10299
GetConnInner::New => {
103-
let queue_id = QueueId::next();
104-
match Pin::new(self.pool_mut()).poll_new_conn(cx, false, queue_id) {
105-
Poll::Pending => {
106-
self.inner = GetConnInner::Queued(queue_id);
107-
return Poll::Pending;
108-
}
109-
Poll::Ready(res) => match res?.inner.take() {
110-
GetConnInner::Connecting(conn_fut) => {
111-
self.inner = GetConnInner::Connecting(conn_fut);
112-
}
113-
GetConnInner::Checking(conn_fut) => {
114-
self.inner = GetConnInner::Checking(conn_fut);
115-
}
116-
GetConnInner::Done => unreachable!(
117-
"Pool::poll_new_conn never gives out already-consumed GetConns"
118-
),
119-
GetConnInner::New => {
120-
unreachable!(
121-
"Pool::poll_new_conn never gives out GetConnInner::New"
122-
)
123-
}
124-
GetConnInner::Queued(_) => {
125-
unreachable!(
126-
"Pool::poll_new_conn never gives out GetConnInner::Queued"
127-
)
128-
}
129-
},
130-
}
131-
}
132-
GetConnInner::Queued(queue_id) => {
133-
match ready!(Pin::new(self.pool_mut()).poll_new_conn(cx, true, queue_id))?
134-
.inner
135-
.take()
136-
{
100+
let queued = self.queue_id.is_some();
101+
let queue_id = *self.queue_id.get_or_insert_with(|| QueueId::next());
102+
let next =
103+
ready!(Pin::new(self.pool_mut()).poll_new_conn(cx, queued, queue_id))?;
104+
match next {
137105
GetConnInner::Connecting(conn_fut) => {
138106
self.inner = GetConnInner::Connecting(conn_fut);
139107
}
@@ -146,9 +114,6 @@ impl Future for GetConn {
146114
GetConnInner::New => {
147115
unreachable!("Pool::poll_new_conn never gives out GetConnInner::New")
148116
}
149-
GetConnInner::Queued(_) => {
150-
unreachable!("Pool::poll_new_conn never gives out GetConnInner::Queued")
151-
}
152117
}
153118
}
154119
GetConnInner::Done => {

src/conn/pool/mod.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl Pool {
273273
cx: &mut Context<'_>,
274274
queued: bool,
275275
queue_id: QueueId,
276-
) -> Poll<Result<GetConn>> {
276+
) -> Poll<Result<GetConnInner>> {
277277
self.poll_new_conn_inner(cx, queued, queue_id)
278278
}
279279

@@ -282,7 +282,7 @@ impl Pool {
282282
cx: &mut Context<'_>,
283283
queued: bool,
284284
queue_id: QueueId,
285-
) -> Poll<Result<GetConn>> {
285+
) -> Poll<Result<GetConnInner>> {
286286
let mut exchange = self.inner.exchange.lock().unwrap();
287287

288288
// NOTE: this load must happen while we hold the lock,
@@ -304,16 +304,13 @@ impl Pool {
304304

305305
while let Some(IdlingConn { mut conn, .. }) = exchange.available.pop_back() {
306306
if !conn.expired() {
307-
return Poll::Ready(Ok(GetConn {
308-
pool: Some(self.clone()),
309-
inner: GetConnInner::Checking(
310-
async move {
311-
conn.stream_mut()?.check().await?;
312-
Ok(conn)
313-
}
314-
.boxed(),
315-
),
316-
}));
307+
return Poll::Ready(Ok(GetConnInner::Checking(
308+
async move {
309+
conn.stream_mut()?.check().await?;
310+
Ok(conn)
311+
}
312+
.boxed(),
313+
)));
317314
} else {
318315
self.send_to_recycler(conn);
319316
}
@@ -325,10 +322,9 @@ impl Pool {
325322
// we are allowed to make a new connection, so we will!
326323
exchange.exist += 1;
327324

328-
return Poll::Ready(Ok(GetConn {
329-
pool: Some(self.clone()),
330-
inner: GetConnInner::Connecting(Conn::new(self.opts.clone()).boxed()),
331-
}));
325+
return Poll::Ready(Ok(GetConnInner::Connecting(
326+
Conn::new(self.opts.clone()).boxed(),
327+
)));
332328
}
333329

334330
// Polled, but no conn available? Back into the queue.

0 commit comments

Comments
 (0)