@@ -108,43 +108,26 @@ impl Builder {
108108 ///
109109 /// This method panics if it can not create tokio runtime
110110 pub fn build < R : Runner > ( self , runner : R ) -> SystemRunner {
111- let name = self . name . clone ( ) ;
112- let testing = self . testing ;
113- let stack_size = self . stack_size ;
114- let stop_on_panic = self . stop_on_panic ;
115-
116- self . build_with ( SystemConfig {
117- name,
118- testing,
119- stack_size,
120- stop_on_panic,
111+ let config = SystemConfig {
112+ name : self . name . clone ( ) ,
113+ testing : self . testing ,
114+ stack_size : self . stack_size ,
115+ stop_on_panic : self . stop_on_panic ,
116+ ping_interval : self . ping_interval ,
117+ pool_limit : self . pool_limit ,
118+ pool_recv_timeout : self . pool_recv_timeout ,
121119 runner : Arc :: new ( runner) ,
122- } )
120+ } ;
121+ self . build_with ( config)
123122 }
124123
125124 /// Create new System.
126125 ///
127126 /// This method panics if it can not create tokio runtime
128127 pub fn build_with ( self , config : SystemConfig ) -> SystemRunner {
129- let ( stop_tx, stop) = oneshot:: channel ( ) ;
130- let ( sys_sender, sys_receiver) = unbounded ( ) ;
131-
132- // thread pool
133- let pool = ThreadPool :: new ( & self . name , self . pool_limit , self . pool_recv_timeout ) ;
134-
135- let ( arb, controller) = Arbiter :: new_system ( self . name . clone ( ) ) ;
136- let _ = sys_sender. try_send ( SystemCommand :: RegisterArbiter ( arb. id ( ) , arb. clone ( ) ) ) ;
137- let system = System :: construct ( sys_sender, arb, config. clone ( ) , pool) ;
138-
139- // system arbiter
140- let support = SystemSupport :: new ( stop_tx, sys_receiver, self . ping_interval ) ;
141-
142128 // init system arbiter and run configuration method
143129 SystemRunner {
144- stop,
145- support,
146- controller,
147- system,
130+ runner : config. runner . clone ( ) ,
148131 config,
149132 _t : PhantomData ,
150133 }
@@ -154,20 +137,12 @@ impl Builder {
154137/// Helper object that runs System's event loop
155138#[ must_use = "SystemRunner must be run" ]
156139pub struct SystemRunner {
157- stop : oneshot:: Receiver < i32 > ,
158- support : SystemSupport ,
159- controller : ArbiterController ,
160- system : System ,
161140 config : SystemConfig ,
141+ runner : Arc < dyn Runner > ,
162142 _t : PhantomData < Rc < ( ) > > ,
163143}
164144
165145impl SystemRunner {
166- /// Get current system.
167- pub fn system ( & self ) -> System {
168- self . system . clone ( )
169- }
170-
171146 /// This function will start event loop and will finish once the
172147 /// `System::stop()` function is called.
173148 pub fn run_until_stop ( self ) -> io:: Result < ( ) > {
@@ -182,16 +157,12 @@ impl SystemRunner {
182157 {
183158 log:: info!( "Starting {:?} system" , self . config. name) ;
184159
185- let SystemRunner {
186- controller,
187- stop,
188- support,
189- config,
190- ..
191- } = self ;
160+ let SystemRunner { config, runner, .. } = self ;
192161
193162 // run loop
194- config. block_on ( async move {
163+ crate :: driver:: block_on ( runner. as_ref ( ) , async move {
164+ let ( _, support, controller, stop) = create ( & config) ;
165+
195166 f ( ) ?;
196167
197168 crate :: spawn ( support. run ( ) ) ;
@@ -215,14 +186,11 @@ impl SystemRunner {
215186 F : Future < Output = R > + ' static ,
216187 R : ' static ,
217188 {
218- let SystemRunner {
219- controller,
220- support,
221- config,
222- ..
223- } = self ;
189+ let SystemRunner { config, runner, .. } = self ;
190+
191+ crate :: driver:: block_on ( runner. as_ref ( ) , async move {
192+ let ( _, support, controller, _) = create ( & config) ;
224193
225- config. block_on ( async move {
226194 crate :: spawn ( support. run ( ) ) ;
227195 crate :: spawn ( controller. run ( ) ) ;
228196 fut. await
@@ -236,15 +204,13 @@ impl SystemRunner {
236204 F : Future < Output = R > + ' static ,
237205 R : ' static ,
238206 {
239- let SystemRunner {
240- controller,
241- support,
242- ..
243- } = self ;
207+ let SystemRunner { config, .. } = self ;
244208
245209 // run loop
246210 tok_io:: task:: LocalSet :: new ( )
247211 . run_until ( async move {
212+ let ( sys, support, controller, _) = create ( & config) ;
213+
248214 crate :: spawn ( support. run ( ) ) ;
249215 crate :: spawn ( controller. run ( ) ) ;
250216 fut. await
@@ -256,9 +222,34 @@ impl SystemRunner {
256222impl fmt:: Debug for SystemRunner {
257223 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
258224 f. debug_struct ( "SystemRunner" )
259- . field ( "system" , & self . system )
260- . field ( "support" , & self . support )
261225 . field ( "config" , & self . config )
262226 . finish ( )
263227 }
264228}
229+
230+ /// Create new System.
231+ ///
232+ /// This method panics if it can not create tokio runtime
233+ fn create (
234+ config : & SystemConfig ,
235+ ) -> (
236+ System ,
237+ SystemSupport ,
238+ ArbiterController ,
239+ oneshot:: Receiver < i32 > ,
240+ ) {
241+ let ( stop_tx, stop) = oneshot:: channel ( ) ;
242+ let ( sys_sender, sys_receiver) = unbounded ( ) ;
243+
244+ // thread pool
245+ let pool = ThreadPool :: new ( & config. name , config. pool_limit , config. pool_recv_timeout ) ;
246+
247+ let ( arb, controller) = Arbiter :: new_system ( config. name . clone ( ) ) ;
248+ let _ = sys_sender. try_send ( SystemCommand :: RegisterArbiter ( arb. id ( ) , arb. clone ( ) ) ) ;
249+ let system = System :: construct ( sys_sender, arb, config. clone ( ) , pool) ;
250+
251+ // system arbiter
252+ let support = SystemSupport :: new ( stop_tx, sys_receiver, config. ping_interval ) ;
253+
254+ ( system, support, controller, stop)
255+ }
0 commit comments