@@ -8,12 +8,12 @@ use async_channel::{self as channel, Receiver, Sender};
8
8
use futures:: future:: try_join_all;
9
9
use futures_lite:: FutureExt ;
10
10
use rand:: Rng ;
11
- use tokio:: sync:: { RwLock , RwLockWriteGuard , oneshot} ;
11
+ use tokio:: sync:: { RwLock , oneshot} ;
12
12
use tokio:: task;
13
13
use tokio_util:: sync:: CancellationToken ;
14
14
use tokio_util:: task:: TaskTracker ;
15
15
16
- use self :: connectivity:: ConnectivityStore ;
16
+ pub ( crate ) use self :: connectivity:: ConnectivityStore ;
17
17
use crate :: config:: { self , Config } ;
18
18
use crate :: constants;
19
19
use crate :: contact:: { ContactId , RecentlySeenLoop } ;
@@ -53,32 +53,32 @@ impl SchedulerState {
53
53
}
54
54
55
55
/// Starts the scheduler if it is not yet started.
56
- pub ( crate ) async fn start ( & self , context : Context ) {
56
+ pub ( crate ) async fn start ( & self , context : & Context ) {
57
57
let mut inner = self . inner . write ( ) . await ;
58
58
match * inner {
59
59
InnerSchedulerState :: Started ( _) => ( ) ,
60
- InnerSchedulerState :: Stopped => Self :: do_start ( inner, context) . await ,
60
+ InnerSchedulerState :: Stopped => Self :: do_start ( & mut inner, context) . await ,
61
61
InnerSchedulerState :: Paused {
62
62
ref mut started, ..
63
63
} => * started = true ,
64
64
}
65
+ context. update_connectivities ( & inner) ;
65
66
}
66
67
67
68
/// Starts the scheduler if it is not yet started.
68
- async fn do_start ( mut inner : RwLockWriteGuard < ' _ , InnerSchedulerState > , context : Context ) {
69
+ async fn do_start ( inner : & mut InnerSchedulerState , context : & Context ) {
69
70
info ! ( context, "starting IO" ) ;
70
71
71
72
// Notify message processing loop
72
73
// to allow processing old messages after restart.
73
74
context. new_msgs_notify . notify_one ( ) ;
74
75
75
- let ctx = context. clone ( ) ;
76
- match Scheduler :: start ( & context) . await {
76
+ match Scheduler :: start ( context) . await {
77
77
Ok ( scheduler) => {
78
78
* inner = InnerSchedulerState :: Started ( scheduler) ;
79
79
context. emit_event ( EventType :: ConnectivityChanged ) ;
80
80
}
81
- Err ( err) => error ! ( & ctx , "Failed to start IO: {:#}" , err) ,
81
+ Err ( err) => error ! ( context , "Failed to start IO: {:#}" , err) ,
82
82
}
83
83
}
84
84
@@ -87,18 +87,19 @@ impl SchedulerState {
87
87
let mut inner = self . inner . write ( ) . await ;
88
88
match * inner {
89
89
InnerSchedulerState :: Started ( _) => {
90
- Self :: do_stop ( inner, context, InnerSchedulerState :: Stopped ) . await
90
+ Self :: do_stop ( & mut inner, context, InnerSchedulerState :: Stopped ) . await
91
91
}
92
92
InnerSchedulerState :: Stopped => ( ) ,
93
93
InnerSchedulerState :: Paused {
94
94
ref mut started, ..
95
95
} => * started = false ,
96
96
}
97
+ context. update_connectivities ( & inner) ;
97
98
}
98
99
99
100
/// Stops the scheduler if it is currently running.
100
101
async fn do_stop (
101
- mut inner : RwLockWriteGuard < ' _ , InnerSchedulerState > ,
102
+ inner : & mut InnerSchedulerState ,
102
103
context : & Context ,
103
104
new_state : InnerSchedulerState ,
104
105
) {
@@ -122,7 +123,7 @@ impl SchedulerState {
122
123
debug_logging. loop_handle . abort ( ) ;
123
124
debug_logging. loop_handle . await . ok ( ) ;
124
125
}
125
- let prev_state = std:: mem:: replace ( & mut * inner, new_state) ;
126
+ let prev_state = std:: mem:: replace ( inner, new_state) ;
126
127
context. emit_event ( EventType :: ConnectivityChanged ) ;
127
128
match prev_state {
128
129
InnerSchedulerState :: Started ( scheduler) => scheduler. stop ( context) . await ,
@@ -138,7 +139,7 @@ impl SchedulerState {
138
139
/// If in the meantime [`SchedulerState::start`] or [`SchedulerState::stop`] is called
139
140
/// resume will do the right thing and restore the scheduler to the state requested by
140
141
/// the last call.
141
- pub ( crate ) async fn pause ( & ' _ self , context : Context ) -> Result < IoPausedGuard > {
142
+ pub ( crate ) async fn pause ( & ' _ self , context : & Context ) -> Result < IoPausedGuard > {
142
143
{
143
144
let mut inner = self . inner . write ( ) . await ;
144
145
match * inner {
@@ -147,7 +148,7 @@ impl SchedulerState {
147
148
started : true ,
148
149
pause_guards_count : NonZeroUsize :: new ( 1 ) . unwrap ( ) ,
149
150
} ;
150
- Self :: do_stop ( inner, & context, new_state) . await ;
151
+ Self :: do_stop ( & mut inner, context, new_state) . await ;
151
152
}
152
153
InnerSchedulerState :: Stopped => {
153
154
* inner = InnerSchedulerState :: Paused {
@@ -164,9 +165,11 @@ impl SchedulerState {
164
165
. ok_or_else ( || Error :: msg ( "Too many pause guards active" ) ) ?
165
166
}
166
167
}
168
+ context. update_connectivities ( & inner) ;
167
169
}
168
170
169
171
let ( tx, rx) = oneshot:: channel ( ) ;
172
+ let context = context. clone ( ) ;
170
173
tokio:: spawn ( async move {
171
174
rx. await . ok ( ) ;
172
175
let mut inner = context. scheduler . inner . write ( ) . await ;
@@ -183,7 +186,7 @@ impl SchedulerState {
183
186
} => {
184
187
if * pause_guards_count == NonZeroUsize :: new ( 1 ) . unwrap ( ) {
185
188
match * started {
186
- true => SchedulerState :: do_start ( inner, context. clone ( ) ) . await ,
189
+ true => SchedulerState :: do_start ( & mut inner, & context) . await ,
187
190
false => * inner = InnerSchedulerState :: Stopped ,
188
191
}
189
192
} else {
@@ -193,6 +196,7 @@ impl SchedulerState {
193
196
}
194
197
}
195
198
}
199
+ context. update_connectivities ( & inner) ;
196
200
} ) ;
197
201
Ok ( IoPausedGuard { sender : Some ( tx) } )
198
202
}
@@ -202,7 +206,7 @@ impl SchedulerState {
202
206
info ! ( context, "restarting IO" ) ;
203
207
if self . is_running ( ) . await {
204
208
self . stop ( context) . await ;
205
- self . start ( context. clone ( ) ) . await ;
209
+ self . start ( context) . await ;
206
210
}
207
211
}
208
212
@@ -288,7 +292,7 @@ impl SchedulerState {
288
292
}
289
293
290
294
#[ derive( Debug , Default ) ]
291
- enum InnerSchedulerState {
295
+ pub ( crate ) enum InnerSchedulerState {
292
296
Started ( Scheduler ) ,
293
297
#[ default]
294
298
Stopped ,
0 commit comments