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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library microlytics.channels;
6
7 import 'dart:async';
8
9 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
10
11 abstract class Channel {
12 void sendData(String data);
13 void shutdown() {}
14 }
15
16 /// [Channel] that implements a leaky bucket
17 /// algorithm to provide rate limiting.
18 /// 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.
19 class RateLimitingBufferedChannel extends Channel {
20 final List<String> _buffer = <String>[];
21 final Channel _innerChannel;
22 final int bufferSizeLimit;
23 Timer _timer;
24
25 RateLimitingBufferedChannel(
26 this._innerChannel,
27 { this.bufferSizeLimit: 10,
ahe 2014/09/03 08:51:31 Remove space after {.
lukechurch 2014/09/04 12:58:41 Done.
28 double packetsPerSecond: 1.0}) {
29 if (!(packetsPerSecond > 0)) {
30 throw new ArgumentError("packetsPerSecond must be larger than zero");
31 }
32
33 int transmitDelay = (1000 / packetsPerSecond).floor();
34 _timer = new Timer(new Duration(milliseconds: transmitDelay), () {
35 _onTimerTick();
36 });
37 }
38
39 void _onTimerTick() {
40 if (_buffer.length > 0) {
41 String item = _buffer.removeLast();
42 _innerChannel.sendData(item);
43 }
44 }
45
46 void sendData(String data) {
47 if (_buffer.length >= bufferSizeLimit) return;
48 _buffer.add(data);
49 }
50
51 void shutdown() {
52 _timer.cancel();
53 _innerChannel.shutdown();
54 }
55 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698