@@ -8,8 +8,7 @@ import 'dart:io';
88import 'package:http/http.dart' as http;
99import 'package:stack_trace/stack_trace.dart' ;
1010
11- // Reporting disabled until we're set up on the backend.
12- const bool CRASH_REPORTING_DISABLED = true ;
11+ import 'src/utils.dart' ;
1312
1413/// Tells crash backend that this is a Dart error (as opposed to, say, Java).
1514const String _dartTypeId = 'DartError' ;
@@ -37,21 +36,37 @@ class CrashReportSender {
3736 static final Uri _baseUri = new Uri (
3837 scheme: 'https' , host: _crashServerHost, path: _crashEndpointPath);
3938
39+ static const int _maxReportsToSend = 1000 ;
40+
4041 final String crashProductId;
4142 final EnablementCallback shouldSend;
4243 final http.Client _httpClient;
4344
44- /// Create a new [CrashReportSender] , using the data from the given
45- /// [Analytics] instance.
46- CrashReportSender (this .crashProductId, this .shouldSend,
47- {http.Client httpClient})
48- : _httpClient = httpClient ?? new http.Client ();
45+ final ThrottlingBucket _throttle = ThrottlingBucket (10 , Duration (minutes: 1 ));
46+ int _reportsSend = 0 ;
47+
48+ /// Create a new [CrashReportSender] .
49+ CrashReportSender (
50+ this .crashProductId,
51+ this .shouldSend, {
52+ http.Client httpClient,
53+ }) : _httpClient = httpClient ?? new http.Client ();
4954
5055 /// Sends one crash report.
5156 ///
5257 /// The report is populated from data in [error] and [stackTrace] .
5358 Future sendReport (dynamic error, {StackTrace stackTrace}) async {
54- if (! shouldSend () || CRASH_REPORTING_DISABLED ) {
59+ if (! shouldSend ()) {
60+ return ;
61+ }
62+
63+ // Check if we've sent too many reports recently.
64+ if (! _throttle.removeDrop ()) {
65+ return ;
66+ }
67+
68+ // Don't send too many total reports to crash reporting.
69+ if (_reportsSend >= _maxReportsToSend) {
5570 return ;
5671 }
5772
0 commit comments