@@ -30,14 +30,16 @@ pub struct TrySendError<T> {
3030
3131pub ( crate ) fn channel < T , U > ( ) -> ( Sender < T , U > , Receiver < T , U > ) {
3232 let ( tx, rx) = mpsc:: unbounded_channel ( ) ;
33- let ( giver, taker) = want:: new ( ) ;
34- let tx = Sender {
35- #[ cfg( feature = "http1" ) ]
36- buffered_once : false ,
37- giver,
38- inner : tx,
33+ let ( giver, mut taker) = want:: new ( ) ;
34+ // Seed with one want so the first request can be sent before the
35+ // receiver is polled; every request must consume a want
36+ taker. want ( ) ;
37+ let tx = Sender { giver, inner : tx } ;
38+ let rx = Receiver {
39+ inner : rx,
40+ taker,
41+ wanting : true ,
3942 } ;
40- let rx = Receiver { inner : rx, taker } ;
4143 ( tx, rx)
4244}
4345
@@ -46,17 +48,11 @@ pub(crate) fn channel<T, U>() -> (Sender<T, U>, Receiver<T, U>) {
4648/// While the inner sender is unbounded, the Giver is used to determine
4749/// if the Receiver is ready for another request.
4850pub ( crate ) struct Sender < T , U > {
49- /// One message is always allowed, even if the Receiver hasn't asked
50- /// for it yet. This boolean keeps track of whether we've sent one
51- /// without notice.
52- #[ cfg( feature = "http1" ) ]
53- buffered_once : bool ,
54- /// The Giver helps watch that the Receiver side has been polled
55- /// when the queue is empty. This helps us know when a request and
56- /// response have been fully processed, and a connection is ready
57- /// for more.
51+ // Controls whether the sender is permitted to dispatch a request
52+ // Beyond the initial request, the Receiver only grants readiness once
53+ // the preceding request/response cycle has fully completed
5854 giver : want:: Giver ,
59- /// Actually bounded by the Giver, plus `buffered_once`.
55+ // Capacity is enforced via the Giver
6056 inner : mpsc:: UnboundedSender < Envelope < T , U > > ,
6157}
6258
@@ -89,23 +85,9 @@ impl<T, U> Sender<T, U> {
8985 self . giver . is_canceled ( )
9086 }
9187
92- #[ cfg( feature = "http1" ) ]
93- fn can_send ( & mut self ) -> bool {
94- if self . giver . give ( ) || !self . buffered_once {
95- // If the receiver is ready *now*, then of course we can send.
96- //
97- // If the receiver isn't ready yet, but we don't have anything
98- // in the channel yet, then allow one message.
99- self . buffered_once = true ;
100- true
101- } else {
102- false
103- }
104- }
105-
10688 #[ cfg( feature = "http1" ) ]
10789 pub ( crate ) fn try_send ( & mut self , val : T ) -> Result < RetryPromise < T , U > , T > {
108- if !self . can_send ( ) {
90+ if !self . giver . give ( ) {
10991 return Err ( val) ;
11092 }
11193 let ( tx, rx) = oneshot:: channel ( ) ;
@@ -117,7 +99,7 @@ impl<T, U> Sender<T, U> {
11799
118100 #[ cfg( feature = "http1" ) ]
119101 pub ( crate ) fn send ( & mut self , val : T ) -> Result < Promise < U > , T > {
120- if !self . can_send ( ) {
102+ if !self . giver . give ( ) {
121103 return Err ( val) ;
122104 }
123105 let ( tx, rx) = oneshot:: channel ( ) ;
@@ -176,34 +158,33 @@ impl<T, U> Clone for UnboundedSender<T, U> {
176158pub ( crate ) struct Receiver < T , U > {
177159 inner : mpsc:: UnboundedReceiver < Envelope < T , U > > ,
178160 taker : want:: Taker ,
161+ // Tracks whether readiness (`want`) has already been granted for the next request
162+ wanting : bool ,
179163}
180164
181165impl < T , U > Receiver < T , U > {
182166 pub ( crate ) fn poll_recv ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Option < ( T , Callback < T , U > ) > > {
167+ if !self . wanting {
168+ self . taker . want ( ) ;
169+ self . wanting = true ;
170+ }
183171 match self . inner . poll_recv ( cx) {
184172 Poll :: Ready ( item) => {
185- // A want signaled after finding the queue empty can land after
186- // the Sender's `give()` for the message taken here, as can one
187- // signaled on a coop-budget Pending with a message queued.
188- // Withdraw it, or the Sender reports this connection as ready
189- // while it is still serving this message.
190- self . taker . unwant ( ) ;
173+ self . wanting = false ;
191174 Poll :: Ready ( item. map ( |mut env| env. 0 . take ( ) . expect ( "envelope not dropped" ) ) )
192175 }
193- Poll :: Pending => {
194- self . taker . want ( ) ;
195- Poll :: Pending
196- }
176+ // Avoid re-signaling `want` here: the sender might have already claimed it
177+ // and enqueued a request, even if polling the channel returns `Pending`
178+ // due to Tokio's coop budget exhaustion or a concurrent send
179+ Poll :: Pending => Poll :: Pending ,
197180 }
198181 }
199182
200- #[ cfg( feature = "http1" ) ]
201183 pub ( crate ) fn close ( & mut self ) {
202184 self . taker . cancel ( ) ;
203185 self . inner . close ( ) ;
204186 }
205187
206- #[ cfg( feature = "http1" ) ]
207188 pub ( crate ) fn poll_close ( & mut self , cx : & mut Context < ' _ > ) -> Poll < ( ) > {
208189 self . close ( ) ;
209190 // A send that started before close may still be publishing its envelope.
0 commit comments