Actix

Pirmin Kalberer @implgeo

Rust Zurich, 11. Juli 2018

About me

Language history

Basic

Assembler (6502)

(Turbo) Pascal

Modula

C

C++

Java

Eiffel

Perl

Ruby

Javascript

Python

Rust (2016)

Sourcepole

  • Geospatial software (C++, Python, React, Ruby on Rails, …)
  • Creating maps with Rust!

Rust Web Frameworks

Hyper

  • Low-Level
  • Synchronous -> Asynchronous
  • Base of iron, gotham, rocket, nickel, …

Rocket

  • Nice API & concepts
  • Beautiful documentation
  • Requires Nightly
  • Synchronous

actix-web

Benchmark

TechEmpower Web Framework Benchmark

Performance high-water marks for trivial exercises of framework functionality (routing, ORM, templates, etc.).

Real world apps will be substantially more complex with far lower RPS.

JSON serialization

JSON serialization of a freshly-instantiated object.

{"message":"Hello, World!"}

Single query

Fetching a single row from a simple database table and serializing as a JSON response.

{"id":3217,"randomNumber":2149}

Multiple queries

Fetching multiple rows from a simple database table and serializing these rows as a JSON response.

The test is run multiple times: testing 1, 5, 10, 15, and 20 queries per request. All tests are run at 256 concurrency.

Fortunes

The framework’s ORM is used to fetch all rows from a database table containing an unknown number of Unix fortune cookie messages. An additional fortune cookie message is inserted into the list at runtime and then the list is sorted by the message text. Finally, the list is delivered to the client using a server-side HTML template.

Data updates

Fetching multiple rows from a simple database table, converting the rows to in-memory objects, modifying one attribute of each object in memory, updating each associated row in the database individually, and then serializing the list of objects as a JSON response.

Plaintext

“Hello, World” message rendered as plain text.

Hello, World!

Synchronous vs. Asynchronous

Synchronous

Single Threaded:

Multi-Threaded:

https://codewala.net/2015/07/29/concurrency-vs-multi-threading-vs-asynchronous-programming-explained/

Asynchronous

Single Threaded:

Multi-Threaded:

Actix actors framework

Actor model

An actor is a computational entity that contains state information and can send, receive and handle messages.

https://www.brianstorti.com/the-actor-model/

Actix features

  • Async/Sync actors
  • Actor communication in a local/thread context
  • Uses Futures for asynchronous message handling
  • Actor supervision
  • Typed messages

Actor

Message

Handler

System

Actix web

Features

  • Support for HTTP/1.x and HTTP/2.0 protocols
  • Streaming and pipelining
  • Keep-alive and slow requests handling
  • Client/server WebSockets support
  • Transparent content compression/decompression (br, gzip, deflate)
  • Configurable request routing
  • Graceful server shutdown
  • Multipart streams
  • Static assets
  • SSL support with OpenSSL or native-tls
  • Middleware (Logger, Session, Redis sessions, DefaultHeaders, CORS, CSRF)
  • Includes an asynchronous HTTP client
  • Built on top of Actix actor framework

https://actix.rs

Hello World

Application State

Application state is shared with all routes and resources within the same application (App).

Note: Http server constructs an application instance for each thread, thus application state must be constructed multiple times.

Request handler

Handler trait: A request handler accepts an HttpRequest instance as parameter and returns a type that can be converted into HttpResponse (Responder trait).

Async handlers

Path info extraction

Option 2: Access by calling extract() on the extractor

Path info extraction with structs

Query parameter extraction

JSON in requests

JSON in responses

Form handling

Multiple extractors

Actix provides extractor implementations for tuples (up to 10 elements) whose elements implement FromRequest.

Request routing

Middleware - CORS

Middleware - CORS (allow from all)

Middleware - CSRF

Origin Header based.

Middleware - User Sessions

  • Built-in: Session Cookie
  • Other implementations must implement SessionBackend

Middleware - Identity handling

  • Built-in: Cookie based identity
  • Other implementations must implement RequestIdentity

Static file handler

Testing support

HTTP Client API

Real World Applications

Fundstelleninventar

  • JsonAPI served with Actix
  • DB access with Diesel
  • React client app

Time tracking UI

  • JsonAPI served with Actix
  • DB access with Diesel
  • React client app

t-rex

  • Vector Tile Server
  • Command line and web server
  • Actix-Web + Actix (planned)
  • Integrated web app
  • PostGIS + GDAL data sources

https://t-rex.tileserver.ch/