Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
fix a bug with the throttling algorithm (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Mar 30, 2021
1 parent 5b7317b commit 7a9ce09
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.0.2
- Fix a bug with the analytics ping throttling algorithm.

## 4.0.1
- Force close the http client from `IOAnalytics.close()`.
This prevents lingering requests from making the application hang.
Expand Down
6 changes: 3 additions & 3 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class ThrottlingBucket {
}

void _checkReplenish() {
var now = DateTime.now().millisecondsSinceEpoch;
final now = DateTime.now().millisecondsSinceEpoch;

if (_lastReplenish + 1000 >= now) {
var inc = (now - _lastReplenish) ~/ 1000;
if (_lastReplenish + 1000 < now) {
final inc = (now - _lastReplenish) ~/ 1000;
drops = math.min(drops + inc, startingCount);
_lastReplenish += (1000 * inc);
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.

name: usage
version: 4.0.1
version: 4.0.2
description: A Google Analytics wrapper for command-line, web, and Flutter apps.
homepage: https://github.com/dart-lang/usage

Expand All @@ -15,4 +15,4 @@ dependencies:

dev_dependencies:
pedantic: ^1.9.0
test: ^1.16.0-nullsafety
test: ^1.16.0
12 changes: 12 additions & 0 deletions test/usage_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ void defineTests() {
}
expect(bucket.removeDrop(), false);
});

test('does re-send after throttling', () async {
var bucket = ThrottlingBucket(20);
for (var i = 0; i < 20; i++) {
expect(bucket.removeDrop(), true);
}
expect(bucket.removeDrop(), false);

// TODO: Re-write to use package:fake_async.
await Future.delayed(Duration(milliseconds: 1500));
expect(bucket.removeDrop(), true);
});
});

group('AnalyticsImpl', () {
Expand Down

0 comments on commit 7a9ce09

Please sign in to comment.