Basics of the actix framework

The actix crate provides a well-organized actors model that's simple to use. There are main concepts you should remember. At first, the actix crate has the System type, which is the main type to maintain actors system. You have to create the System instance before you create and spawn any actor. Actually, System is an Actor that controls the whole process and can be used to stop an application.

Actor is the most-used trait of the framework. Every type that implements the Actor trait can be spawned. We'll implement this trait for our types later in this chapter. Also, the actix crate contains the Arbiter type, which is an event loop controller that have to be one per thread. There's SyncArbiter to run CPU-boud tasks, and this arbiter uses pools of threads to perform actors.

Every Actor has to work in a Context, which is an environment to a runtime and can be used to spawn other tasks. Also, every Actor instance takes an Address and you can use it to send messages to actors and receive responses. We'll store in our example addresses of all necessary actors to a shared state to use them from different handlers in parallel.

Address provides a send method that expects a type that implements the Message trait. To implement message-handling for Actor, you have to implement the Handler trait for the actor's type.

Let's create three actors for our resizing microservice.

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

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