1
- use super :: { Error , ParsedRelays } ;
2
1
use futures:: {
3
2
channel:: mpsc,
4
3
future:: { Fuse , FusedFuture } ,
5
4
Future , FutureExt , SinkExt , StreamExt ,
6
5
} ;
7
- use mullvad_api:: { availability:: ApiAvailabilityHandle , rest:: MullvadRestHandle , RelayListProxy } ;
8
- use mullvad_types:: relay_list:: RelayList ;
9
- use parking_lot:: Mutex ;
10
6
use std:: {
11
7
path:: { Path , PathBuf } ,
12
- sync:: Arc ,
13
8
time:: { Duration , SystemTime , UNIX_EPOCH } ,
14
9
} ;
10
+ use tokio:: fs:: File ;
11
+
12
+ use mullvad_api:: { availability:: ApiAvailabilityHandle , rest:: MullvadRestHandle , RelayListProxy } ;
13
+ use mullvad_relay_selector:: { Error , RelaySelector } ;
14
+ use mullvad_types:: relay_list:: RelayList ;
15
15
use talpid_future:: retry:: { retry_future, ExponentialBackoff , Jittered } ;
16
16
use talpid_types:: ErrorExt ;
17
- use tokio:: fs:: File ;
18
17
19
18
/// How often the updater should wake up to check the cache of the in-memory cache of relays.
20
19
/// This check is very cheap. The only reason to not have it very often is because if downloading
@@ -28,6 +27,9 @@ const DOWNLOAD_RETRY_STRATEGY: Jittered<ExponentialBackoff> = Jittered::jitter(
28
27
. max_delay ( Some ( Duration :: from_secs ( 2 * 60 * 60 ) ) ) ,
29
28
) ;
30
29
30
+ /// Where the relay list is cached on disk.
31
+ pub ( crate ) const RELAYS_FILENAME : & str = "relays.json" ;
32
+
31
33
#[ derive( Clone ) ]
32
34
pub struct RelayListUpdaterHandle {
33
35
tx : mpsc:: Sender < ( ) > ,
@@ -52,15 +54,15 @@ impl RelayListUpdaterHandle {
52
54
pub struct RelayListUpdater {
53
55
api_client : RelayListProxy ,
54
56
cache_path : PathBuf ,
55
- parsed_relays : Arc < Mutex < ParsedRelays > > ,
57
+ relay_selector : RelaySelector ,
56
58
on_update : Box < dyn Fn ( & RelayList ) + Send + ' static > ,
57
59
last_check : SystemTime ,
58
60
api_availability : ApiAvailabilityHandle ,
59
61
}
60
62
61
63
impl RelayListUpdater {
62
64
pub fn spawn (
63
- selector : super :: RelaySelector ,
65
+ selector : RelaySelector ,
64
66
api_handle : MullvadRestHandle ,
65
67
cache_dir : & Path ,
66
68
on_update : impl Fn ( & RelayList ) + Send + ' static ,
@@ -70,8 +72,8 @@ impl RelayListUpdater {
70
72
let api_client = RelayListProxy :: new ( api_handle) ;
71
73
let updater = RelayListUpdater {
72
74
api_client,
73
- cache_path : cache_dir. join ( super :: RELAYS_FILENAME ) ,
74
- parsed_relays : selector. parsed_relays ,
75
+ cache_path : cache_dir. join ( RELAYS_FILENAME ) ,
76
+ relay_selector : selector,
75
77
on_update : Box :: new ( on_update) ,
76
78
last_check : UNIX_EPOCH ,
77
79
api_availability,
@@ -91,7 +93,7 @@ impl RelayListUpdater {
91
93
futures:: select! {
92
94
_check_update = next_check => {
93
95
if download_future. is_terminated( ) && self . should_update( ) {
94
- let tag = self . parsed_relays . lock ( ) . parsed_list . etag. clone ( ) ;
96
+ let tag = self . relay_selector . etag( ) ;
95
97
download_future = Box :: pin( Self :: download_relay_list( self . api_availability. clone( ) , self . api_client. clone( ) , tag) . fuse( ) ) ;
96
98
self . last_check = SystemTime :: now( ) ;
97
99
}
@@ -104,7 +106,7 @@ impl RelayListUpdater {
104
106
cmd = cmd_rx. next( ) => {
105
107
match cmd {
106
108
Some ( ( ) ) => {
107
- let tag = self . parsed_relays . lock ( ) . parsed_list . etag. clone ( ) ;
109
+ let tag = self . relay_selector . etag( ) ;
108
110
download_future = Box :: pin( Self :: download_relay_list( self . api_availability. clone( ) , self . api_client. clone( ) , tag) . fuse( ) ) ;
109
111
self . last_check = SystemTime :: now( ) ;
110
112
} ,
@@ -139,7 +141,7 @@ impl RelayListUpdater {
139
141
140
142
/// Returns true if the current parsed_relays is older than UPDATE_INTERVAL
141
143
fn should_update ( & mut self ) -> bool {
142
- let last_check = std:: cmp:: max ( self . parsed_relays . lock ( ) . last_updated ( ) , self . last_check ) ;
144
+ let last_check = std:: cmp:: max ( self . relay_selector . last_updated ( ) , self . last_check ) ;
143
145
match SystemTime :: now ( ) . duration_since ( last_check) {
144
146
Ok ( duration) => duration >= UPDATE_INTERVAL ,
145
147
// If the clock is skewed we have no idea by how much or when the last update
@@ -178,9 +180,8 @@ impl RelayListUpdater {
178
180
) ;
179
181
}
180
182
181
- let mut parsed_relays = self . parsed_relays . lock ( ) ;
182
- parsed_relays. update ( new_relay_list) ;
183
- ( self . on_update ) ( & parsed_relays. original_list ) ;
183
+ self . relay_selector . set_relays ( new_relay_list. clone ( ) ) ;
184
+ ( self . on_update ) ( & new_relay_list) ;
184
185
Ok ( ( ) )
185
186
}
186
187
0 commit comments