Chromium Code Reviews| OLD | NEW |
|---|---|
| (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. | |
|
ahe
2014/09/02 13:44:23
Add line between copyright and library declaration
lukechurch
2014/09/02 19:52:36
Done.
| |
| 4 library microlytics.channels; | |
| 5 | |
| 6 import 'dart:async'; | |
| 7 | |
| 8 abstract class Channel { | |
| 9 void sendData(String data); | |
| 10 void shutdown() {} | |
| 11 } | |
| 12 | |
| 13 /// [Channel] that implements a leaky bucket | |
| 14 /// algorithm to provide rate limiting. | |
| 15 /// See http://en.wikipedia.org/wiki/Leaky_bucket | |
|
ahe
2014/09/02 13:44:23
Period.
lukechurch
2014/09/02 19:52:37
That will cause the URL to break, I'll use parens
| |
| 16 class RateLimitingBufferedChannel extends Channel { | |
| 17 final List<String> _buffer = <String>[]; | |
| 18 final Channel _innerChannel; | |
| 19 int _bufferSizeLimit; | |
|
ahe
2014/09/02 13:44:23
Is this value going to change over the lifetime of
lukechurch
2014/09/02 19:52:36
There's a language feature interaction problem her
ahe
2014/09/03 08:51:31
You can write:
RateLimitingBufferedChannel(
lukechurch
2014/09/03 11:27:16
Done.
| |
| 20 Timer _timer; | |
|
ahe
2014/09/02 13:44:23
Same question.
lukechurch
2014/09/02 19:52:36
No, but, making this final would imply moving the
ahe
2014/09/03 08:51:31
The current code isn't readable, as you're mixing
lukechurch
2014/09/03 11:27:16
Acknowledged.
| |
| 21 | |
| 22 RateLimitingBufferedChannel( | |
| 23 this._innerChannel, | |
| 24 { bufferSizeLimit: 10, double packetsPerSecond: 1.0}) { | |
|
ahe
2014/09/02 13:44:23
bufferSizeLimit isn't typed.
ahe
2014/09/02 13:44:23
One line per parameter.
lukechurch
2014/09/02 19:52:36
Done.
| |
| 25 | |
|
ahe
2014/09/02 13:44:23
Remove extra line.
lukechurch
2014/09/02 19:52:36
This makes is really hard to see where the init en
ahe
2014/09/03 08:51:30
I can live with the extra line in those cases. How
lukechurch
2014/09/03 11:27:16
Acknowledged.
| |
| 26 if (!(packetsPerSecond > 0)) { | |
| 27 throw new ArgumentError("packetsPerSecond must be positive"); | |
|
ahe
2014/09/02 13:44:23
Period.
Most people with a long education probabl
lukechurch
2014/09/02 19:52:36
Good point. Done.
| |
| 28 } | |
| 29 this._bufferSizeLimit = bufferSizeLimit; | |
|
ahe
2014/09/02 13:44:23
Use initializer.
lukechurch
2014/09/02 19:52:36
Done.
| |
| 30 | |
| 31 int transmitDelay = (1000 / packetsPerSecond).floor(); | |
| 32 _timer = new Timer(new Duration(milliseconds: transmitDelay), () { | |
| 33 _onTimerTick(); | |
| 34 }); | |
| 35 } | |
| 36 | |
| 37 void _onTimerTick() { | |
| 38 if (_buffer.length > 0) { | |
| 39 String item = _buffer.removeLast(); | |
| 40 _innerChannel.sendData(item); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void sendData(String data) { | |
| 45 if (_buffer.length >= _bufferSizeLimit) return; | |
| 46 _buffer.add(data); | |
| 47 } | |
| 48 | |
| 49 void shutdown() { | |
| 50 _timer.cancel(); | |
| 51 _innerChannel.shutdown(); | |
| 52 } | |
| 53 } | |
| OLD | NEW |