@@ -8,7 +8,6 @@ import 'dart:convert';
88import 'package:_pub_shared/utils/http.dart' ;
99import 'package:clock/clock.dart' ;
1010import 'package:gcloud/service_scope.dart' as ss;
11- import 'package:http/http.dart' as http;
1211
1312import '../../../service/rate_limit/rate_limit.dart' ;
1413import '../shared/configuration.dart' ;
@@ -33,16 +32,13 @@ SearchClient get searchClient => ss.lookup(#_searchClient) as SearchClient;
3332/// indexed data.
3433class SearchClient {
3534 /// The HTTP client used for making calls to our search service.
36- final http. Client _httpClient;
35+ final _httpClient = httpRetryClient () ;
3736
3837 /// Before this timestamp we may use the fallback search service URL, which
3938 /// is the unversioned service URL, potentially getting responses from an
4039 /// older instance.
4140 final _fallbackSearchThreshold = clock.now ().add (Duration (minutes: 30 ));
4241
43- SearchClient ([http.Client ? client])
44- : _httpClient = client ?? httpRetryClient (retries: 3 );
45-
4642 /// Calls the search service (or uses cache) to serve the [query] .
4743 Future <PackageSearchResult > search (
4844 ServiceSearchQuery query, {
@@ -69,16 +65,25 @@ class SearchClient {
6965 skipCache = true ;
7066 }
7167
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 {
7471 final httpHostPort = prefix ?? activeConfiguration.searchServicePrefix;
7572 final serviceUrl = '$httpHostPort /search$serviceUrlParams ' ;
7673 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+ );
8083 } on TimeoutException {
8184 return null ;
85+ } on UnexpectedStatusException catch (e) {
86+ return (statusCode: e.statusCode, body: null );
8287 }
8388 }
8489
@@ -103,7 +108,7 @@ class SearchClient {
103108 }
104109 if (response.statusCode == 200 ) {
105110 return PackageSearchResult .fromJson (
106- json.decode (response.body) as Map <String , dynamic >,
111+ json.decode (response.body! ) as Map <String , dynamic >,
107112 );
108113 }
109114 // Search request before the service initialization completed.
0 commit comments