File tree 2 files changed +22
-13
lines changed
src/core/Akka.Docs.Tests/Streams
2 files changed +22
-13
lines changed Original file line number Diff line number Diff line change @@ -96,6 +96,16 @@ when a WebSocket connection fails due to the HTTP server it's running on going d
96
96
By using an exponential backoff, we avoid going into a tight reconnect look, which both gives the HTTP server some time
97
97
to recover, and it avoids using needless resources on the client side.
98
98
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
+
99
109
The following snippet shows how to create a backoff supervisor using ` Akka.Streams.Dsl.RestartSource `
100
110
which will supervise the given ` Source ` . The ` Source ` in this case is a
101
111
` HttpResponseMessage ` , produced by ` HttpCLient ` . If the stream fails or completes at any point, the request will
Original file line number Diff line number Diff line change 8
8
using System ;
9
9
using System . Linq ;
10
10
using System . Net . Http ;
11
- using System . Threading . Tasks ;
12
- using Akka ;
13
11
using Akka . Streams ;
14
12
using Akka . Streams . Dsl ;
15
13
using Akka . TestKit . Xunit2 ;
16
- using FluentAssertions ;
17
14
using Xunit ;
18
15
using Xunit . Abstractions ;
19
16
@@ -39,19 +36,21 @@ public void Restart_stages_should_demonstrate_a_restart_with_backoff_source()
39
36
#region restart-with-backoff-source
40
37
var httpClient = new HttpClient ( ) ;
41
38
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 (
51
40
minBackoff : TimeSpan . FromSeconds ( 3 ) ,
52
41
maxBackoff : TimeSpan . FromSeconds ( 30 ) ,
53
42
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 ) ;
55
54
#endregion
56
55
57
56
#region with-kill-switch
You can’t perform that action at this time.
0 commit comments