Actors

Threads and thread pools are good ways to utilize more resources of a server, but it's a tedious programming style. You have to think about a lot of details: sending and receiving messages, load distribution, and respawning failed threads.

There's another approach to run tasks concurrently: actors. The actors model is a computational model that uses computational primitives called actors. They work in parallel and interact with each other by passing messages. It's a more flexible approach than using threads or pools, because you delegate every complex task to a separate actor that receives messages and return results to any entity that sent a request to an actor. Your code becomes well structured and you can even reuse actors for different projects.

We already studied futures and tokio crates, which are tricky to use directly, but they're a good foundation to build asynchronous computational frameworks, and especially it's good to implement actors model. The actix crate already did that: it's based on both crates to bring an actors model to Rust. Let's study how we can use actors to perform background tasks.

We'll re-implement the resizing microservice, but add three actors: a resizing actor, a counting actor, which counts the amount of requests, and a logging actor, which will write the count values to syslog.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.141.75