@@ -8,7 +8,6 @@ import 'dart:convert';
8
8
import 'package:_pub_shared/utils/http.dart' ;
9
9
import 'package:clock/clock.dart' ;
10
10
import 'package:gcloud/service_scope.dart' as ss;
11
- import 'package:http/http.dart' as http;
12
11
13
12
import '../../../service/rate_limit/rate_limit.dart' ;
14
13
import '../shared/configuration.dart' ;
@@ -33,16 +32,13 @@ SearchClient get searchClient => ss.lookup(#_searchClient) as SearchClient;
33
32
/// indexed data.
34
33
class SearchClient {
35
34
/// The HTTP client used for making calls to our search service.
36
- final http. Client _httpClient;
35
+ final _httpClient = httpRetryClient () ;
37
36
38
37
/// Before this timestamp we may use the fallback search service URL, which
39
38
/// is the unversioned service URL, potentially getting responses from an
40
39
/// older instance.
41
40
final _fallbackSearchThreshold = clock.now ().add (Duration (minutes: 30 ));
42
41
43
- SearchClient ([http.Client ? client])
44
- : _httpClient = client ?? httpRetryClient (retries: 3 );
45
-
46
42
/// Calls the search service (or uses cache) to serve the [query] .
47
43
Future <PackageSearchResult > search (
48
44
ServiceSearchQuery query, {
@@ -69,16 +65,25 @@ class SearchClient {
69
65
skipCache = true ;
70
66
}
71
67
72
- // returns null on timeout (after 5 seconds)
73
- Future <http.Response ?> doCallHttpServiceEndpoint ({String ? prefix}) async {
68
+ // Returns the status code and the body of the last response, or null on timeout.
69
+ Future <({int statusCode, String ? body})?> doCallHttpServiceEndpoint (
70
+ {String ? prefix}) async {
74
71
final httpHostPort = prefix ?? activeConfiguration.searchServicePrefix;
75
72
final serviceUrl = '$httpHostPort /search$serviceUrlParams ' ;
76
73
try {
77
- return await _httpClient
78
- .get (Uri .parse (serviceUrl), headers: cloudTraceHeaders ())
79
- .timeout (Duration (seconds: 5 ));
74
+ return await httpGetWithRetry (
75
+ Uri .parse (serviceUrl),
76
+ client: _httpClient,
77
+ headers: cloudTraceHeaders (),
78
+ perRequestTimeout: Duration (seconds: 5 ),
79
+ retryIf: (e) => (e is UnexpectedStatusException &&
80
+ e.statusCode == searchIndexNotReadyCode),
81
+ responseFn: (rs) => (statusCode: rs.statusCode, body: rs.body),
82
+ );
80
83
} on TimeoutException {
81
84
return null ;
85
+ } on UnexpectedStatusException catch (e) {
86
+ return (statusCode: e.statusCode, body: null );
82
87
}
83
88
}
84
89
@@ -103,7 +108,7 @@ class SearchClient {
103
108
}
104
109
if (response.statusCode == 200 ) {
105
110
return PackageSearchResult .fromJson (
106
- json.decode (response.body) as Map <String , dynamic >,
111
+ json.decode (response.body! ) as Map <String , dynamic >,
107
112
);
108
113
}
109
114
// Search request before the service initialization completed.
0 commit comments