Skip to content

Commit

Permalink
Added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvaluyskiy committed Sep 19, 2017
1 parent 02f3e27 commit 575c84e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
12 changes: 4 additions & 8 deletions docs/articles/streams/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ to recover, and it avoids using needless resources on the client side.

The following snippet shows how to create a backoff supervisor using `Akka.Streams.Dsl.RestartSource`
which will supervise the given `Source`. The `Source` in this case is a
stream of Server Sent Events, produced by akka-http. If the stream fails or completes at any point, the request will
`HttpResponseMessage`, produced by `HttpCLient`. If the stream fails or completes at any point, the request will
be made again, in increasing intervals of 3, 6, 12, 24 and finally 30 seconds (at which point it will remain capped due
to the `maxBackoff` parameter):

```C#
// TODO
```
[!code-csharp[RestartDocTests.cs](../../examples/DocsExamples/Streams/RestartDocTests.cs?name=restart-with-backoff-source)]

Using a `randomFactor` to add a little bit of additional variance to the backoff intervals
is highly recommended, in order to avoid multiple streams re-start at the exact same point in time,
Expand All @@ -116,11 +114,9 @@ large spikes of traffic hitting the recovering server or other resource that the
The above `RestartSource` will never terminate unless the `Sink` it's fed into cancels. It will often be handy to use
it in combination with a `KillSwitch`, so that you can terminate it when needed:

```C#
// TODO
```
[!code-csharp[RestartDocTests.cs](../../examples/DocsExamples/Streams/RestartDocTests.cs?name=with-kill-switch)]

Sinks and flows can also be supervised, using `Akka.Streams.Dsl.RestartSink` and `Akka.Streams.Dsl.RestartFlow`].
Sinks and flows can also be supervised, using `Akka.Streams.Dsl.RestartSink` and `Akka.Streams.Dsl.RestartFlow`.
The `RestartSink` is restarted when it cancels, while the `RestartFlow` is restarted when either the in port cancels,
the out port completes, or the out port sends an error.

Expand Down
62 changes: 62 additions & 0 deletions docs/examples/DocsExamples/Streams/RestartDocTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Akka;
using Akka.Streams;
using Akka.Streams.Dsl;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace DocsExamples.Streams
{
public class RestartDocTests : TestKit
{
private ActorMaterializer Materializer { get; }

public RestartDocTests(ITestOutputHelper output)
: base("", output)
{
Materializer = Sys.Materializer();
}

private void DoSomethingElse()
{
}

[Fact]
public void Restart_stages_should_demonstrate_a_restart_with_backoff_source()
{
#region restart-with-backoff-source
var httpClient = new HttpClient();

var restartSource = RestartSource.WithBackoff(() =>
{
// Create a source from a task
return Source.FromTask(
httpClient.GetAsync("http://example.com/eventstream") // Make a single request
)
.Select(c => c.Content.ReadAsStringAsync())
.Select(c => c.Result);
},
minBackoff: TimeSpan.FromSeconds(3),
maxBackoff: TimeSpan.FromSeconds(30),
randomFactor: 0.2 // adds 20% "noise" to vary the intervals slightly
);
#endregion

#region with-kill-switch
var killSwitch = restartSource
.ViaMaterialized(KillSwitches.Single<string>(), Keep.Right)
.ToMaterialized(Sink.ForEach<string>(evt => Console.WriteLine($"Got event: {evt}")), Keep.Left)
.Run(Materializer);

DoSomethingElse();

killSwitch.Shutdown();
#endregion
}
}
}

0 comments on commit 575c84e

Please sign in to comment.