Files
sdk/pkg/microlytics/lib/channels.dart
T
lukechurch@google.com 710bf6f401 First cut of basic functionality
This CL addresses comments from the previous Pull Request https://github.com/lukechurch/dart-microlytics/pull/1

It can be used to reporting usage and performance data to Google Analytics.

Known issues:

-> unittest library is still used
-> some comments are still not in full sentences

R=ahe@google.com, danrubel@google.com

Review URL: https://codereview.chromium.org//515993003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@39914 260f80e4-7a28-3924-810f-c04153c831b5
2014-09-05 14:00:08 +00:00

55 lines
1.5 KiB
Dart

// 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';
const String ANALYTICS_URL = "https://ssl.google-analytics.com/collect";
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].
class RateLimitingBufferedChannel extends Channel {
final List<String> _buffer = <String>[];
final Channel _innerChannel;
final int _bufferSizeLimit;
Timer _timer;
RateLimitingBufferedChannel(
this._innerChannel,
{int bufferSizeLimit: 10,
double packetsPerSecond: 1.0})
: this._bufferSizeLimit = bufferSizeLimit {
if (!(packetsPerSecond > 0)) {
throw new ArgumentError("packetsPerSecond must be larger than zero.");
}
int transmitDelay = (1000 / packetsPerSecond).floor();
_timer = new Timer.periodic(
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();
}
}