Skip to content

Commit

Permalink
Search fallback: handle timeout, longer window. (dart-lang#7621)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Apr 9, 2024
1 parent f70abec commit 82c6015
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions app/lib/search/search_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SearchClient {
/// Before this timestamp we may use the fallback search service URL, which
/// is the unversioned service URL, potentially getting responses from an
/// older instance.
final _fallbackSearchThreshold = clock.now().add(Duration(minutes: 10));
final _fallbackSearchThreshold = clock.now().add(Duration(minutes: 30));

SearchClient([http.Client? client])
: _httpClient = client ?? httpRetryClient(retries: 3);
Expand All @@ -58,34 +58,41 @@ class SearchClient {

final serviceUrlParams = Uri(queryParameters: query.toUriQueryParameters());

Future<http.Response> doCallHttpServiceEndpoint({String? prefix}) async {
// returns null on timeout (after 5 seconds)
Future<http.Response?> doCallHttpServiceEndpoint({String? prefix}) async {
final httpHostPort = prefix ?? activeConfiguration.searchServicePrefix;
final serviceUrl = '$httpHostPort/search$serviceUrlParams';
return await _httpClient
.get(Uri.parse(serviceUrl), headers: cloudTraceHeaders())
.timeout(Duration(seconds: 5));
try {
return await _httpClient
.get(Uri.parse(serviceUrl), headers: cloudTraceHeaders())
.timeout(Duration(seconds: 5));
} on TimeoutException {
return null;
}
}

Future<PackageSearchResult> searchFn() async {
// calling versioned endpoint
final response = await doCallHttpServiceEndpoint();
var response = await doCallHttpServiceEndpoint();
// if needed and possible, calling fallback request to unversioned endpoint
if (response == null || response.statusCode != 200) {
final serviceIsInStartup =
clock.now().isBefore(_fallbackSearchThreshold);
if (serviceIsInStartup &&
activeConfiguration.fallbackSearchServicePrefix != null) {
response = await doCallHttpServiceEndpoint(
prefix: activeConfiguration.fallbackSearchServicePrefix);
}
}
if (response == null) {
return PackageSearchResult.empty(
message: 'Search is temporarily unavailable.');
}
if (response.statusCode == 200) {
return PackageSearchResult.fromJson(
json.decode(response.body) as Map<String, dynamic>,
);
}
// calling fallback request to unversioned endpoint
final serviceIsInStartup = clock.now().isBefore(_fallbackSearchThreshold);
if (serviceIsInStartup &&
activeConfiguration.fallbackSearchServicePrefix != null) {
final fallbackRs = await doCallHttpServiceEndpoint(
prefix: activeConfiguration.fallbackSearchServicePrefix);
if (fallbackRs.statusCode == 200) {
return PackageSearchResult.fromJson(
json.decode(fallbackRs.body) as Map<String, dynamic>,
);
}
}
// Search request before the service initialization completed.
if (response.statusCode == searchIndexNotReadyCode) {
return PackageSearchResult.empty(
Expand Down

0 comments on commit 82c6015

Please sign in to comment.