@mark1russell7/harness 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ # The Go HTTP client of the harness: the http.Transport of Go 1.27.2 at its defaults, with the
2
+ # settings of a cell over them. It runs as a container in the client's network namespace, as the
3
+ # harness's own TCP client does, and prints the same JSON lines. The program uses the standard
4
+ # library only, thus the digest of the builder pins all of its code.
5
+ # lab.ts stages the build context: this folder, under harness/image/go-http.
6
+ FROM golang:1.27.2-alpine@sha256:f92b6ef800e499660581efdabdf25d9d817a9d124eaf900924f0504e7e27e12d AS build
7
+ WORKDIR /src
8
+ COPY harness/image/go-http/go.mod harness/image/go-http/main.go ./
9
+ # the format and the vet of the source come first, as the cargo test of the harness image does
10
+ RUN test -z "$(gofmt -l .)" || { gofmt -d .; exit 1; }
11
+ RUN CGO_ENABLED=0 go vet ./... && CGO_ENABLED=0 go build -trimpath -o /client-go .
12
+
13
+ FROM alpine:3.22@sha256:5291449c3df73caf6ed85e649dec1b9e818b39a5d8c871e97afc13e9cd5e8fa8
14
+ COPY --from=build /client-go /opt/harness/client-go
@@ -0,0 +1,3 @@
1
+ module client-go
2
+
3
+ go 1.27
@@ -0,0 +1,324 @@
1
+ // Command client-go is the Go client of the HTTP cells: the http.Transport of Go 1.27.2 at its
2
+ // defaults, with each setting that a cell gives over them. Each event is a JSON line with `ev` and
3
+ // `t`, the CLOCK_MONOTONIC time in nanoseconds, as the TCP client of the harness writes them.
4
+ //
5
+ // client-go HOST PORT --mode idle|inflight|gap [--gap-ms N] [--method GET|POST] [--hold-ms N]
6
+ // [--bytes N] [--go PATH] [--max-s N] [--linger-ms N]
7
+ // [--idle-conn-timeout-ms N] [--max-idle-per-host N] [--dial-timeout-ms N]
8
+ // [--keepalive-ms N] [--keepalive-interval-ms N] [--keepalive-count N] [--timeout-ms N]
9
+ //
10
+ // The first request is GET /, and the end of its reply is the ready line. Then the client holds
11
+ // the connection idle (idle), sends the second request when the file --go appears (inflight), or
12
+ // sends it after the gap (gap).
13
+ //
14
+ // A wrapper on each connection writes what the read loop of the transport reads: an end of file,
15
+ // or an error. The trace of the transport writes which connection each request goes on, whether
16
+ // the transport reused it, and each new try of a request on a new connection.
17
+ package main
18
+
19
+ import (
20
+ "context"
21
+ "encoding/json"
22
+ "errors"
23
+ "flag"
24
+ "fmt"
25
+ "io"
26
+ "net"
27
+ "net/http"
28
+ "net/http/httptrace"
29
+ "os"
30
+ "runtime"
31
+ "strings"
32
+ "sync"
33
+ "syscall"
34
+ "time"
35
+ "unsafe"
36
+ )
37
+
38
+ var out sync.Mutex
39
+
40
+ // now gives the time on CLOCK_MONOTONIC in nanoseconds: the clock of the prober.
41
+ func now() int64 {
42
+ var ts syscall.Timespec
43
+ syscall.Syscall(syscall.SYS_CLOCK_GETTIME, 1, uintptr(unsafe.Pointer(&ts)), 0)
44
+ return ts.Nano()
45
+ }
46
+
47
+ // emitAt writes one line with the time t.
48
+ func emitAt(ev string, t int64, fields map[string]any) {
49
+ line := map[string]any{"ev": ev, "t": t}
50
+ for key, value := range fields {
51
+ line[key] = value
52
+ }
53
+ bytes, _ := json.Marshal(line)
54
+ out.Lock()
55
+ os.Stdout.Write(append(bytes, '\n'))
56
+ out.Unlock()
57
+ }
58
+
59
+ // emit writes one line with the time now.
60
+ func emit(ev string, fields map[string]any) { emitAt(ev, now(), fields) }
61
+
62
+ var names = map[syscall.Errno]string{
63
+ syscall.ECONNRESET: "ECONNRESET",
64
+ syscall.ECONNREFUSED: "ECONNREFUSED",
65
+ syscall.ETIMEDOUT: "ETIMEDOUT",
66
+ syscall.EHOSTUNREACH: "EHOSTUNREACH",
67
+ syscall.ENETUNREACH: "ENETUNREACH",
68
+ syscall.EPIPE: "EPIPE",
69
+ syscall.ECONNABORTED: "ECONNABORTED",
70
+ }
71
+
72
+ // errnoOf gives the name of the error of a socket, EOF for an end of file, or TIMEOUT for a
73
+ // deadline of Go.
74
+ func errnoOf(err error) string {
75
+ var errno syscall.Errno
76
+ if errors.As(err, &errno) {
77
+ if name, ok := names[errno]; ok {
78
+ return name
79
+ }
80
+ return fmt.Sprintf("E%d", int(errno))
81
+ }
82
+ if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
83
+ return "EOF"
84
+ }
85
+ var timeout interface{ Timeout() bool }
86
+ if errors.As(err, &timeout) && timeout.Timeout() {
87
+ return "TIMEOUT"
88
+ }
89
+ return "ERROR"
90
+ }
91
+
92
+ // traced is a connection that writes what its reads end with, and its close.
93
+ type traced struct {
94
+ net.Conn
95
+ local string
96
+ mu sync.Mutex
97
+ closed bool
98
+ }
99
+
100
+ func (c *traced) Read(p []byte) (int, error) {
101
+ n, err := c.Conn.Read(p)
102
+ if err != nil {
103
+ c.mu.Lock()
104
+ closed := c.closed
105
+ c.mu.Unlock()
106
+ // a read after the close of the client itself is not an event of the peer
107
+ if !closed {
108
+ if errors.Is(err, io.EOF) {
109
+ emit("eof", map[string]any{"local": c.local})
110
+ } else {
111
+ emit("conn-error", map[string]any{"local": c.local, "errno": errnoOf(err), "message": err.Error()})
112
+ }
113
+ }
114
+ }
115
+ return n, err
116
+ }
117
+
118
+ func (c *traced) Close() error {
119
+ c.mu.Lock()
120
+ first := !c.closed
121
+ c.closed = true
122
+ c.mu.Unlock()
123
+ if first {
124
+ emit("closed", map[string]any{"local": c.local})
125
+ }
126
+ return c.Conn.Close()
127
+ }
128
+
129
+ // dial gives the DialContext of the transport: the dialer, and a wrapper on each connection.
130
+ func dial(d *net.Dialer) func(ctx context.Context, network, addr string) (net.Conn, error) {
131
+ var mu sync.Mutex
132
+ count := 0
133
+ return func(ctx context.Context, network, addr string) (net.Conn, error) {
134
+ start := now()
135
+ conn, err := d.DialContext(ctx, network, addr)
136
+ if err != nil {
137
+ emit("dial-error", map[string]any{"errno": errnoOf(err), "message": err.Error(), "ns": now() - start})
138
+ return nil, err
139
+ }
140
+ mu.Lock()
141
+ count++
142
+ n := count
143
+ mu.Unlock()
144
+ local := conn.LocalAddr().String()
145
+ emit("conn", map[string]any{"n": n, "local": local})
146
+ if n == 1 {
147
+ emit("connected", map[string]any{"local": local})
148
+ }
149
+ return &traced{Conn: conn, local: local}, nil
150
+ }
151
+ }
152
+
153
+ // request sends request i and waits for its outcome. It tells whether a reply came.
154
+ func request(client *http.Client, base string, i int, method, path string) bool {
155
+ attempts := 0
156
+ trace := &httptrace.ClientTrace{
157
+ GotConn: func(info httptrace.GotConnInfo) {
158
+ attempts++
159
+ ev := "request"
160
+ if attempts > 1 {
161
+ ev = "retry"
162
+ }
163
+ emit(ev, map[string]any{
164
+ "i": i,
165
+ "local": info.Conn.LocalAddr().String(),
166
+ "reused": info.Reused,
167
+ "idle_ns": info.IdleTime.Nanoseconds(),
168
+ })
169
+ },
170
+ WroteRequest: func(info httptrace.WroteRequestInfo) {
171
+ fields := map[string]any{"i": i}
172
+ if info.Err != nil {
173
+ fields["errno"] = errnoOf(info.Err)
174
+ }
175
+ emit("wrote", fields)
176
+ },
177
+ GotFirstResponseByte: func() { emit("first-byte", map[string]any{"i": i}) },
178
+ }
179
+ var body io.Reader
180
+ if method == "POST" {
181
+ body = strings.NewReader("x")
182
+ }
183
+ req, err := http.NewRequestWithContext(httptrace.WithClientTrace(context.Background(), trace), method, base+path, body)
184
+ if err != nil {
185
+ emit("fail", map[string]any{"why": err.Error()})
186
+ os.Exit(1)
187
+ }
188
+ // the stamp comes before the call: the transport writes the request after it
189
+ at := now()
190
+ emitAt("sent", at, map[string]any{"i": i})
191
+ resp, err := client.Do(req)
192
+ if err != nil {
193
+ emit("error", map[string]any{"i": i, "errno": errnoOf(err), "message": err.Error()})
194
+ return false
195
+ }
196
+ emit("response", map[string]any{"i": i, "status": resp.StatusCode})
197
+ n, err := io.Copy(io.Discard, resp.Body)
198
+ resp.Body.Close()
199
+ if err != nil {
200
+ emit("error", map[string]any{"i": i, "errno": errnoOf(err), "message": err.Error()})
201
+ return false
202
+ }
203
+ if i == 1 {
204
+ emit("ready", map[string]any{"reply": resp.Status})
205
+ } else {
206
+ emit("data", map[string]any{"i": i, "n": n})
207
+ emit("replied", map[string]any{"i": i, "n": map[string]any{"value": n, "unit": "B"}})
208
+ }
209
+ return true
210
+ }
211
+
212
+ func milliseconds(ms float64) time.Duration { return time.Duration(ms * float64(time.Millisecond)) }
213
+
214
+ func main() {
215
+ usage := "client-go HOST PORT --mode idle|inflight|gap"
216
+ if len(os.Args) < 3 {
217
+ emit("fail", map[string]any{"why": usage})
218
+ os.Exit(1)
219
+ }
220
+ host, port := os.Args[1], os.Args[2]
221
+ flags := flag.NewFlagSet("client-go", flag.ExitOnError)
222
+ mode := flags.String("mode", "", "idle, inflight or gap")
223
+ gapMs := flags.Float64("gap-ms", 0, "the gap after the first reply, in gap mode")
224
+ method := flags.String("method", "GET", "the method of the second request")
225
+ holdMs := flags.Int("hold-ms", 0, "the second request asks the server to hold its reply")
226
+ size := flags.Int("bytes", 0, "the second request asks for a body of this length")
227
+ goPath := flags.String("go", "/tmp/go", "the file that starts the second request, in inflight mode")
228
+ maxS := flags.Float64("max-s", 60, "the deadline of the client")
229
+ lingerMs := flags.Float64("linger-ms", 500, "the wait after the second request, before the exit")
230
+ idleMs := flags.Float64("idle-conn-timeout-ms", 0, "Transport.IdleConnTimeout")
231
+ maxIdle := flags.Int("max-idle-per-host", 0, "Transport.MaxIdleConnsPerHost")
232
+ dialMs := flags.Float64("dial-timeout-ms", 0, "Dialer.Timeout")
233
+ keepAliveMs := flags.Float64("keepalive-ms", 0, "Dialer.KeepAlive, negative for off")
234
+ intervalMs := flags.Float64("keepalive-interval-ms", 0, "KeepAliveConfig.Interval")
235
+ probes := flags.Int("keepalive-count", 0, "KeepAliveConfig.Count")
236
+ timeoutMs := flags.Float64("timeout-ms", 0, "Client.Timeout")
237
+ flags.Parse(os.Args[3:])
238
+ given := map[string]bool{}
239
+ flags.Visit(func(f *flag.Flag) { given[f.Name] = true })
240
+ if *mode != "idle" && *mode != "inflight" && *mode != "gap" {
241
+ emit("fail", map[string]any{"why": usage})
242
+ os.Exit(1)
243
+ }
244
+
245
+ // the dialer of http.DefaultTransport (src/net/http/transport.go:47-53), with the settings of the cell
246
+ dialer := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
247
+ if given["dial-timeout-ms"] {
248
+ dialer.Timeout = milliseconds(*dialMs)
249
+ }
250
+ if given["keepalive-ms"] {
251
+ dialer.KeepAlive = milliseconds(*keepAliveMs)
252
+ }
253
+ if given["keepalive-interval-ms"] || given["keepalive-count"] {
254
+ dialer.KeepAliveConfig = net.KeepAliveConfig{
255
+ Enable: dialer.KeepAlive >= 0,
256
+ Idle: dialer.KeepAlive,
257
+ Interval: milliseconds(*intervalMs),
258
+ Count: *probes,
259
+ }
260
+ }
261
+ transport := http.DefaultTransport.(*http.Transport).Clone()
262
+ transport.DialContext = dial(dialer)
263
+ if given["idle-conn-timeout-ms"] {
264
+ transport.IdleConnTimeout = milliseconds(*idleMs)
265
+ }
266
+ if given["max-idle-per-host"] {
267
+ transport.MaxIdleConnsPerHost = *maxIdle
268
+ }
269
+ client := &http.Client{Transport: transport}
270
+ if given["timeout-ms"] {
271
+ client.Timeout = milliseconds(*timeoutMs)
272
+ }
273
+
274
+ bootID, _ := os.ReadFile("/proc/sys/kernel/random/boot_id")
275
+ settings := map[string]any{}
276
+ settings["IdleConnTimeout_ms"] = transport.IdleConnTimeout.Milliseconds()
277
+ settings["MaxIdleConnsPerHost"] = transport.MaxIdleConnsPerHost
278
+ settings["DialTimeout_ms"] = dialer.Timeout.Milliseconds()
279
+ settings["KeepAlive_ms"] = dialer.KeepAlive.Milliseconds()
280
+ settings["KeepAliveInterval_ms"] = dialer.KeepAliveConfig.Interval.Milliseconds()
281
+ settings["KeepAliveCount"] = dialer.KeepAliveConfig.Count
282
+ settings["ClientTimeout_ms"] = client.Timeout.Milliseconds()
283
+ start := map[string]any{}
284
+ start["boot_id"] = strings.TrimSpace(string(bootID))
285
+ start["mode"] = *mode
286
+ start["client"] = "go-nethttp"
287
+ start["go"] = runtime.Version()
288
+ start["settings"] = settings
289
+ emit("start", start)
290
+ go func() {
291
+ time.Sleep(time.Duration(*maxS * float64(time.Second)))
292
+ emit("deadline", nil)
293
+ os.Exit(0)
294
+ }()
295
+
296
+ base := "http://" + net.JoinHostPort(host, port)
297
+ second := "/"
298
+ if *holdMs > 0 {
299
+ second = fmt.Sprintf("/hold?ms=%d", *holdMs)
300
+ } else if *size > 0 {
301
+ second = fmt.Sprintf("/big?bytes=%d", *size)
302
+ }
303
+ if !request(client, base, 1, "GET", "/") {
304
+ os.Exit(1)
305
+ }
306
+ switch *mode {
307
+ case "idle":
308
+ // the connection stays in the pool until it ends, or until the deadline
309
+ select {}
310
+ case "inflight":
311
+ for {
312
+ if _, err := os.Stat(*goPath); err == nil {
313
+ break
314
+ }
315
+ time.Sleep(2 * time.Millisecond)
316
+ }
317
+ case "gap":
318
+ time.Sleep(milliseconds(*gapMs))
319
+ }
320
+ request(client, base, 2, *method, second)
321
+ emit("done", map[string]any{"i": 2})
322
+ time.Sleep(milliseconds(*lingerMs))
323
+ os.Exit(0)
324
+ }
@@ -0,0 +1,12 @@
1
+ # The Node HTTP clients of the harness: the http.Agent of Node 24.21.0, and an HTTP/1.1 Client of
2
+ # undici 8.12.2. Each runs as a container in the client's network namespace, as the harness's own
3
+ # TCP client does, and prints the same JSON lines. The digest of the image and an npm lockfile pin
4
+ # them. The image is apart from the ioredis image, thus the cells of ioredis keep their image.
5
+ # lab.ts stages the build context: this folder, under harness/image/node-http. The lockfile is
6
+ # npm-lock.json here, because npm leaves each package-lock.json out of a published tarball.
7
+ FROM node:24.21.0-alpine@sha256:ebfe2f90462722a7a4de65e91990e97fe0d401c70e0e762c5b53302f905ec1c1
8
+ WORKDIR /opt/harness/node-http
9
+ COPY harness/image/node-http/package.json ./
10
+ COPY harness/image/node-http/npm-lock.json ./package-lock.json
11
+ RUN npm ci --omit=dev --ignore-scripts --no-audit --no-fund
12
+ COPY harness/image/node-http/lines.mjs harness/image/node-http/client-agent.mjs harness/image/node-http/client-undici.mjs ./
@@ -0,0 +1,163 @@
1
+ // client-agent HOST PORT --mode idle|inflight|gap [--gap-ms N] [--method GET|POST] [--hold-ms N]
2
+ // [--bytes N] [--go PATH] [--max-s N] [--linger-ms N]
3
+ // [--timeout-ms N] [--keepalive-msecs N] [--buffer-ms N]
4
+ //
5
+ // The client of the HTTP cells that uses the http.Agent of Node 24.21.0. Without an agent flag it
6
+ // uses http.globalAgent: keep-alive on, a free socket timeout of 5 s, LIFO
7
+ // (lib/internal/http.js:263-265). With a flag, it uses a new Agent with the same options and the
8
+ // flag. Each event is a JSON line, as the TCP client of the harness writes them.
9
+ //
10
+ // The first request is GET /, and the end of its reply is the `ready` line. Then the client holds
11
+ // the connection idle (idle), sends the second request when the file --go appears (inflight), or
12
+ // sends it after the gap (gap). For each request it writes the connection that it went on (the
13
+ // local address and port), whether the agent reused the socket, and the outcome. For each socket it
14
+ // writes the connect, the end of file, the timeout of the agent, and the close.
15
+
16
+ import http from "node:http";
17
+ import { bootId, commandLine, emit, now, pathOf, sleep, waitFor } from "./lines.mjs";
18
+
19
+ const usage = "client-agent HOST PORT --mode idle|inflight|gap";
20
+ const cli = commandLine(usage);
21
+ const mode = cli.flag("--mode");
22
+ if (mode !== "idle" && mode !== "inflight" && mode !== "gap") {
23
+ emit("fail", { why: usage });
24
+ process.exit(1);
25
+ }
26
+ const method = cli.flag("--method") ?? "GET";
27
+ const second = pathOf({ hold: cli.number("--hold-ms"), bytes: cli.number("--bytes") });
28
+ const max = cli.number("--max-s", 60) * 1000;
29
+ const linger = cli.number("--linger-ms", 500);
30
+
31
+ // the options of http.globalAgent, and each flag over them
32
+ const custom = cli.has("--timeout-ms") || cli.has("--keepalive-msecs") || cli.has("--buffer-ms");
33
+ const options = { keepAlive: true, scheduling: "lifo", timeout: 5000 };
34
+ if (cli.has("--timeout-ms")) options.timeout = cli.number("--timeout-ms");
35
+ if (cli.has("--keepalive-msecs")) options.keepAliveMsecs = cli.number("--keepalive-msecs");
36
+ if (cli.has("--buffer-ms")) options.agentKeepAliveTimeoutBuffer = cli.number("--buffer-ms");
37
+ const agent = custom ? new http.Agent(options) : http.globalAgent;
38
+
39
+ emit("start", {
40
+ boot_id: bootId(),
41
+ mode,
42
+ client: "node-http-agent",
43
+ node: process.version,
44
+ agent: {
45
+ global: !custom,
46
+ keepAlive: agent.keepAlive,
47
+ timeout: agent.options.timeout ?? 0,
48
+ keepAliveMsecs: agent.keepAliveMsecs,
49
+ agentKeepAliveTimeoutBuffer: agent.agentKeepAliveTimeoutBuffer,
50
+ scheduling: agent.scheduling,
51
+ },
52
+ });
53
+
54
+ setTimeout(() => {
55
+ emit("deadline");
56
+ process.exit(0);
57
+ }, max);
58
+
59
+ // the sockets of the agent, each with its number, its local address, and what happened to it
60
+ const sockets = new Map();
61
+ const isFree = (socket) => Object.values(agent.freeSockets).some((list) => list.includes(socket));
62
+
63
+ /** This function follows a socket of the agent, one time. */
64
+ const track = (socket) => {
65
+ const known = sockets.get(socket);
66
+ if (known !== undefined) return known;
67
+ const entry = { n: sockets.size + 1, local: null, timedOut: false, pooled: false };
68
+ sockets.set(socket, entry);
69
+ const connected = () => {
70
+ entry.local = `${socket.localAddress}:${socket.localPort}`;
71
+ emit("conn", { n: entry.n, local: entry.local });
72
+ if (entry.n === 1) emit("connected", { local: entry.local });
73
+ };
74
+ if (socket.connecting) socket.once("connect", connected);
75
+ else connected();
76
+ // the listeners of the agent come first: after its free handler, the socket is in the pool or destroyed
77
+ socket.on("free", () => {
78
+ entry.pooled = isFree(socket);
79
+ emit(entry.pooled ? "pooled" : "not-pooled", { local: entry.local });
80
+ });
81
+ socket.on("timeout", () => {
82
+ entry.timedOut = entry.pooled;
83
+ });
84
+ socket.on("end", () => emit("eof", { local: entry.local }));
85
+ socket.on("error", (error) => emit("conn-error", { local: entry.local, errno: error.code ?? "ERROR", message: error.message }));
86
+ socket.on("close", (hadError) => {
87
+ // the agent destroys a free socket on its own timeout, or on any read: an end of file or a
88
+ // reset from the peer (lib/_http_agent.js:101-112). The guard hides the end event
89
+ const by = entry.timedOut ? "client-timer" : hadError ? "error" : entry.pooled ? "peer" : "client";
90
+ if (by === "peer") emit("eof", { local: entry.local, free: true });
91
+ emit("closed", { local: entry.local, by });
92
+ });
93
+ return entry;
94
+ };
95
+
96
+ /** This function sends request `i` and waits for its outcome. */
97
+ const request = (i, how, path) =>
98
+ new Promise((resolve) => {
99
+ const at = now();
100
+ const req = http.request({
101
+ host: cli.host,
102
+ port: cli.port,
103
+ method: how,
104
+ path,
105
+ agent,
106
+ headers: how === "POST" ? { "content-length": "1" } : {},
107
+ });
108
+ emit("sent", { i }, at);
109
+ let ended = false;
110
+ const end = (ok) => {
111
+ if (ended) return;
112
+ ended = true;
113
+ resolve(ok);
114
+ };
115
+ req.on("socket", (socket) => {
116
+ const entry = track(socket);
117
+ entry.pooled = false;
118
+ entry.timedOut = false;
119
+ const show = () => emit("request", { i, local: entry.local, reused: req.reusedSocket });
120
+ if (socket.connecting) socket.once("connect", show);
121
+ else show();
122
+ });
123
+ req.on("response", (res) => {
124
+ emit("response", { i, status: res.statusCode, local: `${res.socket.localAddress}:${res.socket.localPort}` });
125
+ let n = 0;
126
+ res.on("data", (chunk) => {
127
+ n += chunk.length;
128
+ if (i > 1) emit("data", { i, n: chunk.length });
129
+ });
130
+ res.on("end", () => {
131
+ if (i === 1) emit("ready", { reply: `HTTP ${res.statusCode}` });
132
+ else emit("replied", { i, n: { value: n, unit: "B" } });
133
+ end(true);
134
+ });
135
+ res.on("error", (error) => {
136
+ emit("error", { i, errno: error.code ?? "ERROR", message: error.message });
137
+ end(false);
138
+ });
139
+ });
140
+ req.on("error", (error) => {
141
+ emit("error", { i, errno: error.code ?? "ERROR", message: error.message });
142
+ end(false);
143
+ });
144
+ req.end(how === "POST" ? "x" : undefined);
145
+ });
146
+
147
+ const finish = async () => {
148
+ emit("done", { i: 2 });
149
+ await sleep(linger);
150
+ process.exit(0);
151
+ };
152
+
153
+ if (!(await request(1, "GET", "/"))) process.exit(1);
154
+ if (mode === "inflight") {
155
+ await waitFor(cli.flag("--go") ?? "/tmp/go");
156
+ await request(2, method, second);
157
+ await finish();
158
+ } else if (mode === "gap") {
159
+ await sleep(cli.number("--gap-ms", 0));
160
+ await request(2, method, second);
161
+ await finish();
162
+ }
163
+ // idle: the connection stays in the pool until it ends, or until the deadline
@@ -0,0 +1,141 @@
1
+ // client-undici HOST PORT --mode idle|inflight|gap [--gap-ms N] [--method GET|POST] [--hold-ms N]
2
+ // [--bytes N] [--go PATH] [--max-s N] [--linger-ms N]
3
+ // [--keepalive-timeout-ms N] [--keepalive-max-timeout-ms N] [--keepalive-threshold-ms N]
4
+ // [--headers-timeout-ms N] [--body-timeout-ms N] [--connect-timeout-ms N]
5
+ // [--keepalive-initial-delay-ms N]
6
+ //
7
+ // The client of the HTTP cells that uses an HTTP/1.1 Client of undici 8.12.2, at its defaults
8
+ // with each flag over them. Each event is a JSON line, as the TCP client of the harness writes them.
9
+ //
10
+ // The workload is the workload of client-agent.mjs. A connector around the connector of undici
11
+ // follows each socket. The channel undici:client:sendHeaders tells which socket each request goes
12
+ // on. The disconnect event of the client tells why a socket ended: an end of file (other side
13
+ // closed), a reset, or the keep-alive timer of the client (socket idle timeout).
14
+
15
+ import diagnostics from "node:diagnostics_channel";
16
+ import { Client, buildConnector } from "undici";
17
+ import { bootId, commandLine, emit, now, pathOf, sleep, waitFor } from "./lines.mjs";
18
+
19
+ const usage = "client-undici HOST PORT --mode idle|inflight|gap";
20
+ const cli = commandLine(usage);
21
+ const mode = cli.flag("--mode");
22
+ if (mode !== "idle" && mode !== "inflight" && mode !== "gap") {
23
+ emit("fail", { why: usage });
24
+ process.exit(1);
25
+ }
26
+ const method = cli.flag("--method") ?? "GET";
27
+ const second = pathOf({ hold: cli.number("--hold-ms"), bytes: cli.number("--bytes") });
28
+ const max = cli.number("--max-s", 60) * 1000;
29
+ const linger = cli.number("--linger-ms", 500);
30
+
31
+ // the options of the client: undici's defaults, and each flag over them
32
+ const options = {};
33
+ const set = (option, name) => {
34
+ if (cli.has(name)) options[option] = cli.number(name);
35
+ };
36
+ set("keepAliveTimeout", "--keepalive-timeout-ms");
37
+ set("keepAliveMaxTimeout", "--keepalive-max-timeout-ms");
38
+ set("keepAliveTimeoutThreshold", "--keepalive-threshold-ms");
39
+ set("headersTimeout", "--headers-timeout-ms");
40
+ set("bodyTimeout", "--body-timeout-ms");
41
+ const connect = {};
42
+ if (cli.has("--connect-timeout-ms")) connect.timeout = cli.number("--connect-timeout-ms");
43
+ if (cli.has("--keepalive-initial-delay-ms")) connect.keepAliveInitialDelay = cli.number("--keepalive-initial-delay-ms");
44
+
45
+ emit("start", { boot_id: bootId(), mode, client: "undici", node: process.version, options, connect });
46
+
47
+ setTimeout(() => {
48
+ emit("deadline");
49
+ process.exit(0);
50
+ }, max);
51
+
52
+ // the sockets of the client, each with its number, its local address, and its use
53
+ const sockets = new Map();
54
+ let current = null;
55
+
56
+ /** This function follows a socket of the client, one time. */
57
+ const track = (socket) => {
58
+ const known = sockets.get(socket);
59
+ if (known !== undefined) return known;
60
+ const entry = { n: sockets.size + 1, local: `${socket.localAddress}:${socket.localPort}`, used: false };
61
+ sockets.set(socket, entry);
62
+ current = entry;
63
+ emit("conn", { n: entry.n, local: entry.local });
64
+ if (entry.n === 1) emit("connected", { local: entry.local });
65
+ socket.on("end", () => emit("eof", { local: entry.local }));
66
+ socket.on("error", (error) =>
67
+ // undici destroys a socket with an error of its own when it closes the socket itself
68
+ OWN.has(error.code)
69
+ ? emit("client-close", { local: entry.local, errno: error.code, message: error.message })
70
+ : emit("conn-error", { local: entry.local, errno: error.code ?? "ERROR", message: error.message }),
71
+ );
72
+ socket.on("close", () => emit("closed", { local: entry.local }));
73
+ return entry;
74
+ };
75
+
76
+ // the errors of undici for a close by the client itself: no reuse, its keep-alive timer, its deadlines
77
+ const OWN = new Set(["UND_ERR_INFO", "UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT", "UND_ERR_CONNECT_TIMEOUT"]);
78
+
79
+ // the default connector of undici with the connect options, around a function that follows each socket
80
+ const connector = buildConnector(connect);
81
+ let requestIndex = 0;
82
+ const client = new Client(`http://${cli.host}:${cli.port}`, {
83
+ ...options,
84
+ connect: (opts, callback) =>
85
+ connector(opts, (error, socket) => {
86
+ if (error === null && socket !== undefined && socket !== null) track(socket);
87
+ callback(error, socket);
88
+ }),
89
+ });
90
+
91
+ diagnostics.subscribe("undici:client:sendHeaders", ({ socket }) => {
92
+ const entry = track(socket);
93
+ emit("request", { i: requestIndex, local: entry.local, reused: entry.used });
94
+ entry.used = true;
95
+ });
96
+ client.on("disconnect", (_origin, _targets, error) => {
97
+ // UND_ERR_INFO is a close by the client: no reuse (reset), or its keep-alive timer (socket idle timeout)
98
+ const by = OWN.has(error?.code) ? "client" : "peer";
99
+ emit("disconnect", { local: current?.local ?? null, by, errno: error?.code ?? null, message: error?.message ?? null });
100
+ });
101
+
102
+ /** This function sends request `i` and waits for its outcome. */
103
+ const request = async (i, how, path) => {
104
+ requestIndex = i;
105
+ const at = now();
106
+ const pending = client.request({ path, method: how, body: how === "POST" ? "x" : undefined });
107
+ emit("sent", { i }, at);
108
+ try {
109
+ const { statusCode, body } = await pending;
110
+ emit("response", { i, status: statusCode });
111
+ let n = 0;
112
+ for await (const chunk of body) {
113
+ n += chunk.length;
114
+ if (i > 1) emit("data", { i, n: chunk.length });
115
+ }
116
+ if (i === 1) emit("ready", { reply: `HTTP ${statusCode}` });
117
+ else emit("replied", { i, n: { value: n, unit: "B" } });
118
+ return true;
119
+ } catch (error) {
120
+ emit("error", { i, errno: error.code ?? "ERROR", cause: error.cause?.code ?? null, message: error.message });
121
+ return false;
122
+ }
123
+ };
124
+
125
+ const finish = async () => {
126
+ emit("done", { i: 2 });
127
+ await sleep(linger);
128
+ process.exit(0);
129
+ };
130
+
131
+ if (!(await request(1, "GET", "/"))) process.exit(1);
132
+ if (mode === "inflight") {
133
+ await waitFor(cli.flag("--go") ?? "/tmp/go");
134
+ await request(2, method, second);
135
+ await finish();
136
+ } else if (mode === "gap") {
137
+ await sleep(cli.number("--gap-ms", 0));
138
+ await request(2, method, second);
139
+ await finish();
140
+ }
141
+ // idle: the connection stays open until it ends, or until the deadline