@@ -3,31 +3,35 @@ use crate::source_reader::SourceReader;
3
3
use crate :: ui:: ProgressBar as _;
4
4
use crate :: { executor:: staging_area:: StagingArea , Progress } ;
5
5
use anyhow:: { bail, Result } ;
6
+ use lazy_static:: lazy_static;
6
7
use qlty_cloud:: Client ;
7
8
use qlty_config:: issue_transformer:: IssueTransformer ;
8
9
use qlty_types:: analysis:: v1:: { Issue , Suggestion } ;
10
+ use rayon:: { ThreadPool , ThreadPoolBuilder } ;
9
11
use std:: collections:: HashMap ;
10
12
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
11
13
use std:: sync:: { Arc , Mutex } ;
12
- use std:: thread:: sleep;
13
- use std:: time:: Duration ;
14
- use tracing:: { debug, warn} ;
15
- use tracing:: { info, trace} ;
14
+ use tracing:: { debug, info, warn} ;
16
15
use ureq:: json;
17
16
18
17
const MAX_FIXES : usize = 500 ;
19
18
const MAX_FIXES_PER_FILE : usize = 30 ;
20
19
const MAX_CONCURRENT_FIXES : usize = 10 ;
21
20
21
+ lazy_static ! {
22
+ static ref API_THREAD_POOL : ThreadPool = ThreadPoolBuilder :: new( )
23
+ . num_threads( MAX_CONCURRENT_FIXES )
24
+ . build( )
25
+ . unwrap( ) ;
26
+ }
27
+
22
28
#[ derive( Clone , Debug ) ]
23
29
pub struct Fixer {
24
30
progress : Progress ,
25
31
staging_area : StagingArea ,
26
32
r#unsafe : bool ,
27
33
attempts_per_file : Arc < Mutex < HashMap < Option < String > , AtomicUsize > > > ,
28
34
total_attempts : Arc < AtomicUsize > ,
29
- api_concurrency_lock : Arc < AtomicUsize > ,
30
- api_concurrency_guard : Arc < Mutex < ( ) > > ,
31
35
}
32
36
33
37
impl IssueTransformer for Fixer {
@@ -55,8 +59,6 @@ impl Fixer {
55
59
r#unsafe : plan. settings . r#unsafe ,
56
60
attempts_per_file : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
57
61
total_attempts : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
58
- api_concurrency_lock : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
59
- api_concurrency_guard : Arc :: new ( Mutex :: new ( ( ) ) ) ,
60
62
}
61
63
}
62
64
@@ -129,18 +131,18 @@ impl Fixer {
129
131
if let Some ( path) = issue. path ( ) {
130
132
let client = Client :: authenticated ( ) ?;
131
133
let content = self . staging_area . read ( issue. path ( ) . unwrap ( ) . into ( ) ) ?;
132
- self . try_fix_barrier ( ) ;
133
- let response = client. post ( "/fixes" ) . send_json ( json ! ( {
134
- "issue" : issue. clone( ) ,
135
- "files" : [ { "content" : content, "path" : path } ] ,
136
- "options" : {
137
- "unsafe" : self . r#unsafe
138
- } ,
139
- } ) ) ;
140
- self . api_concurrency_lock . fetch_sub ( 1 , Ordering :: SeqCst ) ;
134
+ let response = API_THREAD_POOL . scope ( |_| {
135
+ client. post ( "/fixes" ) . send_json ( json ! ( {
136
+ "issue" : issue. clone( ) ,
137
+ "files" : [ { "content" : content, "path" : path } ] ,
138
+ "options" : {
139
+ "unsafe" : self . r#unsafe
140
+ } ,
141
+ } ) )
142
+ } ) ? ;
141
143
142
144
let response_debug = format ! ( "{:?}" , & response) ;
143
- let suggestions: Vec < Suggestion > = response? . into_json ( ) ?;
145
+ let suggestions: Vec < Suggestion > = response. into_json ( ) ?;
144
146
debug ! ( "{} with {} suggestions" , response_debug, suggestions. len( ) ) ;
145
147
146
148
let mut issue = issue. clone ( ) ;
@@ -151,14 +153,4 @@ impl Fixer {
151
153
bail ! ( "Issue {} has no path" , issue. id) ;
152
154
}
153
155
}
154
-
155
- fn try_fix_barrier ( & self ) {
156
- let guard = self . api_concurrency_guard . lock ( ) . unwrap ( ) ;
157
- while self . api_concurrency_lock . load ( Ordering :: SeqCst ) >= MAX_CONCURRENT_FIXES {
158
- sleep ( Duration :: from_millis ( 100 ) ) ;
159
- }
160
- let value = self . api_concurrency_lock . fetch_add ( 1 , Ordering :: SeqCst ) ;
161
- trace ! ( "API request made with {} concurrent fixes" , value) ;
162
- drop ( guard) ;
163
- }
164
156
}
0 commit comments