From e73f44cf8bd1f4904ca5ca6c4aca7bfa9339d416 Mon Sep 17 00:00:00 2001 From: Tam Nguyen Duc <1218621+tamnd@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:41:22 +0700 Subject: [PATCH] net/http: port ClientConn and Transport.NewClientConn Port Go 1.27's clientconn.go: Transport.NewClientConn dials one HTTP/1 or HTTP/2 connection the caller manages, with Available, InFlight, Reserve, Release, Err, Close and a state hook. The HTTP/1 side is a persistConn with the clientConn flag, the HTTP/2 side is Go's NetHTTPClientConn, and both sit behind one small vtable. The tests are clientconn_test.go's six, in h1 and h2 modes. --- docs/cheatsheet.md | 13 +- include/burrow/net/http.h | 115 ++++++ src/net/http2.h | 9 + src/net/http2_transport.c | 160 ++++++++- src/net/http_clientconn.c | 359 +++++++++++++++++++ src/net/http_internal.h | 42 +++ src/net/http_transport.c | 305 +++++++++++++++- tests/http_clientconn_test.c | 656 +++++++++++++++++++++++++++++++++++ tools/burrow-coverage | 2 + 9 files changed, 1651 insertions(+), 10 deletions(-) create mode 100644 src/net/http_clientconn.c create mode 100644 tests/http_clientconn_test.c diff --git a/docs/cheatsheet.md b/docs/cheatsheet.md index e70b60d5..888cd80f 100644 --- a/docs/cheatsheet.md +++ b/docs/cheatsheet.md @@ -1,6 +1,6 @@ # Cheatsheet -Every function, function-like macro and variable in burrow's public headers, 7280 of them, one line each, grouped by header. Each line is the declaration, without its lifetime annotations, and a sentence about it. Where the function or variable has a Go original, the sentence is the first one of Go's doc comment for it. Where it has none, the sentence comes from the comment above it in the header. +Every function, function-like macro and variable in burrow's public headers, 7291 of them, one line each, grouped by header. Each line is the declaration, without its lifetime annotations, and a sentence about it. Where the function or variable has a Go original, the sentence is the first one of Go's doc comment for it. Where it has none, the sentence comes from the comment above it in the header. This page is generated by `tools/burrow-cheatsheet` from the headers and from Go's sources at go1.27.3, and CI fails if it falls behind them, so edit those and run the tool rather than editing this. @@ -5027,6 +5027,17 @@ The sentences taken from Go are reproduced from the doc comments of Go's `net/ht - `Url *http_proxy_from_environment(Alloc *a, HttpRequest *req, Error *err)` – ProxyFromEnvironment returns the URL of the proxy to use for a given request, as indicated by the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof). - `HttpProxyFunc http_proxy_from_environment_func(void)` – http_proxy_from_environment as the HttpProxyFunc a transport takes. - `HttpProxyFunc http_proxy_url(const Url *u)` – ProxyURL returns a proxy function (for use in a Transport) that always returns the same URL. +- `HttpClientConn *http_transport_new_client_conn(HttpTransport *t, Context ctx, Str scheme, Str address, Error *err)` – NewClientConn creates a new client connection to the given address. +- `Error http_client_conn_close(HttpClientConn *cc)` – Close closes the connection. +- `Error http_client_conn_err(HttpClientConn *cc)` – Err reports any fatal connection errors. +- `HttpResponse *http_client_conn_round_trip(HttpClientConn *cc, HttpRequest *req, Error *err)` – RoundTrip implements the RoundTripper interface. +- `Int http_client_conn_available(HttpClientConn *cc)` – Available reports the number of requests that may be sent to the connection without blocking. +- `Int http_client_conn_in_flight(HttpClientConn *cc)` – InFlight reports the number of requests in flight, including reserved requests. +- `Error http_client_conn_reserve(HttpClientConn *cc)` – Reserve reserves a concurrency slot on the connection. +- `void http_client_conn_release(HttpClientConn *cc)` – Release releases an unused concurrency slot reserved by Reserve. +- `void http_client_conn_set_state_hook(HttpClientConn *cc, HttpClientConnStateHook f)` – SetStateHook arranges for f to be called when the state of the connection changes. +- `HttpRoundTripper http_client_conn_as_round_tripper(HttpClientConn *cc)` – Not in Go: cc as the HttpRoundTripper it implements, which lasts as long as cc. +- `void http_client_conn_free(HttpClientConn *cc)` – Not in Go: closes the connection and gives cc back, once its state hook is not running. - `extern const Error http_err_use_last_response` – ErrUseLastResponse can be returned by Client.CheckRedirect hooks to control how redirects are processed. - `extern const Error http_err_scheme_mismatch` – ErrSchemeMismatch is returned when a server returns an HTTP response to an HTTPS client. - `HttpResponse *http_client_do(HttpClient *c, HttpRequest *req, Error *err)` – Do sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client. diff --git a/include/burrow/net/http.h b/include/burrow/net/http.h index 94789f51..c4a2fd07 100644 --- a/include/burrow/net/http.h +++ b/include/burrow/net/http.h @@ -2045,6 +2045,121 @@ HttpProxyFunc http_proxy_from_environment_func(void); * it, for every request. */ HttpProxyFunc http_proxy_url(const Url *u); +/* --------------------------------------------------------------- ClientConn */ + +/* A ClientConn is a client connection to an HTTP server. + * + * Unlike a [Transport], a ClientConn represents a single connection. + * Most users should use a Transport rather than creating client connections + * directly. + * + * Not in Go: give it back with http_client_conn_free, before the transport it + * came from is freed, and never from its state hook. */ +typedef struct HttpClientConn HttpClientConn; + +/* The state hook http_client_conn_set_state_hook takes. */ +BURROW_FUNC(HttpClientConnStateHook, void, HttpClientConn *cc); + +/* NewClientConn creates a new client connection to the given address. + * + * If scheme is "http", the connection is unencrypted. + * If scheme is "https", the connection uses TLS. + * + * The protocol used for the new connection is determined by the scheme, + * Transport.Protocols configuration field, and protocols supported by the + * server. See Transport.Protocols for more details. + * + * If Transport.Proxy is set and indicates that a request sent to the given + * address should use a proxy, the new connection uses that proxy. + * + * NewClientConn always creates a new connection, + * even if the Transport has an existing cached connection to the given host. + * + * The new connection is not added to the Transport's connection cache, + * and will not be used by [Transport.RoundTrip]. + * It does not count against the MaxIdleConns and MaxConnsPerHost limits. + * + * The caller is responsible for closing the new connection. + * + * Not in Go: the error is in the caller's error_allocator. */ +BURROW_OWNS(ret) HttpClientConn *http_transport_new_client_conn(HttpTransport *t, + Context ctx, Str scheme, + Str address, + Error *err); + +/* Close closes the connection. + * Outstanding RoundTrip calls are interrupted. */ +BURROW_STATIC(ret) Error http_client_conn_close(HttpClientConn *cc); + +/* Err reports any fatal connection errors. + * It returns nil if the connection is usable. + * If it returns non-nil, the connection can no longer be used. */ +BURROW_BORROWS(ret, cc) Error http_client_conn_err(HttpClientConn *cc); + +/* RoundTrip implements the [RoundTripper] interface. + * + * The request is sent on the client connection, + * regardless of the URL being requested or any proxy settings. + * + * If the connection is at its concurrency limit, + * RoundTrip waits for the connection to become available + * before sending the request. + * + * Not in Go: the error is in the caller's error_allocator. */ +BURROW_OWNS(ret) HttpResponse * +http_client_conn_round_trip(HttpClientConn *cc, HttpRequest *req, Error *err); + +/* Available reports the number of requests that may be sent + * to the connection without blocking. + * It returns 0 if the connection is closed. */ +Int http_client_conn_available(HttpClientConn *cc); + +/* InFlight reports the number of requests in flight, + * including reserved requests. + * It returns 0 if the connection is closed. */ +Int http_client_conn_in_flight(HttpClientConn *cc); + +/* Reserve reserves a concurrency slot on the connection. + * If Reserve returns nil, one additional RoundTrip call may be made + * without waiting for an existing request to complete. + * + * The reserved concurrency slot is accounted as an in-flight request. + * A successful call to RoundTrip will decrement the Available count + * and increment the InFlight count. + * + * Each successful call to Reserve should be followed by exactly one call + * to RoundTrip or Release, which will consume or release the reservation. + * + * If the connection is closed or at its concurrency limit, + * Reserve returns an error. */ +BURROW_BORROWS(ret, cc) Error http_client_conn_reserve(HttpClientConn *cc); + +/* Release releases an unused concurrency slot reserved by Reserve. + * If there are no reserved concurrency slots, it has no effect. */ +void http_client_conn_release(HttpClientConn *cc); + +/* SetStateHook arranges for f to be called when the state of the connection + * changes. At most one call to f is made at a time. + * If the connection's state has changed since it was created, + * f is called immediately in a separate goroutine. + * f may be called synchronously from RoundTrip or Response.Body.Close. + * + * If SetStateHook is called multiple times, the new hook replaces the old one. + * If f is nil, no further calls will be made to f after SetStateHook returns. + * + * f is called when Available increases (more requests may be sent on the + * connection), InFlight decreases (existing requests complete), or Err begins + * returning non-nil (the connection is no longer usable). */ +void http_client_conn_set_state_hook(HttpClientConn *cc, HttpClientConnStateHook f); + +/* Not in Go: cc as the HttpRoundTripper it implements, which lasts as long + * as cc. */ +HttpRoundTripper http_client_conn_as_round_tripper(HttpClientConn *cc); + +/* Not in Go: closes the connection and gives cc back, once its state hook is + * not running. NULL is fine. */ +void http_client_conn_free(HttpClientConn *cc); + /* ------------------------------------------------------------------- Client */ /* http.ErrUseLastResponse, which check_redirect gives to stop following diff --git a/src/net/http2.h b/src/net/http2.h index 7f888e9d..614d6f8a 100644 --- a/src/net/http2.h +++ b/src/net/http2.h @@ -1288,6 +1288,15 @@ Int burrow__http2_client_conn_streams_active(Http2ClientConn *cc); void burrow__http2_client_conn_release(Http2ClientConn *cc); +/* NewClientConn: a connection on c for net/http's ClientConn, which takes c + * whatever happens, with one reference, the caller's. hook is the + * internalStateHook. Not in Go: done is called once whatever happens, when + * the connection is gone and calls hook no more. Its RoundTrip waits for a + * stream when the connection is at its concurrency limit. */ +BURROW_OWNS(ret) Http2ClientConn * +burrow__http2_transport_new_client_conn(Http2Transport *t2, NetConn c, Str key, + Func hook, Func done, Error *err); + /* IdleConnStrsForTesting: the address of each connection that can take a new * request, sorted, as a Slice of Str in a. */ BURROW_OWNS(ret) Slice burrow__http2_transport_idle_conn_strs(Http2Transport *t2, diff --git a/src/net/http2_transport.c b/src/net/http2_transport.c index b50a2367..46b418a9 100644 --- a/src/net/http2_transport.c +++ b/src/net/http2_transport.c @@ -813,6 +813,15 @@ struct burrow__Http2ClientConn { bool extended_connect_allowed; bool strict_max_concurrent_streams; bool rst_stream_pings_blocked; + + /* internalStateHook reports state changes back to the net/http.ClientConn. + * Note that this is different from the user state hook registered by + * net/http.ClientConn.SetStateHook: The internal hook calls ClientConn, + * which calls the user hook. */ + Func state_hook; + /* Not in Go: called once as the connection is freed, when it calls + * state_hook no more. */ + Func hook_done; }; /* clientStream. The flags are at the end. */ @@ -968,12 +977,19 @@ static void h2c_timer_stop(h2c_Conn *cc, TimeTimer *t) { h2c_unref(cc); } +/* maybeCallStateHook. */ +static void h2c_maybe_call_state_hook(h2c_Conn *cc) { + if (!BURROW_FUNC_IS_NIL(cc->state_hook)) + BURROW_CALLF0(cc->state_hook); +} + /* closeConn. Go also arms a timer to close the conn under a TLS conn, which * a plain conn does not have. */ static void h2c_close_conn(h2c_Conn *cc) { ArenaMark m = error_mark(); (void)cc->tconn.vt->closer.close(cc->tconn.data); error_release(m); + h2c_maybe_call_state_hook(cc); } /* stickyErrWriter.Write, with writeWithByteTimeout. */ @@ -1313,6 +1329,8 @@ static void h2c_read_loop(void *env); static void h2c_conn_free(h2c_Conn *cc) { Http2Transport *t = cc->t; Alloc *a = cc->a; + if (!BURROW_FUNC_IS_NIL(cc->hook_done)) + BURROW_CALLF0(cc->hook_done); time_timer_free(cc->idle_t); time_timer_free(cc->dead_t); time_timer_free(cc->read_idle_t); @@ -1351,18 +1369,24 @@ static void h2c_conn_free(h2c_Conn *cc) { /* newClientConn. Takes c whatever happens: when there is an error, c has * been closed and given back. The connection comes with one reference, the - * caller's. */ + * caller's. Not in Go: hook_done is called once whatever happens, when the + * connection is gone. */ static h2c_Conn *h2c_new_client_conn(Http2Transport *t, NetConn c, bool single_use, - Str key, Error *err) { + Str key, Func state_hook, Func hook_done, + Error *err) { Alloc *a = t->a; h2c_Conn *cc = (h2c_Conn *)mem_alloc(a, sizeof *cc, _Alignof(h2c_Conn)); if (cc == NULL) { burrow__http_transport_free_conn(t->t1, c); + if (!BURROW_FUNC_IS_NIL(hook_done)) + BURROW_CALLF0(hook_done); *err = burrow_err_out_of_memory; return NULL; } cc->t = t; cc->a = a; + cc->state_hook = state_hook; + cc->hook_done = hook_done; cc->refs.v = 1; cc->tconn = c; cc->conf = h2c_config_from_transport(t->t1); @@ -1603,7 +1627,8 @@ Error burrow__http2_transport_add_conn(Http2Transport *t, Str scheme, Str author ArenaMark m = error_mark(); Error e = BURROW_NO_ERROR; bool single_use = t->t1 != NULL && t->t1->disable_keep_alives; - h2c_Conn *cc = h2c_new_client_conn(t, c, single_use, addr, &e); + h2c_Conn *cc = h2c_new_client_conn(t, c, single_use, addr, (Func){NULL, NULL}, + (Func){NULL, NULL}, &e); bool added = false; sync_mutex_lock(&t->mu); if (cc == NULL) { @@ -2396,6 +2421,8 @@ static Error h2c_process_settings_no_write(h2c_Conn *cc, const Http2Frame *f) { static Error h2c_process_settings(h2c_Conn *cc, const Http2Frame *f) { sync_mutex_lock(&cc->wmu); Error err = h2c_process_settings_no_write(cc, f); + /* processSettingsNoWrite's deferred maybeCallStateHook. */ + h2c_maybe_call_state_hook(cc); if (BURROW_OK(err) && !burrow__http2_settings_frame_is_ack(f)) { ArenaMark m = error_mark(); (void)burrow__http2_framer_write_settings_ack(cc->fr); @@ -2626,6 +2653,7 @@ static Error h2c_process_ping(h2c_Conn *cc, const Http2Frame *f) { sync_cond_broadcast(&cc->cond); } sync_mutex_unlock(&cc->mu); + h2c_maybe_call_state_hook(cc); return BURROW_NO_ERROR; } sync_mutex_lock(&cc->wmu); @@ -3772,6 +3800,7 @@ static void h2c_cleanup_write_request(h2c_Stream *cs, Error err) { cs->trace = NULL; sync_mutex_unlock(&cs->tmu); chan_close(cs->donec); + h2c_maybe_call_state_hook(cc); } /* doRequest runs for the duration of the request lifetime. It sends the @@ -4471,6 +4500,131 @@ void burrow__http2_client_conn_release(Http2ClientConn *cc) { h2c_unref(cc); } +/* ------------------------------------------------------ NetHTTPClientConn */ + +BURROW_SENTINEL_ERROR(h2c_err_connection_closed, "connection closed"); +BURROW_SENTINEL_ERROR(h2c_err_connection_unavailable, "connection is unavailable"); + +/* canReserveLocked reports whether a net/http.ClientConn can reserve a slot on + * this conn. + * + * This follows slightly different rules than + * clientConnIdleState.canTakeNewRequest. We only permit reservations up to + * the conn's concurrency limit. This differs from ClientConn.ReserveNewRequest, + * which permits reservations past the limit when StrictMaxConcurrentStreams is + * set. */ +static bool h2c_can_reserve_locked(h2c_Conn *cc) { + if (h2c_current_request_count_locked(cc) >= (Int)cc->max_concurrent_streams) + return false; + if (!h2c_is_usable_locked(cc)) + return false; + return true; +} + +/* availableLocked reports the number of concurrency slots available. */ +static Int h2c_available_locked(h2c_Conn *cc) { + if (!h2c_can_take_new_request_locked(cc)) + return 0; + Int n = (Int)cc->max_concurrent_streams - h2c_current_request_count_locked(cc); + return n > 0 ? n : 0; +} + +static HttpResponse *h2c_nhcc_round_trip(void *self, HttpRequest *req, Error *err) { + return burrow__http2_client_conn_round_trip((h2c_Conn *)self, req, err); +} + +static Error h2c_nhcc_close(void *self) { + h2c_close_for_error((h2c_Conn *)self, burrow__http2_err_client_conn_force_closed); + return BURROW_NO_ERROR; +} + +static Error h2c_nhcc_err(void *self) { + h2c_Conn *cc = (h2c_Conn *)self; + sync_mutex_lock(&cc->mu); + bool closed = cc->closed; + sync_mutex_unlock(&cc->mu); + if (closed) + return h2c_err_connection_closed; + return BURROW_NO_ERROR; +} + +static Error h2c_nhcc_reserve(void *self) { + h2c_Conn *cc = (h2c_Conn *)self; + Error err = BURROW_NO_ERROR; + sync_mutex_lock(&cc->mu); + if (!h2c_can_reserve_locked(cc)) + err = h2c_err_connection_unavailable; + else + cc->streams_reserved++; + sync_mutex_unlock(&cc->mu); + h2c_maybe_call_state_hook(cc); + return err; +} + +static void h2c_nhcc_release(void *self) { + h2c_Conn *cc = (h2c_Conn *)self; + sync_mutex_lock(&cc->mu); + /* We don't complain if streamsReserved is 0. + * + * This is consistent with RoundTrip: both Release and RoundTrip will + * consume a reservation iff one exists. */ + if (cc->streams_reserved > 0) + cc->streams_reserved--; + sync_mutex_unlock(&cc->mu); + h2c_maybe_call_state_hook(cc); +} + +static Int h2c_nhcc_available(void *self) { + h2c_Conn *cc = (h2c_Conn *)self; + sync_mutex_lock(&cc->mu); + Int n = h2c_available_locked(cc); + sync_mutex_unlock(&cc->mu); + return n; +} + +static Int h2c_nhcc_in_flight(void *self) { + h2c_Conn *cc = (h2c_Conn *)self; + sync_mutex_lock(&cc->mu); + Int n = h2c_current_request_count_locked(cc); + sync_mutex_unlock(&cc->mu); + return n; +} + +static Error h2c_nhcc_ping(void *self, Context ctx) { + return h2c_ping((h2c_Conn *)self, ctx); +} + +/* Not in Go: closes the connection and lets go of the caller's reference. */ +static void h2c_nhcc_free(void *self) { + h2c_Conn *cc = (h2c_Conn *)self; + h2c_close_for_error(cc, burrow__http2_err_client_conn_force_closed); + h2c_unref(cc); +} + +const burrow__HttpGenericClientConnVT burrow__http2_client_conn_vt = { + h2c_nhcc_close, h2c_nhcc_err, h2c_nhcc_round_trip, + h2c_nhcc_reserve, h2c_nhcc_release, h2c_nhcc_available, + h2c_nhcc_in_flight, h2c_nhcc_ping, h2c_nhcc_free, +}; + +/* NewClientConn. */ +Http2ClientConn *burrow__http2_transport_new_client_conn(Http2Transport *t, NetConn c, + Str key, Func hook, Func done, + Error *err) { + bool single_use = t->t1 != NULL && t->t1->disable_keep_alives; + h2c_Conn *cc = h2c_new_client_conn(t, c, single_use, key, hook, done, err); + if (cc == NULL) + return NULL; + /* RoundTrip should block when the conn is at its concurrency limit, not + * return an error. Setting strictMaxConcurrentStreams enables this. */ + sync_mutex_lock(&cc->wmu); + sync_mutex_lock(&cc->mu); + cc->strict_max_concurrent_streams = true; + sync_mutex_unlock(&cc->mu); + sync_mutex_unlock(&cc->wmu); + return cc; +} + /* IdleConnStrsForTesting. */ Slice burrow__http2_transport_idle_conn_strs(Http2Transport *t, Alloc *a) { Slice ret = slice_make(a, TYPE_STRING, 0, 0); diff --git a/src/net/http_clientconn.c b/src/net/http_clientconn.c new file mode 100644 index 00000000..c092f5c4 --- /dev/null +++ b/src/net/http_clientconn.c @@ -0,0 +1,359 @@ +/* Derived from Go's src/net/http/clientconn.go. + * Go source: go1.27.3. + * + * Copyright 2025 The Go Authors. All rights reserved. + * Copyright 2026 The burrow Authors. All rights reserved. + * Use of this source code is governed by a BSD-style licence that can be found + * in the LICENSE file. */ + +#include "burrow/net/http.h" + +#include "http_internal.h" + +#include "burrow/context.h" +#include "burrow/core.h" +#include "burrow/error.h" +#include "burrow/fmt.h" +#include "burrow/func.h" +#include "burrow/mem.h" +#include "burrow/mem/arena.h" +#include "burrow/mem/heap.h" +#include "burrow/net.h" +#include "burrow/net/url.h" +#include "burrow/proc.h" +#include "burrow/sync.h" +#include "burrow/sync/atomic.h" + +#include +#include +#include + +BURROW_SENTINEL_ERROR(cc_err_nil_url, "http: nil Request.URL"); +BURROW_SENTINEL_ERROR(cc_err_nil_header, "http: nil Request.Header"); +BURROW_SENTINEL_ERROR(cc_err_no_host, "http: no Host in request URL"); +BURROW_SENTINEL_ERROR(cc_err_no_ping, "http: ClientConn does not support PING"); + +/* A ClientConn is a client connection to an HTTP server. + * + * Unlike a [Transport], a ClientConn represents a single connection. + * Most users should use a Transport rather than creating client connections + * directly. */ +struct HttpClientConn { + burrow__HttpGenericClientConn cc; /* set under state_hook_mu, then fixed */ + Alloc *a; + + SyncMutex state_hook_mu; + HttpClientConnStateHook user_state_hook; + bool state_hook_running; + Int last_available; + Int last_in_flight; + bool last_closed; + + /* Not in Go: one for the caller, one for the connection, which done gives + * back, and one for each goroutine that runs the hook. */ + SyncAtomicInt32 refs; + SyncCond hook_idle; /* broadcast when state_hook_running goes false */ + bool freed; /* under state_hook_mu */ + /* The hook the goroutine maybeRunStateHook starts is to run first. There + * is one such goroutine at most, as state_hook_running is set for it. */ + HttpClientConnStateHook loop_hook; +}; + +static void cc_unref(HttpClientConn *cc) { + if (sync_atomic_int32_add(&cc->refs, -1) == 0) + mem_free(cc->a, cc, sizeof *cc, _Alignof(HttpClientConn)); +} + +/* The connection's done: it calls the hook no more. */ +static void cc_conn_done(void *env) { + cc_unref((HttpClientConn *)env); +} + +/* shouldRunStateHook returns the user's state hook if we should call it, + * or nil if we don't need to call it at this time. */ +static HttpClientConnStateHook cc_should_run_state_hook(HttpClientConn *cc, + bool stop_running) { + HttpClientConnStateHook hook = {0}; + sync_mutex_lock(&cc->state_hook_mu); + if (cc->cc.vt == NULL) { + sync_mutex_unlock(&cc->state_hook_mu); + return hook; + } + if (stop_running) { + cc->state_hook_running = false; + sync_cond_broadcast(&cc->hook_idle); + } + if (BURROW_FUNC_IS_NIL(cc->user_state_hook) || cc->state_hook_running) { + sync_mutex_unlock(&cc->state_hook_mu); + return hook; + } + ArenaMark m = error_mark(); + Int available = cc->cc.vt->available(cc->cc.self); + Int in_flight = cc->cc.vt->in_flight(cc->cc.self); + bool closed = BURROW_FAILED(cc->cc.vt->err(cc->cc.self)); + error_release(m); + if (available > cc->last_available || in_flight < cc->last_in_flight || + closed != cc->last_closed) { + hook = cc->user_state_hook; + cc->state_hook_running = true; + } + cc->last_available = available; + cc->last_in_flight = in_flight; + cc->last_closed = closed; + sync_mutex_unlock(&cc->state_hook_mu); + return hook; +} + +/* The goroutine maybeRunStateHook starts, which holds a reference. */ +static void cc_run_state_hook_loop(void *env) { + HttpClientConn *cc = (HttpClientConn *)env; + HttpClientConnStateHook hook = cc->loop_hook; + while (!BURROW_FUNC_IS_NIL(hook)) { + BURROW_CALLF(hook, cc); + hook = cc_should_run_state_hook(cc, true); + } + cc_unref(cc); +} + +static void cc_maybe_run_state_hook(HttpClientConn *cc) { + HttpClientConnStateHook hook = cc_should_run_state_hook(cc, false); + if (BURROW_FUNC_IS_NIL(hook)) + return; + /* Run the hook synchronously. + * + * This means that if, for example, the user calls resp.Body.Close to + * finish a request, the Close call will synchronously run the hook, + * giving the hook the chance to return the ClientConn to a connection + * pool before the next request is made. */ + BURROW_CALLF(hook, cc); + /* The connection state may have changed while the hook was running, in + * which case we need to run it again. + * + * If we do need to run the hook again, do so in a new goroutine to avoid + * blocking the current goroutine indefinitely. */ + hook = cc_should_run_state_hook(cc, true); + if (!BURROW_FUNC_IS_NIL(hook)) { + cc->loop_hook = hook; + (void)sync_atomic_int32_add(&cc->refs, 1); + if (!go(BURROW_FN(Func, cc_run_state_hook_loop, cc))) { + /* Not in Go: with no goroutine to be had, the hook runs here. */ + cc_run_state_hook_loop(cc); + } + } +} + +/* The internal state hook the connection calls. */ +static void cc_internal_state_hook(void *env) { + cc_maybe_run_state_hook((HttpClientConn *)env); +} + +HttpClientConn *http_transport_new_client_conn(HttpTransport *t, Context ctx, + Str scheme, Str address, Error *err) { + if (!str_eq(scheme, BURROW_S("http")) && !str_eq(scheme, BURROW_S("https"))) { + BURROW_OUT(err, fmt_errorf_v("net/http: invalid scheme %q", scheme)); + return NULL; + } + + Error e = BURROW_NO_ERROR; + Str port = {0}; + Str host = net_split_host_port(address, &port, &e); + if (BURROW_FAILED(e)) { + BURROW_OUT(err, e); + return NULL; + } + if (port.len == 0) + port = str_eq(scheme, BURROW_S("http")) ? BURROW_S("80") : BURROW_S("443"); + + Alloc *a = t->a != NULL ? t->a : heap_allocator(); + Arena scratch; + arena_init(&scratch, a, 0); + Alloc *sa = arena_allocator(&scratch); + Url *proxy_url = NULL; + if (t->proxy.f != NULL) { + /* Transport.Proxy takes a *Request, so create a fake one to pass it. */ + Url u; + memset(&u, 0, sizeof u); + u.scheme = scheme; + u.host = host; + u.path = BURROW_S("/"); + HttpRequest req; + memset(&req, 0, sizeof req); + req.ctx = ctx; + req.method = BURROW_S("GET"); + req.url = &u; + req.proto = BURROW_S("HTTP/1.1"); + req.proto_major = 1; + req.proto_minor = 1; + req.header = http_header_make(sa); + req.body = http_no_body; + req.host = host; + if (req.header == NULL) { + e = burrow_err_out_of_memory; + } else { + proxy_url = BURROW_CALLF(t->proxy, &req, sa, &e); + if (BURROW_FAILED(e)) + e = error_retain(error_allocator(), e); + } + if (BURROW_FAILED(e)) { + arena_free(&scratch); + BURROW_OUT(err, e); + return NULL; + } + } + Str target_addr = net_join_host_port(sa, host, port); + HttpClientConn *cc = + target_addr.len == 0 + ? NULL + : (HttpClientConn *)mem_alloc(a, sizeof *cc, _Alignof(HttpClientConn)); + if (cc == NULL) { + arena_free(&scratch); + BURROW_OUT(err, burrow_err_out_of_memory); + return NULL; + } + memset(cc, 0, sizeof *cc); + cc->a = a; + cc->refs.v = 2; + cc->hook_idle = SYNC_COND(sync_mutex_locker(&cc->state_hook_mu)); + + /* The state hook is a bit tricky: + * The persistConn has a state hook which calls + * ClientConn.maybeRunStateHook, which in turn calls the user-provided + * state hook (if any). + * + * ClientConn.maybeRunStateHook handles debouncing hook calls for both + * HTTP/1 and HTTP/2. + * + * Since there's no need to change the persistConn's hook, we set it at + * creation time. */ + burrow__HttpGenericClientConn gc = {0}; + e = burrow__http_transport_dial_client_conn( + t, ctx, scheme, target_addr, proxy_url, + BURROW_FN(Func, cc_internal_state_hook, cc), BURROW_FN(Func, cc_conn_done, cc), + &gc); + arena_free(&scratch); + if (BURROW_FAILED(e)) { + cc_unref(cc); + BURROW_OUT(err, e); + return NULL; + } + + /* Note that cc.maybeRunStateHook may have been called + * in the short window between dialConn and now. + * This is fine. */ + sync_mutex_lock(&cc->state_hook_mu); + cc->cc = gc; + ArenaMark m = error_mark(); + cc->last_available = gc.vt->available(gc.self); + error_release(m); + sync_mutex_unlock(&cc->state_hook_mu); + BURROW_OUT(err, BURROW_NO_ERROR); + return cc; +} + +Error http_client_conn_close(HttpClientConn *cc) { + Error e = cc->cc.vt->close(cc->cc.self); + cc_maybe_run_state_hook(cc); + return e; +} + +Error http_client_conn_err(HttpClientConn *cc) { + return cc->cc.vt->err(cc->cc.self); +} + +static Error cc_validate_client_conn_request(const HttpRequest *req) { + if (req->url == NULL) + return cc_err_nil_url; + if (req->header == NULL) + return cc_err_nil_header; + /* Validate the outgoing headers. */ + Str bad = burrow__http_validate_headers(error_allocator(), req->header); + if (bad.len > 0) + return fmt_errorf_v("http: invalid header %s", bad); + /* Validate the outgoing trailers too. */ + bad = burrow__http_validate_headers(error_allocator(), req->trailer); + if (bad.len > 0) + return fmt_errorf_v("http: invalid trailer %s", bad); + if (req->method.len != 0 && !burrow__http_valid_method(req->method)) + return fmt_errorf_v("http: invalid method %q", req->method); + if (req->url->host.len == 0) + return cc_err_no_host; + return BURROW_NO_ERROR; +} + +HttpResponse *http_client_conn_round_trip(HttpClientConn *cc, HttpRequest *req, + Error *err) { + if (req->url == NULL && str_eq(req->method, BURROW_S(":ping"))) { + /* Undocumented feature for sending a PING frame to a HTTP/2 + * connection, included to support x/net/http2.ClientConn.Ping. */ + Error e = cc_err_no_ping; + if (cc->cc.vt->ping != NULL) + e = cc->cc.vt->ping(cc->cc.self, http_request_context(req)); + cc_maybe_run_state_hook(cc); + BURROW_OUT(err, e); + return NULL; + } + Error e = cc_validate_client_conn_request(req); + if (BURROW_FAILED(e)) { + /* Not in Go: the body is closed, as a RoundTripper's is. */ + if (req->body.vt != NULL) + (void)req->body.vt->closer.close(req->body.data); + http_client_conn_release(cc); + cc_maybe_run_state_hook(cc); + BURROW_OUT(err, e); + return NULL; + } + HttpResponse *res = cc->cc.vt->round_trip(cc->cc.self, req, &e); + cc_maybe_run_state_hook(cc); + BURROW_OUT(err, e); + return res; +} + +Int http_client_conn_available(HttpClientConn *cc) { + return cc->cc.vt->available(cc->cc.self); +} + +Int http_client_conn_in_flight(HttpClientConn *cc) { + return cc->cc.vt->in_flight(cc->cc.self); +} + +Error http_client_conn_reserve(HttpClientConn *cc) { + Error e = cc->cc.vt->reserve(cc->cc.self); + cc_maybe_run_state_hook(cc); + return e; +} + +void http_client_conn_release(HttpClientConn *cc) { + cc->cc.vt->release(cc->cc.self); + cc_maybe_run_state_hook(cc); +} + +void http_client_conn_set_state_hook(HttpClientConn *cc, HttpClientConnStateHook f) { + sync_mutex_lock(&cc->state_hook_mu); + if (!cc->freed) + cc->user_state_hook = f; + sync_mutex_unlock(&cc->state_hook_mu); + cc_maybe_run_state_hook(cc); +} + +static HttpResponse *cc_rt_round_trip(void *self, HttpRequest *req, Error *err) { + return http_client_conn_round_trip((HttpClientConn *)self, req, err); +} + +static const HttpRoundTripperVT cc_round_tripper_vt = {NULL, cc_rt_round_trip}; + +HttpRoundTripper http_client_conn_as_round_tripper(HttpClientConn *cc) { + return (HttpRoundTripper){&cc_round_tripper_vt, cc}; +} + +void http_client_conn_free(HttpClientConn *cc) { + if (cc == NULL) + return; + sync_mutex_lock(&cc->state_hook_mu); + cc->freed = true; + cc->user_state_hook = (HttpClientConnStateHook){0}; + while (cc->state_hook_running) + sync_cond_wait(&cc->hook_idle); + sync_mutex_unlock(&cc->state_hook_mu); + cc->cc.vt->free(cc->cc.self); + cc_unref(cc); +} diff --git a/src/net/http_internal.h b/src/net/http_internal.h index 5e3bca32..34c0eaf1 100644 --- a/src/net/http_internal.h +++ b/src/net/http_internal.h @@ -560,6 +560,48 @@ Int burrow__http_transport_idle_conn_count_for_testing(HttpTransport *t, Str sch void burrow__http_transport_set_post_close_read_time_for_testing(HttpTransport *t, Duration d); +/* export_test.go's MaxPostCloseReadTime, how long a body closed early is + * drained for when the transport has no post_close_read_time of its own. */ +extern const Duration burrow__http_max_post_close_read_time; + +/* ---------------------------------------------------------------- ClientConn + * + * genericClientConn, the connection under a ClientConn: an HTTP/1 one the + * transport made, or an HTTP/2 one the HTTP/2 transport made. Not in Go: ping + * is NULL for a connection with no Ping, and free closes the connection and + * gives back the ClientConn's hold on it. */ +typedef struct burrow__HttpGenericClientConnVT { + Error (*close)(void *self); + Error (*err)(void *self); + HttpResponse *(*round_trip)(void *self, HttpRequest *req, Error *err); + Error (*reserve)(void *self); + void (*release)(void *self); + Int (*available)(void *self); + Int (*in_flight)(void *self); + Error (*ping)(void *self, Context ctx); + void (*free)(void *self); +} burrow__HttpGenericClientConnVT; + +typedef struct burrow__HttpGenericClientConn { + const burrow__HttpGenericClientConnVT *vt; + void *self; +} burrow__HttpGenericClientConn; + +/* dialConn with isClientConn set: a new connection to target_addr by + * target_scheme, through proxy_url when it is not NULL, which goes in no pool. + * hook is the internal state hook, which the connection calls as Go's does. + * Not in Go: done is called once, when the connection is gone and calls hook + * no more, or before this returns when there is no connection to be had. The + * error is in the caller's error_allocator. */ +/* The genericClientConn of the HTTP/2 NetHTTPClientConn, whose self is an + * Http2ClientConn. */ +extern const burrow__HttpGenericClientConnVT burrow__http2_client_conn_vt; + +Error burrow__http_transport_dial_client_conn(HttpTransport *t, Context ctx, + Str target_scheme, Str target_addr, + Url *proxy_url, Func hook, Func done, + burrow__HttpGenericClientConn *gc); + /* The pieces of the server the HTTP/2 server shares: serverHandler.ServeHTTP, * doKeepAlives, and the header value limit. */ void burrow__http_server_handler_serve(HttpServer *srv, HttpResponseWriter w, diff --git a/src/net/http_transport.c b/src/net/http_transport.c index 16310058..341ea52a 100644 --- a/src/net/http_transport.c +++ b/src/net/http_transport.c @@ -123,6 +123,14 @@ BURROW_SENTINEL_ERROR(tp_err_unencrypted_h2, "http: Transport does not support unencrypted HTTP/2"); BURROW_SENTINEL_ERROR(tp_err_socks5, "net/http: SOCKS5 proxies are not supported yet"); BURROW_SENTINEL_ERROR(tp_err_stub_round_tripper, "unexpected use of stub RoundTripper"); +BURROW_SENTINEL_ERROR(tp_err_h2_no_client_conn, + "http: HTTP/2 implementation does not support NewClientConn " + "(update golang.org/x/net?)"); +BURROW_SENTINEL_ERROR( + tp_err_not_client_conn, + "http: NewClientConn returned something that is not a ClientConn"); +BURROW_SENTINEL_ERROR(tp_err_client_conn_closed, "ClientConn closed"); +BURROW_SENTINEL_ERROR(tp_err_conn_unavailable, "connection is unavailable"); /* timeoutError, and errTimeout, which is one. A net.Error that is a timeout * and is context_deadline_exceeded to errors_is. */ @@ -553,6 +561,15 @@ struct burrow__HttpPConn { HttpRoundTripper alt_rt; /* with alt, from TLSNextProto, or none for t->h2 */ const TlsConnectionState *tls_state; /* NULL, or &tls_state_v */ TlsConnectionState tls_state_v; + + /* ClientConn only. */ + bool is_client_conn; /* whether this is a ClientConn (outside any pool) */ + Chan *availch; /* bool, holds a value when the conn is usable */ + bool reserved; /* under mu: concurrency slot reserved */ + bool in_flight; /* under mu: request is in flight */ + Func state_hook; /* ClientConn state hook */ + Func hook_done; /* Not in Go: called as pc is freed */ + void *alt_cc; /* Not in Go: the Http2ClientConn alt is, for a dial */ }; /* transportRequest and requestAndChan in one: a single try at sending a request @@ -804,6 +821,8 @@ void burrow__http_transport_set_post_close_read_time_for_testing(HttpTransport * t->post_close_read_time = d; } +const Duration burrow__http_max_post_close_read_time = TP_MAX_POST_CLOSE_READ_TIME; + /* Takes b out and frees it once it has no connections or wants left. */ static void tp_idle_bucket_trim(HttpTransport *t, tp_IdleBucket *b) { if (b->n > 0 || tp_queue_len(&b->wait) > 0) @@ -979,10 +998,14 @@ static void tp_pc_free(tp_PConn *pc) { chan_free(pc->write_loop_done); chan_free(pc->read_loop_done); chan_free(pc->eofc); + chan_free(pc->availch); time_timer_free(pc->idle_timer); arena_free(&pc->arena); arena_free(&pc->err_arena); + Func done = pc->hook_done; mem_free(a, pc, sizeof *pc, _Alignof(tp_PConn)); + if (!BURROW_FUNC_IS_NIL(done)) + BURROW_CALLF0(done); sync_wait_group_done(&t->live); } @@ -1174,8 +1197,10 @@ static bool tp_remove_idle_locked(HttpTransport *t, tp_PConn *pc) { return removed; } -/* removeIdleConn. */ +/* removeIdleConn marks pconn as dead. */ static bool tp_remove_idle(HttpTransport *t, tp_PConn *pc) { + if (pc->is_client_conn) + return true; sync_mutex_lock(&t->idle_mu); bool removed = tp_remove_idle_locked(t, pc); sync_mutex_unlock(&t->idle_mu); @@ -1260,6 +1285,19 @@ static Error tp_try_put_idle_conn(HttpTransport *t, tp_PConn *pc) { if (tp_pc_is_broken(pc)) return burrow__http_err_conn_broken; tp_pc_mark_reused(pc); + if (pc->is_client_conn) { + /* internalStateHook is always set for conns created by NewClientConn. */ + sync_mutex_lock(&pc->mu); + if (!pc->in_flight) + panic_str(BURROW_S("pconn is not in flight")); + pc->in_flight = false; + bool v = true; + if (!chan_try_send(pc->availch, &v)) + panic_str(BURROW_S("unable to make pconn available")); + sync_mutex_unlock(&pc->mu); + BURROW_CALLF0(pc->state_hook); + return BURROW_NO_ERROR; + } sync_mutex_lock(&t->idle_mu); Error err = tp_try_put_idle_conn_locked(t, pc); sync_mutex_unlock(&t->idle_mu); @@ -1491,8 +1529,15 @@ static void tp_want_detach_trace(tp_Want *w) { /* ---------------------------------------------------------------- dialing */ +/* What dialConn has for a ClientConn: its internalStateHook, and the done + * burrow__http_transport_dial_client_conn takes with it. */ +typedef struct tp_ClientConnDial { + Func hook; + Func done; +} tp_ClientConnDial; + static tp_PConn *tp_dial_conn(HttpTransport *t, Context ctx, const tp_ConnectMethod *cm, - tp_Want *w, Error *err); + tp_Want *w, const tp_ClientConnDial *cd, Error *err); static void tp_dec_conns_per_host_locked(HttpTransport *t, Str key); /* dialConnFor. Dials for w, which may have been canceled or given an idle @@ -1507,7 +1552,7 @@ static void tp_dial_conn_for(HttpTransport *t, tp_Want *w) { return; } Error err = BURROW_NO_ERROR; - tp_PConn *pc = tp_dial_conn(t, w->ctx, &w->cm, w, &err); + tp_PConn *pc = tp_dial_conn(t, w->ctx, &w->cm, w, NULL, &err); bool delivered = tp_want_try_deliver(w, pc, err, (Time){0}); if (pc != NULL) { /* Nobody wanted it, or it is HTTP/2 and can be shared, so it goes in @@ -1938,12 +1983,16 @@ static Http2Transport *tp_h2_transport(HttpTransport *t) { /* The TLS hooks of the request's trace, while the dial still has one. */ static void tp_trace_tls_handshake_start(tp_Want *w) { + if (w == NULL) + return; /* Not in Go: a ClientConn's dial has no trace. */ sync_mutex_lock(&w->trace_mu); httptrace_client_trace_tls_handshake_start(w->trace_to); sync_mutex_unlock(&w->trace_mu); } static void tp_trace_tls_handshake_done(tp_Want *w, TlsConnectionState cs, Error err) { + if (w == NULL) + return; sync_mutex_lock(&w->trace_mu); httptrace_client_trace_tls_handshake_done(w->trace_to, cs, err); sync_mutex_unlock(&w->trace_mu); @@ -2120,21 +2169,36 @@ static Error tp_add_tls(tp_PConn *pc, Context ctx, Str name, bool only_h1, tp_Wa /* dialConn. A connection for cm with its loops going, and a reference for the * caller, or one that is another round tripper's, with alt set. w is the want - * it is for, whose trace gets the TLS events. */ + * it is for, whose trace gets the TLS events, and NULL for a ClientConn, which + * has cd. Its done is called once whatever happens: by the connection as it + * is freed, or here when there is none. An HTTP/2 ClientConn is alt_cc, and + * pc no more than what it came in. */ static tp_PConn *tp_dial_conn(HttpTransport *t, Context ctx, const tp_ConnectMethod *cm, - tp_Want *w, Error *err) { + tp_Want *w, const tp_ClientConnDial *cd, Error *err) { Alloc *a = tp_alloc(t); if (cm->proxy_url != NULL && (str_eq(cm->proxy_url->scheme, BURROW_S("socks5")) || str_eq(cm->proxy_url->scheme, BURROW_S("socks5h")))) { + if (cd != NULL) + BURROW_CALLF0(cd->done); BURROW_OUT(err, tp_err_socks5); return NULL; } tp_PConn *pc = (tp_PConn *)mem_alloc(a, sizeof *pc, _Alignof(tp_PConn)); if (pc == NULL) { + if (cd != NULL) + BURROW_CALLF0(cd->done); BURROW_OUT(err, burrow_err_out_of_memory); return NULL; } pc->t = t; + if (cd != NULL) { + /* A ClientConn's is not counted against MaxConnsPerHost. */ + pc->is_client_conn = true; + pc->uncounted = true; + pc->state_hook = cd->hook; + pc->hook_done = cd->done; + pc->availch = chan_make(a, TYPE_BOOL, 1); + } arena_init(&pc->arena, a, 0); arena_init(&pc->err_arena, a, 0); sync_atomic_int64_store(&pc->refs, 1); @@ -2152,7 +2216,8 @@ static tp_PConn *tp_dial_conn(HttpTransport *t, Context ctx, const tp_ConnectMet Str addr = tp_cm_addr(pa, cm); if (pc->key.len == 0 || addr.len == 0 || pc->reqch == NULL || pc->writech == NULL || pc->closech == NULL || pc->write_err_ch == NULL || - pc->write_loop_done == NULL || pc->read_loop_done == NULL || pc->eofc == NULL) { + pc->write_loop_done == NULL || pc->read_loop_done == NULL || pc->eofc == NULL || + (cd != NULL && pc->availch == NULL)) { e = burrow_err_out_of_memory; goto fail; } @@ -2227,6 +2292,24 @@ static tp_PConn *tp_dial_conn(HttpTransport *t, Context ctx, const tp_ConnectMet (pc->tls_state != NULL && str_eq(pc->tls_state->negotiated_protocol, BURROW_S("h2"))); Http2Transport *t2 = http2 ? tp_h2_transport(t) : NULL; + if (t2 != NULL && cd != NULL) { + /* The connection, and the done, go to the HTTP/2 transport. */ + NetConn c = pc->conn; + memset(&pc->conn, 0, sizeof pc->conn); + pc->hook_done = (Func){0}; + Http2ClientConn *cc = burrow__http2_transport_new_client_conn( + t2, c, cm->target_addr, cd->hook, cd->done, &e); + if (cc == NULL) + goto fail; + pc->alt = true; + pc->alt_cc = cc; + BURROW_OUT(err, BURROW_NO_ERROR); + return pc; + } + if (cd != NULL && http2) { + e = tp_err_h2_no_client_conn; + goto fail; + } if (t2 != NULL) { /* The connection goes to the HTTP/2 transport, and pc is no more than * its place in the pool. */ @@ -3089,6 +3172,8 @@ static void tp_read_loop(void *env) { chan_close(pc->eofc); tp_pc_close(pc, close_err); (void)tp_remove_idle(t, pc); + if (!BURROW_FUNC_IS_NIL(pc->state_hook)) + BURROW_CALLF0(pc->state_hook); sync_mutex_lock(&pc->mu); pc->rl_exited = true; sync_mutex_unlock(&pc->mu); @@ -3240,12 +3325,47 @@ static void tp_pc_take_back(tp_PConn *pc, tp_Trip *tr) { tp_trip_wl_release(tr); } +/* waitForAvailability. The error is pc's, or the context's. */ +static Error tp_pc_wait_for_availability(tp_PConn *pc, Context ctx) { + SelectCase sc[] = { + BURROW_RECV(pc->availch, NULL), + BURROW_RECV(pc->closech, NULL), + BURROW_RECV(context_done(ctx), NULL), + }; + switch (chan_select(sc, 3)) { + case 0: + return BURROW_NO_ERROR; + case 1: + return pc->closed; + default: + return context_err(ctx); + } +} + static HttpResponse *tp_pc_round_trip(tp_PConn *pc, tp_Trip *tr, Error *err) { HttpTransport *t = pc->t; HttpRequest *req = tr->req; Alloc *ta = arena_allocator(&tr->arena); bool ok = true; + if (pc->is_client_conn) { + sync_mutex_lock(&pc->mu); + if (!pc->reserved) { + sync_mutex_unlock(&pc->mu); + Error e = tp_pc_wait_for_availability(pc, tr->ctx); + if (BURROW_FAILED(e)) { + chan_close(tr->rl_done); + chan_close(tr->wl_done); + BURROW_OUT(err, e); + return NULL; + } + sync_mutex_lock(&pc->mu); + } + pc->reserved = false; + pc->in_flight = true; + sync_mutex_unlock(&pc->mu); + } + /* The headers and channels the trip needs are made before the read loop * is told to expect a response, so that there is nothing left to fail * once it has been. */ @@ -3955,6 +4075,179 @@ HttpRoundTripper http_transport_as_round_tripper(HttpTransport *t) { return rt; } +/* ------------------------------------------------------------ ClientConn */ + +/* http1ClientConn.RoundTrip. As with Transport.RoundTrip, the response's + * on_free waits for both loops to be done with req. */ +static HttpResponse *tp_cc_round_trip(void *self, HttpRequest *req, Error *err) { + tp_PConn *pc = (tp_PConn *)self; + HttpTransport *t = pc->t; + Alloc *a = tp_alloc(t); + tp_Call *call = tp_call_new(t, req); + if (call == NULL || + (!tp_is_no_body(req->body) && !tp_call_track(call, req->body))) { + if (call != NULL) + tp_call_free(call); + tp_close_body(req); + BURROW_OUT(err, burrow_err_out_of_memory); + return NULL; + } + + /* Convert Request.Cancel into context cancellation. */ + call->ctx = context_with_cancel_cause(a, http_request_context(req), &call->cancel); + if (BURROW_CONTEXT_IS_NIL(call->ctx)) { + tp_close_body(&call->req); + tp_call_free(call); + BURROW_OUT(err, burrow_err_out_of_memory); + return NULL; + } + if (req->cancel != NULL) { + call->legacy_done = chan_make(a, TYPE_BOOL, 0); + if (call->legacy_done == NULL || + !go(BURROW_FN(Func, tp_await_legacy_cancel, call))) { + chan_free(call->legacy_done); + call->legacy_done = NULL; + tp_close_body(&call->req); + tp_call_free(call); + BURROW_OUT(err, burrow_err_out_of_memory); + return NULL; + } + } + + tp_Trip *tr = tp_trip_new(t, call, pc); + if (tr == NULL) { + tp_close_body(&call->req); + tp_call_free(call); + BURROW_OUT(err, burrow_err_out_of_memory); + return NULL; + } + Error e = BURROW_NO_ERROR; + HttpResponse *res = tp_pc_round_trip(pc, tr, &e); + chan_close(tr->gone); + if (res != NULL) { + call->trip = tr; + res->request = req; + res->on_free = BURROW_FN(Func, tp_call_on_free, call); + BURROW_OUT(err, BURROW_NO_ERROR); + return res; + } + + /* Not in Go: neither loop looks at the request after this, and the body + * is closed if nothing has closed it yet. */ + tp_wait(tr->rl_done); + tp_wait(tr->wl_done); + e = error_retain(error_allocator(), e); + tp_trip_unref(tr); + if (call->req.body.vt == &tp_track_vt && + !sync_atomic_bool_load(&((tp_Track *)call->req.body.data)->did_close)) + tp_close_body(&call->req); + tp_call_cancel(call, e); + tp_call_free(call); + BURROW_OUT(err, e); + return NULL; +} + +static Error tp_cc_close(void *self) { + tp_pc_close((tp_PConn *)self, tp_err_client_conn_closed); + return BURROW_NO_ERROR; +} + +static Error tp_cc_err(void *self) { + tp_PConn *pc = (tp_PConn *)self; + if (chan_try_recv(pc->closech, NULL, NULL)) + return pc->closed; + return BURROW_NO_ERROR; +} + +static Int tp_cc_available(void *self) { + tp_PConn *pc = (tp_PConn *)self; + sync_mutex_lock(&pc->mu); + Int n = BURROW_FAILED(pc->closed) || pc->reserved || pc->in_flight ? 0 : 1; + sync_mutex_unlock(&pc->mu); + return n; +} + +static Int tp_cc_in_flight(void *self) { + tp_PConn *pc = (tp_PConn *)self; + sync_mutex_lock(&pc->mu); + Int n = BURROW_OK(pc->closed) && (pc->reserved || pc->in_flight) ? 1 : 0; + sync_mutex_unlock(&pc->mu); + return n; +} + +static Error tp_cc_reserve(void *self) { + tp_PConn *pc = (tp_PConn *)self; + sync_mutex_lock(&pc->mu); + Error e = pc->closed; + if (BURROW_OK(e)) { + if (chan_try_recv(pc->availch, NULL, NULL)) + pc->reserved = true; + else + e = tp_err_conn_unavailable; + } + sync_mutex_unlock(&pc->mu); + return e; +} + +static void tp_cc_release(void *self) { + tp_PConn *pc = (tp_PConn *)self; + sync_mutex_lock(&pc->mu); + if (pc->reserved) { + bool v = true; + if (!chan_try_send(pc->availch, &v)) + panic_str(BURROW_S("cannot release reservation")); + pc->reserved = false; + } + sync_mutex_unlock(&pc->mu); +} + +/* Not in Go: closes pc and lets go of it. Its loops have the rest. */ +static void tp_cc_free(void *self) { + tp_PConn *pc = (tp_PConn *)self; + tp_pc_close(pc, tp_err_client_conn_closed); + tp_pc_unref(pc); +} + +static const burrow__HttpGenericClientConnVT tp_cc_vt = { + tp_cc_close, tp_cc_err, tp_cc_round_trip, + tp_cc_reserve, tp_cc_release, tp_cc_available, + tp_cc_in_flight, NULL, tp_cc_free, +}; + +Error burrow__http_transport_dial_client_conn(HttpTransport *t, Context ctx, + Str target_scheme, Str target_addr, + Url *proxy_url, Func hook, Func done, + burrow__HttpGenericClientConn *gc) { + tp_next_proto_defaults(t); + tp_ConnectMethod cm; + memset(&cm, 0, sizeof cm); + cm.target_scheme = target_scheme; + cm.target_addr = target_addr; + cm.proxy_url = proxy_url; + tp_ClientConnDial cd = {hook, done}; + Error e = BURROW_NO_ERROR; + tp_PConn *pc = tp_dial_conn(t, ctx, &cm, NULL, &cd, &e); + if (pc == NULL) + return e; + if (pc->alt) { + /* If pconn.alt is set, this is a connection implemented in another + * package. */ + void *cc = pc->alt_cc; + tp_pc_unref(pc); + if (cc == NULL) + return tp_err_not_client_conn; + gc->vt = &burrow__http2_client_conn_vt; + gc->self = cc; + return BURROW_NO_ERROR; + } + /* This is an HTTP/1 connection. */ + bool v = true; + (void)chan_try_send(pc->availch, &v); + gc->vt = &tp_cc_vt; + gc->self = pc; + return BURROW_NO_ERROR; +} + /* ---------------------------------------------------- the rest of Transport */ void http_transport_close_idle_connections(HttpTransport *t) { diff --git a/tests/http_clientconn_test.c b/tests/http_clientconn_test.c new file mode 100644 index 00000000..135d6cc2 --- /dev/null +++ b/tests/http_clientconn_test.c @@ -0,0 +1,656 @@ +/* Derived from Go's src/net/http/clientconn_test.go. + * Go source: go1.27.3. + * + * The tests run in Go's http1 and h2 modes, on the network in memory + * httptest_new_test_server has, which is Go's optFakeNet. + * + * Copyright 2025 The Go Authors. All rights reserved. + * Copyright 2026 The burrow Authors. All rights reserved. + * Use of this source code is governed by a BSD-style licence that can be found + * in the LICENSE file. */ + +#include "check.h" + +#include "../src/net/http_internal.h" + +#include "burrow/burrow.h" +#include "burrow/chan.h" +#include "burrow/context.h" +#include "burrow/error.h" +#include "burrow/fmt.h" +#include "burrow/io.h" +#include "burrow/mem/arena.h" +#include "burrow/mem/heap.h" +#include "burrow/net/http.h" +#include "burrow/net/http/httptest.h" +#include "burrow/proc.h" +#include "burrow/sync.h" +#include "burrow/sync/atomic.h" +#include "burrow/synctest.h" +#include "burrow/testing.h" +#include "burrow/time.h" + +#include +#include + +/* Not in Go: the mode a subtest runs in, of the two http3SkippedMode has. */ +typedef enum { HTTP1_MODE, HTTP2_MODE } TestMode; + +static const TestMode http3_skipped_mode[2] = {HTTP1_MODE, HTTP2_MODE}; + +static Str mode_scheme(TestMode mode) { + return mode == HTTP1_MODE ? BURROW_S("http") : BURROW_S("https"); +} + +/* Not in Go: the network in memory has no listener address, and a dial goes + * to the server whatever the address is. */ +static Str mode_addr(TestMode mode) { + return mode == HTTP1_MODE ? BURROW_S("example.tld:80") + : BURROW_S("example.tld:443"); +} + +/* What a test in one mode gets, made outside the bubble, since the server's + * cleanup runs after the function the bubble runs has returned. */ +typedef struct CCTest { + TestMode mode; + void (*fn)(struct CCTest *ct, TestingT *t); + bool sync; /* run in a bubble, as runSynctest does */ + const void *arg; /* the test's own */ + HttpHandlerFunc hf; + void (*handler)(struct CCTest *ct, HttpResponseWriter w, HttpRequest *r); + Chan *donec; /* for the handler to wait on */ + Chan *requestc; /* for the handler to wait on */ + SyncAtomicInt64 handler_calls; + SyncAtomicInt64 state_hook_calls; + HttpHTTP2Config h2; +} CCTest; + +static void cc_test_bubble(void *env, TestingT *t) { + CCTest *ct = (CCTest *)env; + ct->fn(ct, t); +} + +static void cc_test_mode(void *env, TestingT *t) { + CCTest *ct = (CCTest *)env; + if (ct->sync) + synctest_test(t, BURROW_FN(TestingTFunc, cc_test_bubble, ct)); + else + ct->fn(ct, t); +} + +/* run and runSynctest, for the modes in http3SkippedMode. */ +static void run_modes(TestingT *t, void (*fn)(CCTest *ct, TestingT *t), bool sync, + const void *arg) { + for (int i = 0; i < 2; i++) { + CCTest ct; + memset(&ct, 0, sizeof ct); + ct.mode = http3_skipped_mode[i]; + ct.fn = fn; + ct.sync = sync; + ct.arg = arg; + Str name = ct.mode == HTTP1_MODE ? BURROW_S("h1") : BURROW_S("h2"); + (void)testing_t_run(t, name, BURROW_FN(TestingTFunc, cc_test_mode, &ct)); + } +} + +static void serve_cc_test(void *env, HttpResponseWriter w, HttpRequest *r) { + CCTest *ct = (CCTest *)env; + if (ct->handler != NULL) + ct->handler(ct, w, r); +} + +/* defer close(donec), as a cleanup that runs before the ClientConn's and the + * server's, so that a test that stops early lets the handlers go. */ +static void close_donec(void *env) { + chan_close(((CCTest *)env)->donec); +} + +/* Not in Go: frees the channels once the server is gone. */ +static void free_chans(void *env) { + CCTest *ct = (CCTest *)env; + chan_free(ct->donec); + chan_free(ct->requestc); + ct->donec = NULL; + ct->requestc = NULL; +} + +static void free_client_conn(void *env) { + http_client_conn_free((HttpClientConn *)env); +} + +/* newClientServerTest with optFakeNet, and h2 for a test that sets + * Server.HTTP2.MaxConcurrentStreams. */ +static HttptestServer *new_client_server_test(CCTest *ct, TestingT *t, + Int max_streams) { + ct->hf = BURROW_FN(HttpHandlerFunc, serve_cc_test, ct); + testing_t_cleanup(t, BURROW_FN(Func, free_chans, ct)); + HttptestServer *ts = httptest_new_test_server( + NULL, testing_t_as_testing_tb(t), http_handler_func_as_handler(&ct->hf)); + if (max_streams > 0) { + ct->h2.max_concurrent_streams = max_streams; + ts->config.http2 = &ct->h2; + } + ts->enable_http2 = ct->mode == HTTP2_MODE; + (void)httptest_server_client(ts); + return ts; +} + +/* newClientConnTest. The ClientConn is freed, which closes it, in a cleanup + * that runs before the server's. */ +static HttpClientConn *new_client_conn_test(CCTest *ct, TestingT *t, Int max_streams, + HttptestServer **tsp) { + HttptestServer *ts = new_client_server_test(ct, t, max_streams); + Error err = BURROW_NO_ERROR; + HttpClientConn *cc = http_transport_new_client_conn( + &ts->transport, testing_t_context(t), mode_scheme(ct->mode), + mode_addr(ct->mode), &err); + if (cc == NULL) + testing_t_fatal_v(t, err); + testing_t_cleanup(t, BURROW_FN(Func, free_client_conn, cc)); + synctest_wait(); + if (tsp != NULL) + *tsp = ts; + return cc; +} + +/* Not in Go: a request in a, for a test that cannot fail to make one. */ +static HttpRequest *new_request(TestingT *t, Alloc *a, Context ctx, Str url) { + Error err = BURROW_NO_ERROR; + HttpRequest *req = http_new_request_with_context(a, ctx, BURROW_S("GET"), url, + (IoReader){0}, &err); + if (req == NULL) + testing_t_fatalf_v(t, "NewRequest: %v", err); + return req; +} + +static Str scheme_url(Alloc *a, TestMode mode, Str host) { + return fmt_sprintf_v(a, "%v://%v/", mode_scheme(mode), host); +} + +/* Not in Go: closes the body and frees the response. */ +static void close_response(HttpResponse *res) { + (void)res->body.vt->closer.close(res->body.data); + http_response_free(res); +} + +/* -------------------------------------------------------------- RoundTrip */ + +static void write_host(CCTest *ct, HttpResponseWriter w, HttpRequest *r) { + (void)ct; + (void)io_write_string(http_response_writer_as_io_writer(w), r->host, NULL); +} + +static void transport_new_client_conn_round_trip(CCTest *ct, TestingT *t) { + ct->handler = write_host; + HttptestServer *ts = new_client_server_test(ct, t, 0); + + Str scheme = mode_scheme(ct->mode); /* http or https */ + Error err = BURROW_NO_ERROR; + HttpClientConn *cc = http_transport_new_client_conn( + &ts->transport, testing_t_context(t), scheme, mode_addr(ct->mode), &err); + if (cc == NULL) + testing_t_fatal_v(t, err); + testing_t_cleanup(t, BURROW_FN(Func, free_client_conn, cc)); + + Arena ar; + arena_init(&ar, heap_allocator(), 0); + Alloc *a = arena_allocator(&ar); + + /* Send requests for a couple different domains. + * All use the same connection. */ + static const char *const hosts[2] = {"example.tld", "go.dev"}; + for (int i = 0; i < 2; i++) { + Str host = str_from_cstr(hosts[i]); + HttpRequest *req = + new_request(t, a, context_background(), scheme_url(a, ct->mode, host)); + HttpResponse *resp = http_client_conn_round_trip(cc, req, &err); + if (resp == NULL) { + arena_free(&ar); + testing_t_fatal_v(t, err); + } + IoReader body = {&resp->body.vt->reader, resp->body.data}; + Slice got = io_read_all(a, body, NULL); + Str gs = str_from_bytes(got.p, got.len); + if (!str_eq(gs, host)) + testing_t_errorf_v(t, "got response body %q, want %v", gs, host); + close_response(resp); + + /* CloseIdleConnections does not close connections created by + * NewClientConn. */ + http_transport_close_idle_connections(&ts->transport); + } + + err = http_client_conn_err(cc); + if (BURROW_FAILED(err)) + testing_t_errorf_v(t, "before close: ClientConn.Err() = %v, want nil", err); + + (void)http_client_conn_close(cc); + err = http_client_conn_err(cc); + if (BURROW_OK(err)) + testing_t_errorf_v(t, "after close: ClientConn.Err() = nil, want error"); + + HttpRequest *req = new_request(t, a, context_background(), + scheme_url(a, ct->mode, BURROW_S("example.tld"))); + HttpResponse *resp = http_client_conn_round_trip(cc, req, &err); + if (resp != NULL) { + close_response(resp); + testing_t_errorf_v(t, "after close: cc.RoundTrip succeeded, want error"); + } + testing_t_log_v(t, err); + arena_free(&ar); +} + +static void TestTransportNewClientConnRoundTrip(TestingT *t) { + run_modes(t, transport_new_client_conn_round_trip, false, NULL); +} + +/* ------------------------------------------------------------- ReserveAll */ + +/* TestClientConnReserveAll reserves every concurrency slot on a connection. */ +static void client_conn_reserve_all(CCTest *ct, TestingT *t) { + HttptestServer *ts = NULL; + HttpClientConn *cc = new_client_conn_test(ct, t, 3, &ts); + + Int want = 1; + if (ct->mode == HTTP2_MODE) + want = ts->config.http2->max_concurrent_streams; + Int available = http_client_conn_available(cc); + if (available != want) + testing_t_fatalf_v(t, "cc.Available() = %v, want %v", available, want); + + /* Reserve every available concurrency slot on the connection. */ + for (Int i = 0; i < available; i++) { + Error err = http_client_conn_reserve(cc); + if (BURROW_FAILED(err)) + testing_t_fatalf_v(t, "cc.Reserve() #%v = %v, want nil", i, err); + Int got = http_client_conn_available(cc), w = available - i - 1; + if (got != w) + testing_t_fatalf_v(t, "cc.Available() = %v, want %v", got, w); + got = http_client_conn_in_flight(cc); + w = i + 1; + if (got != w) + testing_t_fatalf_v(t, "cc.InFlight() = %v, want %v", got, w); + } + + /* The next reservation attempt should fail, since every slot is consumed. */ + if (BURROW_OK(http_client_conn_reserve(cc))) + testing_t_fatalf_v(t, "cc.Reserve() = nil, want error"); +} + +static void TestClientConnReserveAll(TestingT *t) { + run_modes(t, client_conn_reserve_all, true, NULL); +} + +/* -------------------------------------------------------- ReserveParallel */ + +typedef struct ReserveParallel { + HttpClientConn *cc; + SyncMutex mu; + Int success; + Int failure; +} ReserveParallel; + +static void reserve_parallel(void *env) { + ReserveParallel *rp = (ReserveParallel *)env; + Error err = http_client_conn_reserve(rp->cc); + sync_mutex_lock(&rp->mu); + if (BURROW_OK(err)) + rp->success++; + else + rp->failure++; + sync_mutex_unlock(&rp->mu); +} + +/* TestClientConnReserveParallel starts concurrent goroutines which reserve + * every concurrency slot on a connection. */ +static void client_conn_reserve_parallel(CCTest *ct, TestingT *t) { + HttpClientConn *cc = new_client_conn_test(ct, t, 3, NULL); + SyncWaitGroup wg; + memset(&wg, 0, sizeof wg); + ReserveParallel rp; + memset(&rp, 0, sizeof rp); + rp.cc = cc; + Int available = http_client_conn_available(cc); + const Int extra = 2; + for (Int i = 0; i < available + extra; i++) + sync_wait_group_go(&wg, BURROW_FN(Func, reserve_parallel, &rp)); + sync_wait_group_wait(&wg); + + if (rp.success != available) + testing_t_errorf_v(t, "%v successful reservations, want %v", rp.success, + available); + if (rp.failure != extra) + testing_t_errorf_v(t, "%v failed reservations, want %v", rp.failure, extra); +} + +static void TestClientConnReserveParallel(TestingT *t) { + run_modes(t, client_conn_reserve_parallel, true, NULL); +} + +/* --------------------------------------------------------- ReserveRelease */ + +/* TestClientConnReserveRelease repeatedly reserves and releases concurrency + * slots. */ +static void client_conn_reserve_release(CCTest *ct, TestingT *t) { + HttpClientConn *cc = new_client_conn_test(ct, t, 3, NULL); + + Int available = http_client_conn_available(cc); + for (Int i = 0; i < 2 * available; i++) { + Error err = http_client_conn_reserve(cc); + if (BURROW_FAILED(err)) + testing_t_fatalf_v(t, "cc.Reserve() #%v = %v, want nil", i, err); + http_client_conn_release(cc); + } + + Int got = http_client_conn_available(cc); + if (got != available) + testing_t_fatalf_v(t, "cc.Available() = %v, want %v", available, available); +} + +static void TestClientConnReserveRelease(TestingT *t) { + run_modes(t, client_conn_reserve_release, true, NULL); +} + +/* ----------------------------------------------------- ReserveAndConsume */ + +/* A request on a goroutine of its own, for the cancel case. */ +typedef struct CancelRT { + TestingT *t; + HttpClientConn *cc; + HttpRequest *req; +} CancelRT; + +static void cancel_rt(void *env) { + CancelRT *c = (CancelRT *)env; + Error err = BURROW_NO_ERROR; + HttpResponse *res = http_client_conn_round_trip(c->cc, c->req, &err); + if (res != NULL) { + close_response(res); /* Not in Go. */ + testing_t_errorf_v(c->t, "RoundTrip succeeded, want failure"); + } +} + +/* What a consume gets. Not in Go: a for the requests, and wg for a goroutine + * to be waited for. */ +typedef struct Consume { + TestingT *t; + HttpClientConn *cc; + TestMode mode; + Alloc *a; + SyncWaitGroup wg; + CancelRT crt; + Context ctx; + Func cancel; +} Consume; + +typedef struct ConsumeCase { + const char *name; + void (*consume)(Consume *c); + void (*handler)(CCTest *ct, HttpResponseWriter w, HttpRequest *r); + bool h1_closed; +} ConsumeCase; + +/* Explicit release. */ +static void consume_release(Consume *c) { + http_client_conn_release(c->cc); +} + +/* Invalid request sent to RoundTrip. */ +static void consume_invalid_field_name(Consume *c) { + HttpRequest *req = new_request(c->t, c->a, context_background(), + scheme_url(c->a, c->mode, BURROW_S("example.tld"))); + (void)http_header_set(req->header, BURROW_S("invalid field name"), BURROW_S("x")); + Error err = BURROW_NO_ERROR; + HttpResponse *res = http_client_conn_round_trip(c->cc, req, &err); + if (res != NULL) { + close_response(res); /* Not in Go. */ + testing_t_fatalf_v(c->t, "RoundTrip succeeded, want failure"); + } +} + +/* Successful request/response cycle. */ +static void consume_body_close(Consume *c) { + HttpRequest *req = new_request(c->t, c->a, context_background(), + scheme_url(c->a, c->mode, BURROW_S("example.tld"))); + Error err = BURROW_NO_ERROR; + HttpResponse *resp = http_client_conn_round_trip(c->cc, req, &err); + if (resp == NULL) + testing_t_fatalf_v(c->t, "RoundTrip: %v", err); + close_response(resp); +} + +/* Request context canceled before headers received. */ +static void consume_cancel(Consume *c) { + c->ctx = context_with_cancel(c->a, testing_t_context(c->t), &c->cancel); + c->crt.t = c->t; + c->crt.cc = c->cc; + c->crt.req = new_request(c->t, c->a, c->ctx, + scheme_url(c->a, c->mode, BURROW_S("example.tld"))); + sync_wait_group_go(&c->wg, BURROW_FN(Func, cancel_rt, &c->crt)); + synctest_wait(); + BURROW_CALLF0(c->cancel); +} + +static void handler_wait(CCTest *ct, HttpResponseWriter w, HttpRequest *r) { + (void)w; + (void)r; + (void)chan_recv(ct->donec, NULL); +} + +/* Response body closed before full response received. */ +static void consume_early_body_close(Consume *c) { + HttpRequest *req = new_request(c->t, c->a, context_background(), + scheme_url(c->a, c->mode, BURROW_S("example.tld"))); + Error err = BURROW_NO_ERROR; + HttpResponse *resp = http_client_conn_round_trip(c->cc, req, &err); + if (resp == NULL) + testing_t_fatalf_v(c->t, "RoundTrip: %v", err); + close_response(resp); +} + +static void handler_flush_wait(CCTest *ct, HttpResponseWriter w, HttpRequest *r) { + (void)r; + http_response_writer_write_header(w, 200); + HttpResponseController rc = http_new_response_controller(w); + (void)http_response_controller_flush(&rc); + (void)chan_recv(ct->donec, NULL); +} + +static void count_state_hook(void *env, HttpClientConn *cc) { + (void)cc; + (void)sync_atomic_int64_add((SyncAtomicInt64 *)env, 1); +} + +static void reserve_and_consume(CCTest *ct, TestingT *t) { + const ConsumeCase *test = (const ConsumeCase *)ct->arg; + ct->donec = chan_make(heap_allocator(), TYPE_BOOL, 0); + if (ct->donec == NULL) + testing_t_fatal_v(t, burrow_err_out_of_memory); + ct->handler = test->handler; + + HttpClientConn *cc = new_client_conn_test(ct, t, 0, NULL); + testing_t_cleanup(t, BURROW_FN(Func, close_donec, ct)); + /* Not in Go: an atomic, since the hook runs on other goroutines. */ + http_client_conn_set_state_hook( + cc, + BURROW_FN(HttpClientConnStateHook, count_state_hook, &ct->state_hook_calls)); + synctest_wait(); + sync_atomic_int64_store(&ct->state_hook_calls, + 0); /* ignore any initial update call */ + + Int avail = http_client_conn_available(cc); + Error err = http_client_conn_reserve(cc); + if (BURROW_FAILED(err)) + testing_t_fatalf_v(t, "cc.Reserve() = %v, want nil", err); + synctest_wait(); + int64_t calls = sync_atomic_int64_load(&ct->state_hook_calls); + if (calls != 0) + testing_t_errorf_v(t, "connection state hook calls: %v, want %v", calls, 0); + + Arena ar; + arena_init(&ar, heap_allocator(), 0); + Consume c; + memset(&c, 0, sizeof c); + c.t = t; + c.cc = cc; + c.mode = ct->mode; + c.a = arena_allocator(&ar); + test->consume(&c); + if (ct->mode == HTTP1_MODE) + time_sleep(burrow__http_max_post_close_read_time); + synctest_wait(); + + /* State hook should be called, either to report the + * connection availability increasing or the connection closing, + * or both. */ + calls = sync_atomic_int64_load(&ct->state_hook_calls); + if (calls == 0) + testing_t_errorf_v(t, "connection state hook calls: %v, want >1", calls); + + if (test->h1_closed && ct->mode == HTTP1_MODE) { + Int got = http_client_conn_available(cc); + if (got != 0) + testing_t_errorf_v(t, "cc.Available() = %v, want %v", got, 0); + got = http_client_conn_in_flight(cc); + if (got != 0) + testing_t_errorf_v(t, "cc.InFlight() = %v, want %v", got, 0); + if (BURROW_OK(http_client_conn_err(cc))) + testing_t_errorf_v(t, "cc.Err() = nil, want closed connection"); + } else { + Int got = http_client_conn_available(cc); + if (got != avail) + testing_t_errorf_v(t, "cc.Available() = %v, want %v", got, avail); + got = http_client_conn_in_flight(cc); + if (got != 0) + testing_t_errorf_v(t, "cc.InFlight() = %v, want %v", got, 0); + err = http_client_conn_err(cc); + if (BURROW_FAILED(err)) + testing_t_errorf_v(t, "cc.Err() = %v, want nil", err); + } + + if (http_client_conn_available(cc) > 0) { + err = http_client_conn_reserve(cc); + if (BURROW_FAILED(err)) + testing_t_errorf_v(t, "cc.Reserve() = %v, want success", err); + } + + /* Not in Go: the request's goroutine is done before its memory goes. */ + sync_wait_group_wait(&c.wg); + if (!BURROW_CONTEXT_IS_NIL(c.ctx)) + context_release(c.ctx); + arena_free(&ar); +} + +static void reserve_and_consume_case(void *env, TestingT *t) { + run_modes(t, reserve_and_consume, true, env); +} + +/* TestClientConnReserveAndConsume reserves a concurrency slot on a + * connection, and then verifies that various events consume the + * reservation. */ +static void TestClientConnReserveAndConsume(TestingT *t) { + static const ConsumeCase tests[] = { + {"release", consume_release, NULL, false}, + {"invalid field name", consume_invalid_field_name, NULL, false}, + {"body close", consume_body_close, NULL, false}, + /* An HTTP/1 connection is closed after a request is canceled on it. */ + {"cancel", consume_cancel, handler_wait, true}, + /* An HTTP/1 connection is closed after a request is canceled on it. */ + {"early body close", consume_early_body_close, handler_flush_wait, true}, + }; + for (size_t i = 0; i < sizeof tests / sizeof tests[0]; i++) + (void)testing_t_run(t, str_from_cstr(tests[i].name), + BURROW_FN(TestingTFunc, reserve_and_consume_case, + (void *)(uintptr_t)&tests[i])); +} + +/* --------------------------------------------------------- RoundTripBlocks */ + +static void handler_count_wait(CCTest *ct, HttpResponseWriter w, HttpRequest *r) { + (void)w; + (void)r; + (void)sync_atomic_int64_add(&ct->handler_calls, 1); + (void)chan_recv(ct->requestc, NULL); +} + +typedef struct BlockRT { + TestingT *t; + HttpClientConn *cc; + HttpRequest *req; + SyncAtomicInt64 *responses; +} BlockRT; + +static void block_rt(void *env) { + BlockRT *b = (BlockRT *)env; + Error err = BURROW_NO_ERROR; + HttpResponse *resp = http_client_conn_round_trip(b->cc, b->req, &err); + (void)sync_atomic_int64_add(b->responses, 1); + if (resp == NULL) { + testing_t_errorf_v(b->t, "RoundTrip: %v", err); + return; + } + close_response(resp); +} + +/* TestClientConnRoundTripBlocks verifies that RoundTrip blocks until a + * concurrency slot is available on a connection. */ +static void client_conn_round_trip_blocks(CCTest *ct, TestingT *t) { + ct->requestc = chan_make(heap_allocator(), TYPE_BOOL, 0); + if (ct->requestc == NULL) + testing_t_fatal_v(t, burrow_err_out_of_memory); + ct->handler = handler_count_wait; + HttpClientConn *cc = new_client_conn_test(ct, t, 3, NULL); + + Int available = http_client_conn_available(cc); + SyncAtomicInt64 responses; + memset(&responses, 0, sizeof responses); + enum { EXTRA = 2, MAX_RT = 3 + EXTRA }; + if (available + EXTRA > MAX_RT) + testing_t_fatalf_v(t, "cc.Available() = %v, want at most 3", available); + Arena ar; + arena_init(&ar, heap_allocator(), 0); + Alloc *a = arena_allocator(&ar); + SyncWaitGroup wg; + memset(&wg, 0, sizeof wg); + BlockRT rts[MAX_RT]; + for (Int i = 0; i < available + EXTRA; i++) { + rts[i].t = t; + rts[i].cc = cc; + rts[i].req = new_request(t, a, context_background(), + scheme_url(a, ct->mode, BURROW_S("example.tld"))); + rts[i].responses = &responses; + sync_wait_group_go(&wg, BURROW_FN(Func, block_rt, &rts[i])); + } + + synctest_wait(); + int64_t got = sync_atomic_int64_load(&ct->handler_calls); + if (got != available) + testing_t_errorf_v(t, "got %v handler calls, want %v", got, available); + got = sync_atomic_int64_load(&responses); + if (got != 0) + testing_t_errorf_v(t, "got %v responses, want %v", got, 0); + + for (Int i = 0; i < available + EXTRA; i++) { + bool v = true; + (void)chan_send(ct->requestc, &v); + synctest_wait(); + got = sync_atomic_int64_load(&responses); + if (got != i + 1) + testing_t_errorf_v(t, "got %v responses, want %v", got, i + 1); + } + sync_wait_group_wait(&wg); + arena_free(&ar); +} + +static void TestClientConnRoundTripBlocks(TestingT *t) { + run_modes(t, client_conn_round_trip_blocks, true, NULL); +} + +#define TESTS(X) \ + X(TestTransportNewClientConnRoundTrip) \ + X(TestClientConnReserveAll) \ + X(TestClientConnReserveParallel) \ + X(TestClientConnReserveRelease) \ + X(TestClientConnReserveAndConsume) \ + X(TestClientConnRoundTripBlocks) +TESTING_MAIN(TESTS) diff --git a/tools/burrow-coverage b/tools/burrow-coverage index c55ebb00..7079fecc 100755 --- a/tools/burrow-coverage +++ b/tools/burrow-coverage @@ -843,6 +843,8 @@ FUNC_TYPES = { "func(*testing.T)": {"TestingTFunc"}, "func(*B)": {"TestingBFunc"}, "func(*PB)": {"TestingPBFunc"}, + # burrow/net/http.h, for ClientConn.SetStateHook. + "func(*ClientConn)": {"HttpClientConnStateHook"}, } BUILTIN = {