Host-owned Tcp connection pool with per-connection state (reuse database connections across requests)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend
Hướng nghiên cứu
Start by reading the host-managed subsystems design and the existing Sqlite.Db pool implementation; the issue does not name source files or tests. Work out how the host can own connection limits, lifecycle, exclusive leases, and opaque per-connection state while handlers access the pool through context. Done means the host can safely reuse Tcp connections across requests and discard connections after errors, with tests covering leasing and state persistence.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
Please add a small amount of host-owned state that lives longer than one request: a pool of reusable Tcp connections, each with a small state value attached. A database client written in pure Roc (for example a PostgreSQL wire-protocol client over Tcp) can then reuse connections between requests, as the built-in Sqlite.Db pool already does for SQLite.
This fits the "host-managed subsystems" design of the stateless architecture: the host owns the pool, its limits and its lifecycle. Handlers stay deterministic and receive the pool only through the context.
Why it is needed
Today a handler that talks to PostgreSQL must open a new TCP connection and log in on every request. There is nowhere to keep an open connection between requests:
- The context is fixed in
init!, and the platform has no shared mutable value, so a pool written in Roc cannot record which connection is free. - Sharing one connection between handlers is not safe.
StreamBusyguards one read or one write, not one exchange. Handler B's request can go between handler A's write and A's read, so A reads B's reply. B's statements also run inside A's transaction. - Keeping a list of connections in the context and trying the next one on
StreamBusyhas the same problem. A connection is free between two steps of a transaction, not only when no read or write runs.
Per-connection state is the second need. A database client must know facts about each connection that the immutable values in Roc cannot carry:
| State | What it is used for | Without it |
|---|---|---|
| Free or leased | A pool | One connection and one login per request |
| In a transaction or not | Refuse a statement that uses the outer connection inside a transaction (a programming error) | One extra round trip per statement to ask the server (a probe) |
| Prepared statement names | Parse a statement once per connection, not once per execution | The server parses and plans every statement again |
| Session settings | SET once per connection (time zone, search path) |
Send them again on each request |
Costs today
| Cost | Size |
|---|---|
| TCP connect and SCRAM-SHA-256 login per request | About 5 ms on a local server. More over a network, and more again with TLS |
| Server side | PostgreSQL starts a new backend process for each connection, so each request causes a process start on the database server |
| Concurrency | At most 64 streams per process, so at most 64 requests can use the database at the same time. The 65th gets CapacityExhausted |
| Busy-guard probe | One extra round trip per statement outside a transaction |
Workarounds
| Workaround | Limit |
|---|---|
| One connection per request (what works today) | The costs above |
| An external pooler (PgBouncer) on the same host | One more process to deploy and run. In transaction mode it drops session state, so prepared statements and SET do not survive between transactions |
| Keep one connection in the context | Not safe: two handlers mix their messages and transactions |
| Skip the busy guard | A wrong call inside a transaction is not caught |
None of these blocks an app, so this is a wanted feature, not a blocker.
A possible shape
## Made in init!, kept in the context. The host owns the connections and the limits.
Tcp.Pool.open! : { host : Str, port : U16, max : U64, idle_timeout_ms : U64 } => Try(Tcp.Pool, [..])
## Lease one connection for the block. The host returns it to the pool when the block ends,
## or closes it if the block returns Err or the stream failed.
## `state` is opaque to the host: the client stores what it needs between leases.
Tcp.Pool.with! : Tcp.Pool, ({ stream : Tcp.Stream, state : List(U8) } => Try({ state : List(U8), keep : Bool }, e)) => Try(a, [Pool(..), ..e])
stateas bytes keeps the host independent of the client. A PostgreSQL client could store the transaction status and its prepared statement names there.keep : Boollets the client drop a connection that is out of step, for example after a protocol error.- A connection is leased to one handler at a time, so messages and transactions cannot mix.
Alternative: a general host-owned cell
A more general version is a host-owned cell, made in init! with a fixed size limit and atomic update!. It would also cover other bounded caches. It is less in line with the stateless design than a pool, so the pool above is the preferred form.
Related
- #272: tell that the current request already holds a transaction on the pool (SQLite)
- #273: lease one pooled SQLite connection for pragmas plus a transaction
- With a TCP pool and a per-connection state like this, a client written in pure Roc gets the same guarantees for PostgreSQL.
- Ngôn ngữ chính
- HTML
- Star
- 110
- Fork
- 25
- Merge trung bình
- 18 giờ 28 phút
- Pull request đã merge (30 ngày)
- 16
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của roc-lang/basic-webserver
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
roc-lang/basic-webserver#277 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
roc-lang/basic-webserver#273 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
roc-lang/basic-webserver#272 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
roc-lang/basic-webserver#265 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 28/100
roc-lang/basic-webserver#231 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của roc-lang/basic-webserver
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
OHDSI/Data2Evidence#3815 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
e2e-failure ready-to-code
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
redhat-developer/rhdh-plugin-export-overlays#4995 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
fidelity
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: Azure SAS signing uses local time instead of UTC, shifting validity window on non-UTC hostsCó thể đã có người làm @huanghaoyuanhhy đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
zilliztech/milvus-backup#1418 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] download repeatedly fetches unchanged gzip responses due to compressed/decoded size mismatchCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
stanfordnlp/dspy#11000 ·
Maintainer thường phản hồi trong vòng 1 ngày