The Top 7 Mistakes Newbies Make with Akka.NET explains why using async/await within an actor is often a bad idea:
[...] we see end users develop lots of nested async / await operations inside an individual message handler. There’s a cost overlooked by most users to doing this: the actor can’t process any other messages between each await operation because those awaits are still part of the “1 message at a time” guarantee for the original message!
Yet in Unit 3 Lesson 4 of the Petabridge Akka .NET Bootcamp, this example is considered okay:
// asynchronously download the image and pipe the results to ourself
_httpClient.GetAsync(imageUrl).ContinueWith(httpRequest =>
{
var response = httpRequest.Result;
// successful img download
if (response.StatusCode == HttpStatusCode.OK)
{
var contentStream = response.Content.ReadAsStreamAsync();
try
{
contentStream.Wait(TimeSpan.FromSeconds(1));
return new ImageDownloadResult(image,
response.StatusCode, contentStream.Result);
}
catch //timeout exceptions!
{
return new ImageDownloadResult(image, HttpStatusCode.PartialContent);
}
}
return new ImageDownloadResult(image, response.StatusCode);
},
TaskContinuationOptions.ExecuteSynchronously)
.PipeTo(Self);
The way I understand this, the actor will not be able to process any other messages until GetAsync()
AND ContinueWith()
are done, which is exactly the problem PipeTo()
was trying to avoid.
Am I missing something here?