Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(951)

Unified Diff: microlytics/lib/channels.dart

Issue 515993003: Minimalistic analytics library used by Dart Server and try.dartlang.org (Closed) Base URL: https://github.com/lukechurch/dart-mircolytics.git@master
Patch Set: Address review comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: microlytics/lib/channels.dart
diff --git a/microlytics/lib/channels.dart b/microlytics/lib/channels.dart
new file mode 100644
index 0000000000000000000000000000000000000000..42cd00d098363b543973a078e722401fc687d6c9
--- /dev/null
+++ b/microlytics/lib/channels.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// 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.
+
+library microlytics.channels;
+
+import 'dart:async';
+
+final String ANALYTICS_URL = "https://www.google-analytics.com/collect";
ahe 2014/09/03 08:51:31 I'm not sure this is related to channels.
lukechurch 2014/09/04 12:58:41 True. I thought that having a separate class to de
+
+abstract class Channel {
+ void sendData(String data);
+ void shutdown() {}
+}
+
+/// [Channel] that implements a leaky bucket
+/// algorithm to provide rate limiting.
+/// See (http://en.wikipedia.org/wiki/Leaky_bucket)
ahe 2014/09/03 08:51:31 Add period, and use [] instead of ().
lukechurch 2014/09/04 12:58:41 Done.
+class RateLimitingBufferedChannel extends Channel {
+ final List<String> _buffer = <String>[];
+ final Channel _innerChannel;
+ final int bufferSizeLimit;
+ Timer _timer;
+
+ RateLimitingBufferedChannel(
+ this._innerChannel,
+ { this.bufferSizeLimit: 10,
ahe 2014/09/03 08:51:31 Remove space after {.
lukechurch 2014/09/04 12:58:41 Done.
+ double packetsPerSecond: 1.0}) {
+ if (!(packetsPerSecond > 0)) {
+ throw new ArgumentError("packetsPerSecond must be larger than zero");
+ }
+
+ int transmitDelay = (1000 / packetsPerSecond).floor();
+ _timer = new Timer(new Duration(milliseconds: transmitDelay), () {
+ _onTimerTick();
+ });
+ }
+
+ void _onTimerTick() {
+ if (_buffer.length > 0) {
+ String item = _buffer.removeLast();
+ _innerChannel.sendData(item);
+ }
+ }
+
+ void sendData(String data) {
+ if (_buffer.length >= bufferSizeLimit) return;
+ _buffer.add(data);
+ }
+
+ void shutdown() {
+ _timer.cancel();
+ _innerChannel.shutdown();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698