|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:googleapis/storage/v1.dart'; |
| 6 | +import 'package:googleapis_auth/auth.dart'; |
| 7 | + |
| 8 | +/// Global (in terms of earth) mutex using Google Cloud Storage. |
| 9 | +class GcsLock { |
| 10 | + /// Create a lock with an authenticated client and a GCS bucket name. |
| 11 | + /// |
| 12 | + /// The client is used to communicate with Google Cloud Storage APIs. |
| 13 | + GcsLock(this._client, this._bucketName) |
| 14 | + : assert(_client != null), |
| 15 | + assert(_bucketName != null) { |
| 16 | + _api = StorageApi(_client); |
| 17 | + } |
| 18 | + |
| 19 | + /// Create a temporary lock file in GCS, and use it as a mutex mechanism to |
| 20 | + /// run a piece of code exclusively. |
| 21 | + /// |
| 22 | + /// There must be no existing lock file with the same name in order to |
| 23 | + /// proceed. If multiple [GcsLock]s with the same `bucketName` and |
| 24 | + /// `lockFileName` try [protectedRun] simultaneously, only one will proceed |
| 25 | + /// and create the lock file. All others will be blocked. |
| 26 | + /// |
| 27 | + /// When [protectedRun] finishes, the lock file is deleted, and other blocked |
| 28 | + /// [protectedRun] may proceed. |
| 29 | + /// |
| 30 | + /// If the lock file is stuck (e.g., `_unlock` is interrupted unexpectedly), |
| 31 | + /// one may need to manually delete the lock file from GCS to unblock any |
| 32 | + /// [protectedRun] that may depend on it. |
| 33 | + Future<void> protectedRun(String lockFileName, Future<void> f()) async { |
| 34 | + await _lock(lockFileName); |
| 35 | + try { |
| 36 | + await f(); |
| 37 | + } catch (e, stacktrace) { |
| 38 | + print(stacktrace); |
| 39 | + rethrow; |
| 40 | + } finally { |
| 41 | + await _unlock(lockFileName); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + Future<void> _lock(String lockFileName) async { |
| 46 | + final Object object = Object(); |
| 47 | + object.bucket = _bucketName; |
| 48 | + object.name = lockFileName; |
| 49 | + final Media content = Media(const Stream<List<int>>.empty(), 0); |
| 50 | + |
| 51 | + Duration waitPeriod = const Duration(milliseconds: 10); |
| 52 | + bool locked = false; |
| 53 | + while (!locked) { |
| 54 | + try { |
| 55 | + await _api.objects.insert(object, _bucketName, |
| 56 | + ifGenerationMatch: '0', uploadMedia: content); |
| 57 | + locked = true; |
| 58 | + } on DetailedApiRequestError catch (e) { |
| 59 | + if (e.status == 412) { |
| 60 | + // Status 412 means that the lock file already exists. Wait until |
| 61 | + // that lock file is deleted. |
| 62 | + await Future<void>.delayed(waitPeriod); |
| 63 | + waitPeriod *= 2; |
| 64 | + if (waitPeriod >= _kWarningThreshold) { |
| 65 | + print( |
| 66 | + 'The lock is waiting for a long time: $waitPeriod. ' |
| 67 | + 'If the lock file $lockFileName in bucket $_bucketName ' |
| 68 | + 'seems to be stuck (i.e., it was created a long time ago and ' |
| 69 | + 'no one seems to be owning it currently), delete it manually ' |
| 70 | + 'to unblock this.', |
| 71 | + ); |
| 72 | + } |
| 73 | + } else { |
| 74 | + rethrow; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + Future<void> _unlock(String lockFileName) async { |
| 81 | + await _api.objects.delete(_bucketName, lockFileName); |
| 82 | + } |
| 83 | + |
| 84 | + StorageApi _api; |
| 85 | + |
| 86 | + final String _bucketName; |
| 87 | + final AuthClient _client; |
| 88 | + |
| 89 | + static const Duration _kWarningThreshold = Duration(seconds: 10); |
| 90 | +} |
0 commit comments