Skip to content

Commit 735772f

Browse files
committed
On a non websocket upgrade connection, close the connection
Solves slow startup time with chromedp
1 parent 82e67b7 commit 735772f

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/server.zig

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,15 @@ pub const Client = struct {
346346
}
347347

348348
if (std.mem.eql(u8, url, "/json/version")) {
349-
return self.send(null, self.server.json_version_response);
349+
try self.send(null, self.server.json_version_response);
350+
// Chromedp (a Go driver) does an http request to /json/version
351+
// then to / (websocket upgrade) using a different connection.
352+
// Since we only allow 1 connection at a time, the 2nd one (the
353+
// websocket upgrade) blocks until the first one times out.
354+
// We can avoid that by closing the connection. json_version_response
355+
// has a Connection: Close header too.
356+
try posix.shutdown(self.socket, .recv);
357+
return;
350358
}
351359

352360
return error.NotFound;
@@ -1059,9 +1067,16 @@ fn buildJSONVersionResponse(
10591067
const body_format = "{{\"webSocketDebuggerUrl\": \"ws://{}/\"}}";
10601068
const body_len = std.fmt.count(body_format, .{address});
10611069

1070+
// We send a Connection: Close (and actually close the connection)
1071+
// because chromedp (Go driver) sends a request to /json/version and then
1072+
// does an upgrade request, on a different connection. Since we only allow
1073+
// 1 connection at a time, the upgrade connection doesn't proceed until we
1074+
// timeout the /json/version. So, instead of waiting for that, we just
1075+
// always close HTTP requests.
10621076
const response_format =
10631077
"HTTP/1.1 200 OK\r\n" ++
10641078
"Content-Length: {d}\r\n" ++
1079+
"Connection: Close\r\n" ++
10651080
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
10661081
body_format;
10671082
return try std.fmt.allocPrint(allocator, response_format, .{ body_len, address });
@@ -1122,6 +1137,7 @@ test "server: buildJSONVersionResponse" {
11221137

11231138
try testing.expectEqualStrings("HTTP/1.1 200 OK\r\n" ++
11241139
"Content-Length: 48\r\n" ++
1140+
"Connection: Close\r\n" ++
11251141
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
11261142
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9001/\"}", res);
11271143
}
@@ -1357,6 +1373,7 @@ test "server: get /json/version" {
13571373
const expected_response =
13581374
"HTTP/1.1 200 OK\r\n" ++
13591375
"Content-Length: 48\r\n" ++
1376+
"Connection: Close\r\n" ++
13601377
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
13611378
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9583/\"}";
13621379

@@ -1366,11 +1383,7 @@ test "server: get /json/version" {
13661383
defer c.deinit();
13671384

13681385
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
1369-
try testing.expectEqualStrings(expected_response, res1);
1370-
1371-
const res2 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
1372-
try testing.expectEqualStrings(expected_response, res2);
1373-
}
1386+
try testing.expectEqualStrings(expected_response, res1); }
13741387

13751388
{
13761389
// again on a new connection
@@ -1379,9 +1392,6 @@ test "server: get /json/version" {
13791392

13801393
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
13811394
try testing.expectEqualStrings(expected_response, res1);
1382-
1383-
const res2 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
1384-
try testing.expectEqualStrings(expected_response, res2);
13851395
}
13861396
}
13871397

0 commit comments

Comments
 (0)