|
| 1 | +# HTTP transport requirements and options |
| 2 | + |
| 3 | +## Why this document |
| 4 | + |
| 5 | +The HTTP client under `ext/http/client/curl` has accumulated a set of reports |
| 6 | +that no longer read as separate bugs. They cluster around a handful of places |
| 7 | +where two parts of the code hold different models of the same thing, and each |
| 8 | +individual fix has had to guess which model wins. |
| 9 | + |
| 10 | +Discussion #4448 asks the question that has to come before any rewrite: does the |
| 11 | +HTTP transport itself need to manage asynchronous concurrency, or is a simpler |
| 12 | +request and result model enough, with concurrency owned somewhere else? |
| 13 | + |
| 14 | +This document is written to answer that, not to propose class names. It sets out |
| 15 | +what the transport is used for today, compares three shapes it could take, and |
| 16 | +lists the decisions that have to be made before any of them can be built. It |
| 17 | +does not assume that `HttpOperation` has to go, that a `Session` carries one |
| 18 | +request, or where retry belongs. |
| 19 | + |
| 20 | +The evidence behind it is the invariant matrix in #4448, which maps each report |
| 21 | +to the invariant it broke and says which are measured and which are only |
| 22 | +reasoned. That is deliberately not repeated here. |
| 23 | + |
| 24 | +## What the transport is used for today |
| 25 | + |
| 26 | +Every in-tree consumer, read from `main`: |
| 27 | + |
| 28 | +| Consumer | Client | What it does | |
| 29 | +| --- | --- | --- | |
| 30 | +| Zipkin | `HttpClientSync` | Blocking by construction | |
| 31 | +| Elasticsearch, default | async curl client | `handler->waitForResponse()` before `Export` returns | |
| 32 | +| OTLP HTTP, default | async curl client | `Export(..., 0)` captures the result and waits | |
| 33 | +| OTLP HTTP, async preview | async curl client | non-zero budget, returns with sessions outstanding | |
| 34 | +| Elasticsearch, async preview | async curl client | returns with sessions outstanding | |
| 35 | +| A user supplied client | anything | callbacks may run inline or on another thread | |
| 36 | + |
| 37 | +`WITH_ASYNC_EXPORT_PREVIEW` is `OFF` by default. |
| 38 | + |
| 39 | +So in the configuration almost everyone builds, every exporter blocks until its |
| 40 | +request is done, and two of the three do it by handing the request to an |
| 41 | +asynchronous client and then waiting for it. The background thread, the callback |
| 42 | +machinery and the `CURLM` bookkeeping are all paid for and none of it is used |
| 43 | +for anything the caller can observe. |
| 44 | + |
| 45 | +That is the fact the rest of this document turns on. |
| 46 | + |
| 47 | +## Where the current model breaks |
| 48 | + |
| 49 | +Five things are modelled inconsistently, and most reports are a consequence of |
| 50 | +one of them. |
| 51 | + |
| 52 | +**Operation identity.** A `Session` holds one operation that can be replaced. |
| 53 | +The background thread recovers a `Session` from `CURLOPT_PRIVATE` and then asks |
| 54 | +it for its current operation, which is not necessarily the operation the |
| 55 | +completion belongs to. |
| 56 | + |
| 57 | +**Ownership across an asynchronous boundary.** The operation borrows the request |
| 58 | +headers, body and SSL options, the event handler as a raw pointer, and the |
| 59 | +session as a raw pointer. Whether those outlive the transfer is left to |
| 60 | +convention. |
| 61 | + |
| 62 | +**Publication.** The operation, its promise, its callback, its cancel route and |
| 63 | +its entry in the client's map become visible one at a time. User code can run |
| 64 | +and another thread can cancel while that is still happening. |
| 65 | + |
| 66 | +**Completion.** Progress and outcome are delivered through the same two |
| 67 | +callbacks, and nothing states which states are terminal, whether exactly one |
| 68 | +arrives, or who wins when a response and a cancel race. |
| 69 | + |
| 70 | +**Backend resources.** The easy handle, the header list, multi membership, the |
| 71 | +retry queue and the session registry are owned across three classes and two |
| 72 | +threads. |
| 73 | + |
| 74 | +Adding another mutex or atomic can only synchronise a model. It cannot decide |
| 75 | +what the model is, which is why the individual fixes keep needing a contract |
| 76 | +that does not exist yet. |
| 77 | + |
| 78 | +## Option A: keep `Session`, `EventHandler` and `HttpOperation` |
| 79 | + |
| 80 | +Repair the current shapes in place: publish atomically, define which states are |
| 81 | +terminal, give the operation an identity, and settle resource ownership. |
| 82 | + |
| 83 | +This is the smallest migration. Nothing installed changes, no consumer moves, |
| 84 | +and the existing tests keep their meaning. It also keeps the asynchronous |
| 85 | +capability that #1175 and #1243 were written for. |
| 86 | + |
| 87 | +What it does not do is remove the cost from the default configuration. A caller |
| 88 | +that blocks still pays for a background thread, a callback contract and a |
| 89 | +`CURLM`. It also leaves the abstraction shaped by one backend: `EventHandler` |
| 90 | +delivers curl's progress states, and a WinHTTP or `NSURLSession` implementation |
| 91 | +would have to invent or ignore them. |
| 92 | + |
| 93 | +## Option B: an implementation neutral request and result |
| 94 | + |
| 95 | +One attempt in, one result out. The transport is handed an immutable request and |
| 96 | +a deadline, and produces exactly one of a response, an invalid request, a |
| 97 | +transport error, a deadline, or a cancellation. |
| 98 | + |
| 99 | +This matches what every default consumer already does, and it makes the |
| 100 | +properties that keep breaking structural rather than conventional. Exactly one |
| 101 | +outcome is what the interface returns. Immutability removes the borrowed request |
| 102 | +lifetime. There is no progress callback to re-enter from. |
| 103 | + |
| 104 | +The cost is that it does not by itself serve the async preview or a native |
| 105 | +asynchronous backend. Something has to sit between a blocking interface and a |
| 106 | +platform that only offers completion callbacks, and where that adapter lives is |
| 107 | +a real design question rather than a detail. |
| 108 | + |
| 109 | +## Option C: a single owner `CURLM` event loop |
| 110 | + |
| 111 | +If transport level concurrency is wanted, this is the shape libcurl's own rules |
| 112 | +point at. One thread owns the multi handle and every easy handle attached to it. |
| 113 | +Other threads only enqueue submit, cancel and shutdown commands and wake it with |
| 114 | +`curl_multi_wakeup`. Every add, perform, remove and cleanup happens on the owner |
| 115 | +thread, and user completions run after leaving libcurl's callback stack and |
| 116 | +without holding any client lock. |
| 117 | + |
| 118 | +libcurl documents that one handle must not be used by two threads at once, that |
| 119 | +a transfer's easy handle stays attached until it is explicitly removed, and that |
| 120 | +removal has to come before cleanup. A single owner satisfies all three by |
| 121 | +construction rather than by review. |
| 122 | + |
| 123 | +This is a backend implementation choice. It does not have to appear in the |
| 124 | +public interface, and it should not: a native Windows or Apple transport has no |
| 125 | +multi handle to own. |
| 126 | + |
| 127 | +## Against the criteria in #4448 |
| 128 | + |
| 129 | +| | A: current model | B: request and result | C: single owner loop | |
| 130 | +| --- | --- | --- | --- | |
| 131 | +| Throughput, connection reuse | keeps both | reuse yes, parallelism needs a layer above | keeps both | |
| 132 | +| Cancellation | flag plus races | explicit, at a defined point | explicit, owner thread applies it | |
| 133 | +| Shutdown | four reports open | bounded by construction | bounded, one thread to drain | |
| 134 | +| Retry and concurrency owner | inside the transport | left open, above the transport | inside the backend | |
| 135 | +| Installed interfaces | unchanged | new interface plus an adapter | unchanged, backend only | |
| 136 | +| Custom clients | must be re-entrant, undocumented | one method to implement | unaffected | |
| 137 | +| Native backends | awkward | natural | not applicable | |
| 138 | + |
| 139 | +## What this needs decided |
| 140 | + |
| 141 | +These are the questions a design cannot avoid. They are listed as decisions |
| 142 | +rather than answered here. |
| 143 | + |
| 144 | +- [ ] Is a `Session` single use, reusable after completion, or a legacy adapter? |
| 145 | +- [ ] Does submitting a request snapshot it or take ownership of it? |
| 146 | +- [ ] Is completion one terminal result, with progress separated out as optional |
| 147 | + diagnostics? |
| 148 | +- [ ] On what thread do callbacks run, and which calls may a callback make? |
| 149 | +- [ ] Which layer owns retry, in-flight concurrency and `Retry-After`? |
| 150 | +- [ ] Is a new contract introduced alongside the current one with an adapter, or |
| 151 | + does the current interface evolve? |
| 152 | + |
| 153 | +The last one is not only an API question. CMake installs the abstract |
| 154 | +interfaces and `http_client_factory_curl.h` but not the concrete curl headers, |
| 155 | +while Bazel's `//ext:headers` still globs everything under `ext/include`. The |
| 156 | +two surfaces disagree today, and whichever direction is chosen has to say what |
| 157 | +happens to a Bazel consumer that includes a concrete header. |
| 158 | + |
| 159 | +## A recommendation |
| 160 | + |
| 161 | +This part is an opinion and is meant to be argued with. |
| 162 | + |
| 163 | +I would define the one attempt semantics of option B as the contract, keep the |
| 164 | +current interface working through an adapter rather than replacing it, and treat |
| 165 | +option C as the curl backend's implementation for the case where concurrency is |
| 166 | +actually wanted. Blocking and asynchronous callers then share one set of rules |
| 167 | +about ownership, deadlines and settlement, and differ only in how the result is |
| 168 | +presented. |
| 169 | + |
| 170 | +I would also want the concurrency question answered with a measurement rather |
| 171 | +than an assumption. The OTLP specification suggests sequential requests are |
| 172 | +reasonable against a local collector and that concurrency matters as round trip |
| 173 | +time grows. If sequential requests with connection reuse are enough for a local |
| 174 | +collector, the default configuration should not carry the machinery, and the |
| 175 | +async preview becomes the thing that opts into it. |
| 176 | + |
| 177 | +## On the C++ baseline |
| 178 | + |
| 179 | +The current baseline is C++14, and nothing above depends on changing it. |
| 180 | + |
| 181 | +C++17 would remove some of the awkwardness rather than change the design. |
| 182 | +`std::optional` and `std::variant` express a result that is exactly one of |
| 183 | +several things without a sentinel, and `std::string_view` removes some copies at |
| 184 | +the request boundary. C++20 coroutines would make an asynchronous adapter easier |
| 185 | +to write but would also make it the natural way to consume the transport, which |
| 186 | +is a larger commitment than this document is asking for. |
| 187 | + |
| 188 | +The recommendation is to design for C++14 and take the simplifications if and |
| 189 | +when the baseline moves, rather than to wait for it. |
0 commit comments