Skip to content

Commit 752122d

Browse files
committed
Update documentation and code snippets
1 parent 1fcadd6 commit 752122d

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

docs/articles/streams/error-handling.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ when a WebSocket connection fails due to the HTTP server it's running on going d
9696
By using an exponential backoff, we avoid going into a tight reconnect look, which both gives the HTTP server some time
9797
to recover, and it avoids using needless resources on the client side.
9898

99+
The various restart shapes mentioned all expect an `Akka.Stream.RestartSettings` which configures the restart behavior.
100+
101+
Configurable parameters are:
102+
103+
* `minBackoff` is the initial duration until the underlying stream is restarted
104+
* `maxBackoff` caps the exponential backoff
105+
* `randomFactor` allows addition of a random delay following backoff calculation
106+
* `maxRestarts` caps the total number of restarts
107+
* `maxRestartsWithin` sets a timeframe during which restarts are counted towards the same total for `maxRestarts`
108+
99109
The following snippet shows how to create a backoff supervisor using `Akka.Streams.Dsl.RestartSource`
100110
which will supervise the given `Source`. The `Source` in this case is a
101111
`HttpResponseMessage`, produced by `HttpCLient`. If the stream fails or completes at any point, the request will

src/core/Akka.Docs.Tests/Streams/RestartDocTests.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
using System;
99
using System.Linq;
1010
using System.Net.Http;
11-
using System.Threading.Tasks;
12-
using Akka;
1311
using Akka.Streams;
1412
using Akka.Streams.Dsl;
1513
using Akka.TestKit.Xunit2;
16-
using FluentAssertions;
1714
using Xunit;
1815
using Xunit.Abstractions;
1916

@@ -39,19 +36,21 @@ public void Restart_stages_should_demonstrate_a_restart_with_backoff_source()
3936
#region restart-with-backoff-source
4037
var httpClient = new HttpClient();
4138

42-
var restartSource = RestartSource.WithBackoff(() =>
43-
{
44-
// Create a source from a task
45-
return Source.FromTask(
46-
httpClient.GetAsync("http://example.com/eventstream") // Make a single request
47-
)
48-
.Select(c => c.Content.ReadAsStringAsync())
49-
.Select(c => c.Result);
50-
},
39+
var settings = RestartSettings.Create(
5140
minBackoff: TimeSpan.FromSeconds(3),
5241
maxBackoff: TimeSpan.FromSeconds(30),
5342
randomFactor: 0.2 // adds 20% "noise" to vary the intervals slightly
54-
);
43+
).WithMaxRestarts(20, TimeSpan.FromMinutes(5)); // limits the amount of restarts to 20 within 5 minutes
44+
45+
var restartSource = RestartSource.WithBackoff(() =>
46+
{
47+
// Create a source from a task
48+
return Source.FromTask(
49+
httpClient.GetAsync("http://example.com/eventstream") // Make a single request
50+
)
51+
.Select(c => c.Content.ReadAsStringAsync())
52+
.Select(c => c.Result);
53+
}, settings);
5554
#endregion
5655

5756
#region with-kill-switch

0 commit comments

Comments
 (0)