# benoitc/gunicorn architecture

> Architecture diagram of the GitHub repository benoitc/gunicorn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/benoitc/gunicorn
- Repository: https://github.com/benoitc/gunicorn

## Overview

Gunicorn is a Unix pre-fork application server: an operator starts a WSGI or ASGI app, the master configures listeners and manages workers, and workers handle requests and return application responses. Its product also includes HTTP/2, uWSGI protocol support, a dirty-worker pool for persistent heavy workloads, and a Unix-socket control interface. The README documents these capabilities; some implementation paths were not sampled, so uncertain runtime links are omitted or marked unverified.

Main entry points: [`gunicorn/arbiter.py`](https://github.com/benoitc/gunicorn/blob/master/gunicorn/arbiter.py) (Master Arbiter), [`gunicorn/ctl/server.py`](https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/server.py) (Control Socket), [`gunicorn/app/base.py`](https://github.com/benoitc/gunicorn/blob/master/gunicorn/app/base.py) (App Loading), [`gunicorn/workers/sync.py`](https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/sync.py) (Sync Worker)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_startup["Startup and Supervision"]
  node_cli["CLI<br/>[cli.py]"]
  node_app_loader["App Loading<br/>[base.py]"]
  node_config["Configuration<br/>[config.py]"]
  node_master["Master Arbiter<br/>[arbiter.py]"]
  node_listeners["Sockets<br/>[sock.py]"]
end

subgraph group_protocols["Request Protocols"]
  node_http1["HTTP/1 Parsing<br/>[message.py]"]
  node_http_parser["HTTP Parser<br/>[parser.py]"]
  node_http_body["Request Bodies<br/>[body.py]"]
  node_http2_negotiation["HTTP/2 Negotiation<br/>[negotiation.py]"]
  node_http2_connection["HTTP/2 Connections"]
  node_http2_stream["HTTP/2 Streams<br/>[stream.py]"]
  node_asgi["ASGI Protocol<br/>[protocol.py]"]
  node_uwsgi["uWSGI Protocol<br/>[message.py]"]
end

subgraph group_workers["Application Workers"]
  node_app["WSGI / ASGI App<br/>[wsgiapp.py]"]
  node_worker_base["Worker Lifecycle<br/>[base.py]"]
  node_sync_worker["Sync Worker<br/>[sync.py]"]
  node_async_worker["Async Workers<br/>[base_async.py]"]
  node_asgi_worker["ASGI Worker<br/>[gasgi.py]"]
end

subgraph group_dirty["Dirty Worker Pool"]
  node_dirty_arbiter["Dirty Arbiter<br/>[arbiter.py]"]
  node_dirty_worker["Dirty Worker<br/>[worker.py]"]
  node_dirty_app["Persistent App<br/>[app.py]"]
  node_dirty_client["Dirty Client<br/>[client.py]"]
  node_dirty_protocol["Dirty Protocol<br/>[protocol.py]"]
  node_dirty_stash["Dirty Stash<br/>[stash.py]"]
end

subgraph group_control["Control and Operations"]
  node_control_server["Control Socket<br/>[server.py]"]
  node_control_handlers["Command Handlers<br/>[handlers.py]"]
  node_control_client["Control Client<br/>[client.py]"]
  node_statsd["StatsD Metrics<br/>[statsd.py]"]
end

node_operator(("Operator"))
node_http_client(("HTTP Client"))
node_nginx(("nginx"))

node_operator -->|"starts server"| node_cli
node_cli -.->|"loads app"| node_app_loader
node_app_loader -->|"uses settings"| node_config
node_app_loader -->|"starts arbiter"| node_master
node_master -->|"reads settings"| node_config
node_master -->|"creates listeners"| node_listeners
node_master -.->|"manages workers"| node_worker_base
node_http_client -.->|"connects"| node_listeners
node_listeners -.->|"accepts requests"| node_sync_worker
node_sync_worker -.->|"parses requests"| node_http_parser
node_http_parser -->|"constructs messages"| node_http1
node_http1 -->|"reads bodies"| node_http_body
node_sync_worker -.->|"invokes app"| node_app
node_async_worker -.->|"serves app"| node_app
node_asgi_worker -.->|"runs protocol"| node_asgi
node_asgi -.->|"dispatches requests"| node_app
node_async_worker -->|"negotiates protocol"| node_http2_negotiation
node_http2_negotiation -.->|"selects connection"| node_http2_connection
node_http2_connection -->|"manages streams"| node_http2_stream
node_nginx -->|"uses binary protocol"| node_uwsgi
node_master -.->|"starts dirty pool"| node_dirty_arbiter
node_dirty_arbiter -.->|"manages workers"| node_dirty_worker
node_dirty_worker -->|"runs persistent app"| node_dirty_app
node_dirty_client -->|"encodes requests"| node_dirty_protocol
node_dirty_worker -->|"handles messages"| node_dirty_protocol
node_dirty_client -->|"uses stash"| node_dirty_stash
node_master -->|"hosts control socket"| node_control_server
node_control_server -->|"dispatches commands"| node_control_handlers
node_control_client -.->|"sends commands"| node_control_server
node_statsd -.->|"reports metrics"| node_control_server

click node_cli "https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/cli.py"
click node_app_loader "https://github.com/benoitc/gunicorn/blob/master/gunicorn/app/base.py"
click node_config "https://github.com/benoitc/gunicorn/blob/master/gunicorn/config.py"
click node_master "https://github.com/benoitc/gunicorn/blob/master/gunicorn/arbiter.py"
click node_listeners "https://github.com/benoitc/gunicorn/blob/master/gunicorn/sock.py"
click node_http1 "https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/message.py"
click node_http_parser "https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/parser.py"
click node_http_body "https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/body.py"
click node_http2_negotiation "https://github.com/benoitc/gunicorn/blob/master/gunicorn/http2/negotiation.py"
click node_http2_connection "https://github.com/benoitc/gunicorn/blob/master/gunicorn/http2/async_connection.py"
click node_http2_stream "https://github.com/benoitc/gunicorn/blob/master/gunicorn/http2/stream.py"
click node_asgi "https://github.com/benoitc/gunicorn/blob/master/gunicorn/asgi/protocol.py"
click node_uwsgi "https://github.com/benoitc/gunicorn/blob/master/gunicorn/uwsgi/message.py"
click node_app "https://github.com/benoitc/gunicorn/blob/master/gunicorn/app/wsgiapp.py"
click node_worker_base "https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/base.py"
click node_sync_worker "https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/sync.py"
click node_async_worker "https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/base_async.py"
click node_asgi_worker "https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/gasgi.py"
click node_dirty_arbiter "https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/arbiter.py"
click node_dirty_worker "https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/worker.py"
click node_dirty_app "https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/app.py"
click node_dirty_client "https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/client.py"
click node_dirty_protocol "https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/protocol.py"
click node_dirty_stash "https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/stash.py"
click node_control_server "https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/server.py"
click node_control_handlers "https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/handlers.py"
click node_control_client "https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/client.py"
click node_statsd "https://github.com/benoitc/gunicorn/blob/master/gunicorn/instrument/statsd.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_app_loader,node_config,node_master,node_listeners,node_http_client toneBlue
class node_http1,node_http_parser,node_http_body,node_http2_negotiation,node_http2_connection,node_http2_stream,node_asgi,node_uwsgi toneAmber
class node_app,node_worker_base,node_sync_worker,node_async_worker,node_asgi_worker toneMint
class node_dirty_arbiter,node_dirty_worker,node_dirty_app,node_dirty_client,node_dirty_protocol,node_dirty_stash toneRose
class node_control_server,node_control_handlers,node_control_client,node_statsd,node_operator,node_nginx toneIndigo
```

## Components

### Startup and Supervision

- [CLI](https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/cli.py): `gunicorn/ctl/cli.py`
- [App Loading](https://github.com/benoitc/gunicorn/blob/master/gunicorn/app/base.py): `gunicorn/app/base.py`
- [Configuration](https://github.com/benoitc/gunicorn/blob/master/gunicorn/config.py): `gunicorn/config.py`
- [Master Arbiter](https://github.com/benoitc/gunicorn/blob/master/gunicorn/arbiter.py): `gunicorn/arbiter.py`
- [Sockets](https://github.com/benoitc/gunicorn/blob/master/gunicorn/sock.py): `gunicorn/sock.py`

### Request Protocols

- [HTTP/1 Parsing](https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/message.py): `gunicorn/http/message.py`
- [HTTP Parser](https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/parser.py): `gunicorn/http/parser.py`
- [Request Bodies](https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/body.py): `gunicorn/http/body.py`
- [HTTP/2 Negotiation](https://github.com/benoitc/gunicorn/blob/master/gunicorn/http2/negotiation.py): `gunicorn/http2/negotiation.py`
- [HTTP/2 Connections](https://github.com/benoitc/gunicorn/blob/master/gunicorn/http2/async_connection.py): `gunicorn/http2/async_connection.py`
- [HTTP/2 Streams](https://github.com/benoitc/gunicorn/blob/master/gunicorn/http2/stream.py): `gunicorn/http2/stream.py`
- [ASGI Protocol](https://github.com/benoitc/gunicorn/blob/master/gunicorn/asgi/protocol.py): `gunicorn/asgi/protocol.py`
- [uWSGI Protocol](https://github.com/benoitc/gunicorn/blob/master/gunicorn/uwsgi/message.py): `gunicorn/uwsgi/message.py`

### Application Workers

- [WSGI / ASGI App](https://github.com/benoitc/gunicorn/blob/master/gunicorn/app/wsgiapp.py): `gunicorn/app/wsgiapp.py`
- [Worker Lifecycle](https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/base.py): `gunicorn/workers/base.py`
- [Sync Worker](https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/sync.py): `gunicorn/workers/sync.py`
- [Async Workers](https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/base_async.py): `gunicorn/workers/base_async.py`
- [ASGI Worker](https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/gasgi.py): `gunicorn/workers/gasgi.py`

### Dirty Worker Pool

- [Dirty Arbiter](https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/arbiter.py): `gunicorn/dirty/arbiter.py`
- [Dirty Worker](https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/worker.py): `gunicorn/dirty/worker.py`
- [Persistent App](https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/app.py): `gunicorn/dirty/app.py`
- [Dirty Client](https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/client.py): `gunicorn/dirty/client.py`
- [Dirty Protocol](https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/protocol.py): `gunicorn/dirty/protocol.py`
- [Dirty Stash](https://github.com/benoitc/gunicorn/blob/master/gunicorn/dirty/stash.py): `gunicorn/dirty/stash.py`

### Control and Operations

- [Control Socket](https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/server.py): `gunicorn/ctl/server.py`
- [Command Handlers](https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/handlers.py): `gunicorn/ctl/handlers.py`
- [Control Client](https://github.com/benoitc/gunicorn/blob/master/gunicorn/ctl/client.py): `gunicorn/ctl/client.py`
- [StatsD Metrics](https://github.com/benoitc/gunicorn/blob/master/gunicorn/instrument/statsd.py): `gunicorn/instrument/statsd.py`

### Other components

- **Operator**
- **HTTP Client**
- **nginx**

## Connections

- Operator → CLI: starts server
- CLI → App Loading: loads app
- App Loading → Configuration: uses settings (evidence: `gunicorn/app/base.py`)
- App Loading → Master Arbiter: starts arbiter (evidence: `gunicorn/app/base.py`)
- Master Arbiter → Configuration: reads settings (evidence: `gunicorn/arbiter.py`)
- Master Arbiter → Sockets: creates listeners (evidence: `gunicorn/arbiter.py`)
- Master Arbiter → Worker Lifecycle: manages workers
- HTTP Client → Sockets: connects
- Sockets → Sync Worker: accepts requests
- Sync Worker → HTTP Parser: parses requests
- HTTP Parser → HTTP/1 Parsing: constructs messages (evidence: `gunicorn/http/parser.py`)
- HTTP/1 Parsing → Request Bodies: reads bodies (evidence: `gunicorn/http/message.py`)
- Sync Worker → WSGI / ASGI App: invokes app
- Async Workers → WSGI / ASGI App: serves app
- ASGI Worker → ASGI Protocol: runs protocol
- ASGI Protocol → WSGI / ASGI App: dispatches requests
- Async Workers → HTTP/2 Negotiation: negotiates protocol
- HTTP/2 Negotiation → HTTP/2 Connections: selects connection
- HTTP/2 Connections → HTTP/2 Streams: manages streams (evidence: `gunicorn/http2/async_connection.py`)
- nginx → uWSGI Protocol: uses binary protocol
- Master Arbiter → Dirty Arbiter: starts dirty pool
- Dirty Arbiter → Dirty Worker: manages workers
- Dirty Worker → Persistent App: runs persistent app (evidence: `gunicorn/dirty/worker.py`)
- Dirty Client → Dirty Protocol: encodes requests (evidence: `gunicorn/dirty/client.py`)
- Dirty Worker → Dirty Protocol: handles messages (evidence: `gunicorn/dirty/worker.py`)
- Dirty Client → Dirty Stash: uses stash (evidence: `gunicorn/dirty/client.py`)
- Master Arbiter → Control Socket: hosts control socket (evidence: `gunicorn/arbiter.py`)
- Control Socket → Command Handlers: dispatches commands (evidence: `gunicorn/ctl/server.py`)
- Control Client → Control Socket: sends commands
- StatsD Metrics → Control Socket: reports metrics
