123456789_123456789_123456789_123456789_123456789_

Architecture

Overview

https://bit.ly/2iJuFky

Puma is a threaded Ruby HTTP application server processing requests across a TCP and/or UNIX socket.

Puma processes (there can be one or many) accept connections from the socket via a thread (in the Reactor class). The connection, once fully buffered and read, moves into the todo list, where an available thread will pick it up (in the ThreadPool class).

Puma works in two main modes: cluster and single. In single mode, only one Puma process boots. In cluster mode, a master process is booted, which prepares (and may boot) the application and then uses the fork() system call to create one or more child processes. These child processes all listen to the same socket. The master process does not listen to the socket or process requests - its purpose is primarily to manage and listen for UNIX signals and possibly kill or boot child processes.

We sometimes call child processes (or Puma processes in single mode) workers, and we sometimes call the threads created by Puma's ThreadPool worker threads.

How Requests Work

https://bit.ly/2zwzhEK

queue_requests

https://bit.ly/2zxCJ1Z

The queue_requests option is true by default, enabling the separate reactor thread used to buffer requests as described above.

If set to false, this buffer will not be used for connections while waiting for the request to arrive.

In this mode, when a connection is accepted, it is added to the "todo" queue immediately, and a worker will synchronously do any waiting necessary to read the HTTP request from the socket.