@@ -28,11 +28,9 @@ use crate::socket::MioStream;
28
28
use crate :: waker_queue:: { WakerInterest , WakerQueue } ;
29
29
use crate :: { join_all, Token } ;
30
30
31
- pub ( crate ) struct WorkerCommand ( Conn ) ;
32
-
33
- /// Stop worker message. Returns `true` on successful shutdown
34
- /// and `false` if some connections still alive.
35
- pub ( crate ) struct StopCommand {
31
+ /// Stop worker message. Returns `true` on successful graceful shutdown.
32
+ /// and `false` if some connections still alive when shutdown execute.
33
+ pub ( crate ) struct Stop {
36
34
graceful : bool ,
37
35
tx : oneshot:: Sender < bool > ,
38
36
}
@@ -43,42 +41,55 @@ pub(crate) struct Conn {
43
41
pub token : Token ,
44
42
}
45
43
46
- // a handle to worker that can send message to worker and share the availability of worker to other
47
- // thread.
48
- #[ derive( Clone ) ]
49
- pub ( crate ) struct WorkerHandle {
50
- pub idx : usize ,
51
- tx1 : UnboundedSender < WorkerCommand > ,
52
- tx2 : UnboundedSender < StopCommand > ,
44
+ fn handle_pair (
45
+ idx : usize ,
46
+ tx1 : UnboundedSender < Conn > ,
47
+ tx2 : UnboundedSender < Stop > ,
53
48
avail : WorkerAvailability ,
49
+ ) -> ( WorkerHandleAccept , WorkerHandleServer ) {
50
+ let accept = WorkerHandleAccept {
51
+ idx,
52
+ tx : tx1,
53
+ avail,
54
+ } ;
55
+
56
+ let server = WorkerHandleServer { idx, tx : tx2 } ;
57
+
58
+ ( accept, server)
54
59
}
55
60
56
- impl WorkerHandle {
57
- pub fn new (
58
- idx : usize ,
59
- tx1 : UnboundedSender < WorkerCommand > ,
60
- tx2 : UnboundedSender < StopCommand > ,
61
- avail : WorkerAvailability ,
62
- ) -> Self {
63
- WorkerHandle {
64
- idx,
65
- tx1,
66
- tx2,
67
- avail,
68
- }
69
- }
61
+ /// Handle to worker that can send connection message to worker and share the
62
+ /// availability of worker to other thread.
63
+ ///
64
+ /// Held by [Accept](crate::accept::Accept).
65
+ pub ( crate ) struct WorkerHandleAccept {
66
+ pub idx : usize ,
67
+ tx : UnboundedSender < Conn > ,
68
+ avail : WorkerAvailability ,
69
+ }
70
70
71
- pub fn send ( & self , msg : Conn ) -> Result < ( ) , Conn > {
72
- self . tx1 . send ( WorkerCommand ( msg) ) . map_err ( |msg| msg. 0 . 0 )
71
+ impl WorkerHandleAccept {
72
+ pub ( crate ) fn send ( & self , msg : Conn ) -> Result < ( ) , Conn > {
73
+ self . tx . send ( msg) . map_err ( |msg| msg. 0 )
73
74
}
74
75
75
- pub fn available ( & self ) -> bool {
76
+ pub ( crate ) fn available ( & self ) -> bool {
76
77
self . avail . available ( )
77
78
}
79
+ }
80
+
81
+ /// Handle to worker than can send stop message to worker.
82
+ ///
83
+ /// Held by [ServerBuilder](crate::builder::ServerBuilder).
84
+ pub ( crate ) struct WorkerHandleServer {
85
+ pub idx : usize ,
86
+ tx : UnboundedSender < Stop > ,
87
+ }
78
88
79
- pub fn stop ( & self , graceful : bool ) -> oneshot:: Receiver < bool > {
89
+ impl WorkerHandleServer {
90
+ pub ( crate ) fn stop ( & self , graceful : bool ) -> oneshot:: Receiver < bool > {
80
91
let ( tx, rx) = oneshot:: channel ( ) ;
81
- let _ = self . tx2 . send ( StopCommand { graceful, tx } ) ;
92
+ let _ = self . tx . send ( Stop { graceful, tx } ) ;
82
93
rx
83
94
}
84
95
}
@@ -114,8 +125,8 @@ impl WorkerAvailability {
114
125
///
115
126
/// Worker accepts Socket objects via unbounded channel and starts stream processing.
116
127
pub ( crate ) struct ServerWorker {
117
- rx : UnboundedReceiver < WorkerCommand > ,
118
- rx2 : UnboundedReceiver < StopCommand > ,
128
+ rx : UnboundedReceiver < Conn > ,
129
+ rx2 : UnboundedReceiver < Stop > ,
119
130
services : Vec < WorkerService > ,
120
131
availability : WorkerAvailability ,
121
132
conns : Counter ,
@@ -187,7 +198,7 @@ impl ServerWorker {
187
198
factories : Vec < Box < dyn InternalServiceFactory > > ,
188
199
availability : WorkerAvailability ,
189
200
config : ServerWorkerConfig ,
190
- ) -> WorkerHandle {
201
+ ) -> ( WorkerHandleAccept , WorkerHandleServer ) {
191
202
let ( tx1, rx) = unbounded_channel ( ) ;
192
203
let ( tx2, rx2) = unbounded_channel ( ) ;
193
204
let avail = availability. clone ( ) ;
@@ -254,7 +265,7 @@ impl ServerWorker {
254
265
} ) ;
255
266
} ) ;
256
267
257
- WorkerHandle :: new ( idx, tx1, tx2, avail)
268
+ handle_pair ( idx, tx1, tx2, avail)
258
269
}
259
270
260
271
fn restart_service ( & mut self , token : Token , factory_id : usize ) {
@@ -360,8 +371,7 @@ impl Future for ServerWorker {
360
371
let this = self . as_mut ( ) . get_mut ( ) ;
361
372
362
373
// `StopWorker` message handler
363
- if let Poll :: Ready ( Some ( StopCommand { graceful, tx } ) ) =
364
- Pin :: new ( & mut this. rx2 ) . poll_recv ( cx)
374
+ if let Poll :: Ready ( Some ( Stop { graceful, tx } ) ) = Pin :: new ( & mut this. rx2 ) . poll_recv ( cx)
365
375
{
366
376
this. availability . set ( false ) ;
367
377
let num = this. conns . total ( ) ;
@@ -472,7 +482,7 @@ impl Future for ServerWorker {
472
482
473
483
match ready ! ( Pin :: new( & mut this. rx) . poll_recv( cx) ) {
474
484
// handle incoming io stream
475
- Some ( WorkerCommand ( msg) ) => {
485
+ Some ( msg) => {
476
486
let guard = this. conns . get ( ) ;
477
487
let _ = this. services [ msg. token . 0 ] . service . call ( ( guard, msg. io ) ) ;
478
488
}
0 commit comments