File tree Expand file tree Collapse file tree 2 files changed +12
-8
lines changed Expand file tree Collapse file tree 2 files changed +12
-8
lines changed Original file line number Diff line number Diff line change 1
- use arc_swap:: ArcSwap ;
2
-
3
1
use crate :: { bors:: command:: CommandParser , database:: DbClient , github:: GithubRepoName } ;
4
- use std:: { collections:: HashMap , sync:: Arc } ;
2
+ use std:: {
3
+ collections:: HashMap ,
4
+ sync:: { Arc , RwLock } ,
5
+ } ;
5
6
6
7
use super :: { RepositoryClient , RepositoryLoader , RepositoryState } ;
7
8
8
9
pub struct BorsContext < Client : RepositoryClient > {
9
10
pub parser : CommandParser ,
10
11
pub db : Arc < dyn DbClient > ,
11
12
pub repository_loader : Arc < dyn RepositoryLoader < Client > > ,
12
- pub repositories : ArcSwap < HashMap < GithubRepoName , Arc < RepositoryState < Client > > > > ,
13
+ pub repositories : RwLock < HashMap < GithubRepoName , Arc < RepositoryState < Client > > > > ,
13
14
}
14
15
15
16
impl < Client : RepositoryClient > BorsContext < Client > {
@@ -21,7 +22,7 @@ impl<Client: RepositoryClient> BorsContext<Client> {
21
22
// this unwrap is making me nervous, but if lhe repos loading
22
23
// fails we might as well restart the bot
23
24
let repositories = global_client. load_repositories ( ) . await ?;
24
- let repositories = ArcSwap :: new ( Arc :: new ( repositories) ) ;
25
+ let repositories = RwLock :: new ( repositories) ;
25
26
Ok ( Self {
26
27
parser,
27
28
db,
Original file line number Diff line number Diff line change @@ -30,7 +30,8 @@ pub async fn handle_bors_repository_event<Client: RepositoryClient>(
30
30
let db = Arc :: clone ( & ctx. db ) ;
31
31
let Some ( repo) = ctx
32
32
. repositories
33
- . load ( )
33
+ . read ( )
34
+ . unwrap ( )
34
35
. get ( event. repository ( ) )
35
36
. map ( Arc :: clone)
36
37
else {
@@ -125,7 +126,9 @@ pub async fn handle_bors_global_event<Client: RepositoryClient>(
125
126
126
127
match ctx. repository_loader . load_repositories ( ) . await {
127
128
Ok ( repos) => {
128
- ctx. repositories . store ( Arc :: new ( repos) ) ;
129
+ let mut repositories = ctx. repositories . write ( ) . unwrap ( ) ;
130
+ // TODO: keep old repos in case of repo loading failure
131
+ * repositories = repos;
129
132
}
130
133
Err ( error) => {
131
134
span. log_error ( error) ;
@@ -136,7 +139,7 @@ pub async fn handle_bors_global_event<Client: RepositoryClient>(
136
139
BorsGlobalEvent :: Refresh => {
137
140
let span = tracing:: info_span!( "Refresh" ) ;
138
141
let repos: Vec < Arc < RepositoryState < Client > > > =
139
- ctx. repositories . load ( ) . values ( ) . cloned ( ) . collect ( ) ;
142
+ ctx. repositories . read ( ) . unwrap ( ) . values ( ) . cloned ( ) . collect ( ) ;
140
143
futures:: future:: join_all ( repos. into_iter ( ) . map ( |repo| {
141
144
let repo = Arc :: clone ( & repo) ;
142
145
async {
You can’t perform that action at this time.
0 commit comments