Skip to content

Commit

Permalink
Move SDK version fetching to pkg/_pub_shared. (#7509)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Feb 26, 2024
1 parent 8562ea4 commit c54b17e
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/bin/tools/public_package_page_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import 'dart:convert';
import 'dart:io';

import 'package:_pub_shared/utils/http.dart';
import 'package:args/args.dart';
import 'package:pool/pool.dart';
import 'package:pub_dev/tool/utils/http.dart';

/// Crawls and checks public package pages on pub.dev site, intended for pre-release
/// checks for rendering user-generated content (esp. markdown).
Expand Down
2 changes: 1 addition & 1 deletion app/lib/account/default_auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:convert';

import 'package:_pub_shared/utils/http.dart';
import 'package:basics/basics.dart';
import 'package:clock/clock.dart';
import 'package:googleapis/oauth2/v2.dart' as oauth2_v2;
Expand All @@ -21,7 +22,6 @@ import '../shared/email.dart' show looksLikeEmail;
import '../shared/exceptions.dart';
import '../shared/monitoring.dart';
import '../shared/utils.dart' show fixedTimeEquals;
import '../tool/utils/http.dart' show httpRetryClient;
import 'auth_provider.dart';

final _logger = Logger('pub.account.google_auth2');
Expand Down
6 changes: 4 additions & 2 deletions app/lib/admin/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import 'dart:convert';
import 'package:_pub_shared/data/admin_api.dart' as api;
import 'package:_pub_shared/data/package_api.dart';
import 'package:_pub_shared/search/tags.dart';
import 'package:_pub_shared/utils/dart_sdk_version.dart';
import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
import 'package:convert/convert.dart';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:logging/logging.dart';
import 'package:pool/pool.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_semver/pub_semver.dart';

import '../account/backend.dart';
Expand All @@ -30,7 +32,6 @@ import '../shared/datastore.dart';
import '../shared/email.dart';
import '../shared/exceptions.dart';
import '../task/backend.dart';
import '../tool/utils/dart_sdk_version.dart';
import 'actions/actions.dart' show AdminAction;
import 'tools/delete_all_staging.dart';
import 'tools/list_package_blocked.dart';
Expand Down Expand Up @@ -425,7 +426,8 @@ class AdminBackend {
_logger.info('${caller.displayId}) initiated the delete '
'of package $packageName $version');

final currentDartSdk = await getDartSdkVersion();
final currentDartSdk =
await getDartSdkVersion(lastKnownStable: toolStableDartSdkVersion);
await withRetryTransaction(_db, (tx) async {
final packageKey = _db.emptyKey.append(Package, id: packageName);
final package = await tx.lookupOrNull<Package>(packageKey);
Expand Down
6 changes: 4 additions & 2 deletions app/lib/frontend/handlers/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import 'dart:io';

import 'package:_pub_shared/data/advisories_api.dart'
show ListAdvisoriesResponse;
import 'package:_pub_shared/utils/dart_sdk_version.dart';
import 'package:meta/meta.dart';
import 'package:neat_cache/neat_cache.dart';
import 'package:pub_dev/service/security_advisories/backend.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/task/backend.dart';
import 'package:shelf/shelf.dart' as shelf;

Expand All @@ -26,7 +28,6 @@ import '../../shared/handlers.dart';
import '../../shared/redis_cache.dart' show cache;
import '../../shared/urls.dart' as urls;
import '../../shared/utils.dart';
import '../../tool/utils/dart_sdk_version.dart';

import '../request_context.dart';
import '../templates/misc.dart';
Expand Down Expand Up @@ -82,7 +83,8 @@ Future<shelf.Response> packageVersionsListHandler(
return redirectToSearch(packageName);
}

final dartSdkVersion = await getDartSdkVersion();
final dartSdkVersion =
await getDartSdkVersion(lastKnownStable: toolStableDartSdkVersion);
final taskStatus = await taskBackend.packageStatus(packageName);
return renderPkgVersionsPage(
data,
Expand Down
13 changes: 9 additions & 4 deletions app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:io';

import 'package:_pub_shared/data/account_api.dart' as account_api;
import 'package:_pub_shared/data/package_api.dart' as api;
import 'package:_pub_shared/utils/dart_sdk_version.dart';
import 'package:clock/clock.dart';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
Expand All @@ -19,6 +20,7 @@ import 'package:meta/meta.dart';
import 'package:pool/pool.dart';
import 'package:pub_dev/package/export_api_to_bucket.dart';
import 'package:pub_dev/service/async_queue/async_queue.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/task/backend.dart';
import 'package:pub_package_reader/pub_package_reader.dart';
import 'package:pub_semver/pub_semver.dart';
Expand All @@ -41,7 +43,6 @@ import '../shared/redis_cache.dart' show cache;
import '../shared/storage.dart';
import '../shared/urls.dart' as urls;
import '../shared/utils.dart';
import '../tool/utils/dart_sdk_version.dart';
import 'model_properties.dart';
import 'models.dart';
import 'name_tracker.dart';
Expand Down Expand Up @@ -319,7 +320,9 @@ class PackageBackend {
}) async {
_logger.info("Checking Package's versions fields for package `$package`.");
final pkgKey = db.emptyKey.append(Package, id: package);
dartSdkVersion ??= (await getDartSdkVersion()).semanticVersion;
dartSdkVersion ??=
(await getDartSdkVersion(lastKnownStable: toolStableDartSdkVersion))
.semanticVersion;

// ordered version list by publish date
final versions =
Expand Down Expand Up @@ -598,7 +601,8 @@ class PackageBackend {
// the latest version or the restored version is newer than the latest.
if (p.mayAffectLatestVersions(pv.semanticVersion)) {
final versions = await tx.query<PackageVersion>(p.key).run().toList();
final currentDartSdk = await getDartSdkVersion();
final currentDartSdk =
await getDartSdkVersion(lastKnownStable: toolStableDartSdkVersion);
p.updateLatestVersionReferences(
versions,
dartSdkVersion: currentDartSdk.semanticVersion,
Expand Down Expand Up @@ -1006,7 +1010,8 @@ class PackageBackend {
}) async {
final sw = Stopwatch()..start();
final newVersion = entities.packageVersion;
final currentDartSdk = await getDartSdkVersion();
final currentDartSdk =
await getDartSdkVersion(lastKnownStable: toolStableDartSdkVersion);
final existingPackage = await lookupPackage(newVersion.package);
final isNew = existingPackage == null;

Expand Down
2 changes: 1 addition & 1 deletion app/lib/search/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:io';

import 'package:_pub_shared/search/search_form.dart';
import 'package:_pub_shared/search/tags.dart';
import 'package:_pub_shared/utils/http.dart';
import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
import 'package:gcloud/service_scope.dart' as ss;
Expand Down Expand Up @@ -39,7 +40,6 @@ import '../shared/versions.dart';
import '../task/backend.dart';
import '../task/global_lock.dart';
import '../task/models.dart';
import '../tool/utils/http.dart';

import 'dart_sdk_mem_index.dart';
import 'flutter_sdk_mem_index.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/search/sdk_mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:_pub_shared/utils/http.dart';
// ignore: implementation_imports
import 'package:pana/src/dartdoc/dartdoc_index.dart';
import 'package:path/path.dart' as p;

import '../shared/versions.dart';
import '../tool/utils/http.dart';
import 'search_service.dart';
import 'token_index.dart';

Expand Down
2 changes: 1 addition & 1 deletion app/lib/search/search_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import 'dart:async';
import 'dart:convert';

import 'package:_pub_shared/utils/http.dart';
import 'package:clock/clock.dart';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:http/http.dart' as http;

import '../shared/configuration.dart';
import '../shared/redis_cache.dart' show cache;
import '../shared/utils.dart';
import '../tool/utils/http.dart';

import 'search_service.dart';

Expand Down
3 changes: 1 addition & 2 deletions app/lib/service/openid/openid_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import 'dart:convert';

import 'package:_pub_shared/utils/http.dart';
import 'package:pub_dev/shared/env_config.dart';

import '../../tool/utils/http.dart';

import 'openid_models.dart';

/// Fetches the OpenID configuration and then the JSON Web Key list from the given endpoint.
Expand Down
2 changes: 1 addition & 1 deletion app/lib/service/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async' show FutureOr, Zone;

import 'package:_pub_shared/utils/http.dart';
import 'package:appengine/appengine.dart';
import 'package:clock/clock.dart';
import 'package:fake_gcloud/mem_datastore.dart';
Expand Down Expand Up @@ -58,7 +59,6 @@ import '../shared/versions.dart';
import '../task/backend.dart';
import '../task/cloudcompute/fakecloudcompute.dart';
import '../task/cloudcompute/googlecloudcompute.dart';
import '../tool/utils/http.dart';
import 'announcement/backend.dart';
import 'entrypoint/logging.dart';
import 'secret/backend.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/shared/integrity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:math' as math;

import 'package:_pub_shared/search/tags.dart';
import 'package:_pub_shared/utils/http.dart';
import 'package:clock/clock.dart';
import 'package:crypto/crypto.dart';
import 'package:gcloud/storage.dart';
Expand All @@ -23,7 +24,6 @@ import '../package/backend.dart';
import '../package/model_properties.dart';
import '../package/models.dart';
import '../publisher/models.dart';
import '../tool/utils/http.dart';

import 'configuration.dart';
import 'datastore.dart';
Expand Down
35 changes: 2 additions & 33 deletions app/lib/tool/utils/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,10 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:_pub_shared/utils/http.dart';
import 'package:http/http.dart' as http;
import 'package:http/retry.dart';
import 'package:pub_dev/account/session_cookie.dart';

final _transientStatusCodes = {
// See: https://cloud.google.com/storage/docs/xml-api/reference-status
429,
500,
502,
503,
};

/// Creates a HTTP client that retries transient status codes.
///
/// When [lenient] is set, we retry on more errors and status codes.
http.Client httpRetryClient({
http.Client? innerClient,
int? retries,
bool lenient = false,
}) {
return RetryClient(
innerClient ?? http.Client(),
when: (r) =>
(lenient && r.statusCode >= 500) ||
_transientStatusCodes.contains(r.statusCode),
retries: retries ?? 5,
// TOOD: Consider implementing whenError to handle DNS + handshake errors.
// These are safe, retrying after partially sending data is more
// sketchy, but probably safe in our application.
whenError: (e, st) => lenient || e is SocketException,
);
}

/// Returns an [http.Client] which sends a `Bearer` token as `Authorization`
/// header for each request.
http.Client httpClientWithAuthorization({
Expand All @@ -49,7 +18,7 @@ http.Client httpClientWithAuthorization({
tokenProvider,
sessionIdProvider,
csrfTokenProvider,
client ?? http.Client(),
client ?? httpRetryClient(),
client == null,
);
}
Expand Down
6 changes: 4 additions & 2 deletions app/test/package/preview_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import 'dart:async';
import 'dart:convert';

import 'package:_pub_shared/utils/dart_sdk_version.dart';
import 'package:pub_dev/package/backend.dart';
import 'package:pub_dev/package/models.dart';
import 'package:pub_dev/shared/datastore.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/tool/test_profile/import_source.dart';
import 'package:pub_dev/tool/test_profile/models.dart';
import 'package:pub_dev/tool/utils/dart_sdk_version.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';

Expand All @@ -24,7 +25,8 @@ void main() {

group('SDK version changing', () {
setUpAll(() async {
final current = await getDartSdkVersion();
final current =
await getDartSdkVersion(lastKnownStable: toolStableDartSdkVersion);
currentSdkVersion = current.semanticVersion;
futureSdkVersion = Version.parse('3.99.0');
});
Expand Down
2 changes: 1 addition & 1 deletion app/test/shared/versions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import 'dart:io';

import 'package:_pub_shared/utils/flutter_archive.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/tool/utils/flutter_archive.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:_pub_shared/utils/http.dart';
import 'package:clock/clock.dart';
import 'package:logging/logging.dart';
import 'package:pub_semver/pub_semver.dart';

import '../../shared/versions.dart';
import 'http.dart' show httpRetryClient;

final _logger = Logger('tool.sdk_version');

/// The default cache timeout after the request is made to the storage server.
Expand All @@ -37,16 +36,19 @@ class DartSdkVersion {
}

/// Gets the latest Dart SDK version information (value may be cached).
Future<DartSdkVersion> getDartSdkVersion() async {
Future<DartSdkVersion> getDartSdkVersion({
required String lastKnownStable,
}) async {
if (_cached != null && !_cached!.isExpired) {
return _cached!;
}
return _fetchDartSdkVersion();
return _fetchDartSdkVersion(fallbackVersion: lastKnownStable);
}

/// Fetches the latest Dart SDK version information.
Future<DartSdkVersion> _fetchDartSdkVersion() async {
// TODO: Migrate to proper retry logic
Future<DartSdkVersion> _fetchDartSdkVersion({
String? fallbackVersion,
}) async {
final client = httpRetryClient();
try {
final rs = await client.get(Uri.parse(
Expand All @@ -63,7 +65,7 @@ Future<DartSdkVersion> _fetchDartSdkVersion() async {
// If there exists a cached value, extend it.
// If there is no cached value, use the runtime analysis SDK as the latest.
return _cached = DartSdkVersion(
_cached?.version ?? toolStableDartSdkVersion,
_cached?.version ?? fallbackVersion ?? Platform.version.split(' ').first,
_cached?.published ?? clock.now(),
expires: clock.now().add(_failedFetchCacheDuration),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:convert';

import 'package:http/http.dart';
import 'package:_pub_shared/utils/http.dart';
import 'package:json_annotation/json_annotation.dart';

part 'flutter_archive.g.dart';
Expand All @@ -15,7 +15,7 @@ part 'flutter_archive.g.dart';
/// - https://flutter.dev/docs/development/tools/sdk/releases?tab=linux
/// - https://github.com/flutter/flutter/wiki/Flutter-build-release-channels
Future<FlutterArchive> fetchFlutterArchive() async {
final client = Client();
final client = httpRetryClient();
final rs = await client.get(Uri.parse(
'https://storage.googleapis.com/flutter_infra_release/releases/releases_linux.json'));
client.close();
Expand Down
File renamed without changes.
Loading

0 comments on commit c54b17e

Please sign in to comment.