Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suspend http transactions for 30 seconds in case of failure #71

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions rollbar_dart/lib/src/sender/http_sender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class HttpSender implements Sender {
@override
Future<bool> sendString(String payload) async {
try {
if (_State.suspended) {
throw HttpException(
akornich marked this conversation as resolved.
Show resolved Hide resolved
'HTTP transactions are currently suspended.',
uri: _endpoint,
);
}

final response = await http
.post(_endpoint, headers: _headers, body: payload)
.then(Response.from);
Expand All @@ -52,6 +59,8 @@ class HttpSender implements Sender {

return true;
} catch (error, stackTrace) {
if (!_State.suspended) _State.suspend(30.seconds);

log('Exception sending payload',
time: DateTime.now(),
level: Level.error.value,
akornich marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -63,3 +72,16 @@ class HttpSender implements Sender {
}
}
}

extension _State on HttpSender {
static bool _suspended = false;

static bool get suspended => _suspended;

static void suspend(Duration duration) async {
if (_suspended) return;
_suspended = true;
await Future.delayed(duration);
_suspended = false;
}
}