I'm doing some Akka lately and wonder: Can I do blocking I/O in Akka without getting into big trouble? Let us say we have an Actor which does some blocking I/O because it uses a legacy library or for any other reason: Couldn't I just use a special dispatcher for those Actors which a reasonably sized ThreadPool and do blocking I/O without blocking all other actors because they run with a different dispatcher?
What are the downsides of this? And what would be the optimal way to call a 3rd party HTTP-API from an actor?
Dispatchers are responsible for scheduling all code that run inside the ActorSystem . Dispatchers are one of the most important parts of Akka.NET, as they control the throughput and time share for each of the actors, giving each one a fair share of resources. By default, all actors share a single Global Dispatcher.
Defining an Actor In Akka, actors are guaranteed to be run in a single-threaded illusion, which means that the Akka framework takes care of threading issues while allowing us to focus on the behavior that needs to be implemented. Actors may only communicate with each other and the outside world by through messages.
An Akka MessageDispatcher is what makes Akka Actors “tick”, it is the engine of the machine so to speak. All MessageDispatcher implementations are also an ExecutionContext , which means that they can be used to execute arbitrary code, for instance Futures.
Akka actors are asynchronous from the beginning. Reactive programs can be implemented in either way, synchronous and asynchronous.
Doing blocking IO is a bad idea in general, and in a reactive multithreaded environment in particular, so your first step is to try to avoid it alltogether, that means looking into using AsyncHttpClient
or HttpAsyncClient
.
If that does not work, you can at least mitigate the risks by giving the blocking actors their own threads. This will of course be costly and you still risk filling up their mailboxes, but such is the choice of using blocking IO.
You also might want to look at the IO Actor module for a more raw interface to network IO.
Hope any of this helps,
Cheers, √
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With