| ... | ... | @@ -4,8 +4,6 @@ |
| 4 | 4 | //! |
| 5 | 5 | //! TLS support may be disabled via `std.options.http_disable_tls`. |
| 6 | 6 | //! |
| 7 | | //! TODO all the lockUncancelable in this file should be changed to regular lock and |
| 8 | | //! `error.Canceled` added to more error sets. |
| 9 | 7 | const Client = @This(); |
| 10 | 8 | |
| 11 | 9 | const builtin = @import("builtin"); |
| ... | ... | @@ -84,8 +82,8 @@ pub const ConnectionPool = struct { |
| 84 | 82 | /// If no connection is found, null is returned. |
| 85 | 83 | /// |
| 86 | 84 | /// Threadsafe. |
| 87 | | pub fn findConnection(pool: *ConnectionPool, io: Io, criteria: Criteria) ?*Connection { |
| 88 | | pool.mutex.lockUncancelable(io); |
| 85 | pub fn findConnection(pool: *ConnectionPool, io: Io, criteria: Criteria) Io.Cancelable!?*Connection { |
| 86 | try pool.mutex.lock(io); |
| 89 | 87 | defer pool.mutex.unlock(io); |
| 90 | 88 | |
| 91 | 89 | var next = pool.free.last; |
| ... | ... | @@ -113,8 +111,8 @@ pub const ConnectionPool = struct { |
| 113 | 111 | } |
| 114 | 112 | |
| 115 | 113 | /// Acquires an existing connection from the connection pool. This function is threadsafe. |
| 116 | | pub fn acquire(pool: *ConnectionPool, io: Io, connection: *Connection) void { |
| 117 | | pool.mutex.lockUncancelable(io); |
| 114 | pub fn acquire(pool: *ConnectionPool, io: Io, connection: *Connection) Io.Cancelable!void { |
| 115 | try pool.mutex.lock(io); |
| 118 | 116 | defer pool.mutex.unlock(io); |
| 119 | 117 | |
| 120 | 118 | return pool.acquireUnsafe(connection); |
| ... | ... | @@ -150,8 +148,8 @@ pub const ConnectionPool = struct { |
| 150 | 148 | } |
| 151 | 149 | |
| 152 | 150 | /// Adds a newly created node to the pool of used connections. This function is threadsafe. |
| 153 | | pub fn addUsed(pool: *ConnectionPool, io: Io, connection: *Connection) void { |
| 154 | | pool.mutex.lockUncancelable(io); |
| 151 | pub fn addUsed(pool: *ConnectionPool, io: Io, connection: *Connection) Io.Cancelable!void { |
| 152 | try pool.mutex.lock(io); |
| 155 | 153 | defer pool.mutex.unlock(io); |
| 156 | 154 | |
| 157 | 155 | pool.used.append(&connection.pool_node); |
| ... | ... | @@ -162,18 +160,15 @@ pub const ConnectionPool = struct { |
| 162 | 160 | /// If the new size is smaller than the current size, then idle connections will be closed until the pool is the new size. |
| 163 | 161 | /// |
| 164 | 162 | /// Threadsafe. |
| 165 | | pub fn resize(pool: *ConnectionPool, io: Io, allocator: Allocator, new_size: usize) void { |
| 166 | | pool.mutex.lockUncancelable(io); |
| 163 | pub fn resize(pool: *ConnectionPool, io: Io, new_size: usize) Io.Cancelable!void { |
| 164 | try pool.mutex.lock(io); |
| 167 | 165 | defer pool.mutex.unlock(io); |
| 168 | 166 | |
| 169 | | const next = pool.free.first; |
| 170 | | _ = next; |
| 171 | 167 | while (pool.free_len > new_size) { |
| 172 | | const popped = pool.free.popFirst() orelse unreachable; |
| 168 | const popped: *Connection = @alignCast(@fieldParentPtr("pool_node", pool.free.popFirst().?)); |
| 173 | 169 | pool.free_len -= 1; |
| 174 | 170 | |
| 175 | | popped.data.close(allocator); |
| 176 | | allocator.destroy(popped); |
| 171 | popped.destroy(io); |
| 177 | 172 | } |
| 178 | 173 | |
| 179 | 174 | pool.free_size = new_size; |
| ... | ... | @@ -1323,7 +1318,7 @@ pub fn initDefaultProxies(client: *Client, arena: Allocator, environ_map: *const |
| 1323 | 1318 | const io = client.io; |
| 1324 | 1319 | |
| 1325 | 1320 | // Prevent any new connections from being created. |
| 1326 | | client.connection_pool.mutex.lockUncancelable(io); |
| 1321 | try client.connection_pool.mutex.lock(io); |
| 1327 | 1322 | defer client.connection_pool.mutex.unlock(io); |
| 1328 | 1323 | |
| 1329 | 1324 | assert(client.connection_pool.used.first == null); // There are active requests. |
| ... | ... | @@ -1418,7 +1413,7 @@ pub const basic_authorization = struct { |
| 1418 | 1413 | |
| 1419 | 1414 | pub const ConnectTcpError = error{ |
| 1420 | 1415 | TlsInitializationFailed, |
| 1421 | | } || Allocator.Error || HostName.ConnectError; |
| 1416 | } || Allocator.Error || HostName.ConnectError || Io.Cancelable; |
| 1422 | 1417 | |
| 1423 | 1418 | /// Reuses a `Connection` if one matching `host` and `port` is already open. |
| 1424 | 1419 | /// |
| ... | ... | @@ -1451,7 +1446,7 @@ pub fn connectTcpOptions(client: *Client, options: ConnectTcpOptions) ConnectTcp |
| 1451 | 1446 | const proxied_host = options.proxied_host orelse host; |
| 1452 | 1447 | const proxied_port = options.proxied_port orelse port; |
| 1453 | 1448 | |
| 1454 | | if (client.connection_pool.findConnection(io, .{ |
| 1449 | if (try client.connection_pool.findConnection(io, .{ |
| 1455 | 1450 | .host = proxied_host, |
| 1456 | 1451 | .port = proxied_port, |
| 1457 | 1452 | .protocol = protocol, |
| ... | ... | @@ -1469,18 +1464,20 @@ pub fn connectTcpOptions(client: *Client, options: ConnectTcpOptions) ConnectTcp |
| 1469 | 1464 | error.Canceled => |e| return e, |
| 1470 | 1465 | else => return error.TlsInitializationFailed, |
| 1471 | 1466 | }; |
| 1472 | | client.connection_pool.addUsed(io, &tc.connection); |
| 1467 | errdefer tc.destroy(); |
| 1468 | try client.connection_pool.addUsed(io, &tc.connection); |
| 1473 | 1469 | return &tc.connection; |
| 1474 | 1470 | }, |
| 1475 | 1471 | .plain => { |
| 1476 | 1472 | const pc = try Connection.Plain.create(client, proxied_host, proxied_port, stream); |
| 1477 | | client.connection_pool.addUsed(io, &pc.connection); |
| 1473 | errdefer pc.destroy(); |
| 1474 | try client.connection_pool.addUsed(io, &pc.connection); |
| 1478 | 1475 | return &pc.connection; |
| 1479 | 1476 | }, |
| 1480 | 1477 | } |
| 1481 | 1478 | } |
| 1482 | 1479 | |
| 1483 | | pub const ConnectUnixError = Allocator.Error || std.posix.SocketError || error{NameTooLong} || std.posix.ConnectError; |
| 1480 | pub const ConnectUnixError = Allocator.Error || std.posix.SocketError || error{NameTooLong} || std.posix.ConnectError || Io.Cancelable; |
| 1484 | 1481 | |
| 1485 | 1482 | /// Connect to `path` as a unix domain socket. This will reuse a connection if one is already open. |
| 1486 | 1483 | /// |
| ... | ... | @@ -1488,7 +1485,7 @@ pub const ConnectUnixError = Allocator.Error || std.posix.SocketError || error{N |
| 1488 | 1485 | pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*Connection { |
| 1489 | 1486 | const io = client.io; |
| 1490 | 1487 | |
| 1491 | | if (client.connection_pool.findConnection(io, .{ |
| 1488 | if (try client.connection_pool.findConnection(io, .{ |
| 1492 | 1489 | .host = path, |
| 1493 | 1490 | .port = 0, |
| 1494 | 1491 | .protocol = .plain, |
| ... | ... | @@ -1512,7 +1509,7 @@ pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*Connecti |
| 1512 | 1509 | }; |
| 1513 | 1510 | errdefer client.allocator.free(conn.data.host); |
| 1514 | 1511 | |
| 1515 | | client.connection_pool.addUsed(conn); |
| 1512 | try client.connection_pool.addUsed(conn); |
| 1516 | 1513 | |
| 1517 | 1514 | return &conn.data; |
| 1518 | 1515 | } |
| ... | ... | @@ -1530,7 +1527,7 @@ pub fn connectProxied( |
| 1530 | 1527 | const io = client.io; |
| 1531 | 1528 | if (!proxy.supports_connect) return error.TunnelNotSupported; |
| 1532 | 1529 | |
| 1533 | | if (client.connection_pool.findConnection(io, .{ |
| 1530 | if (try client.connection_pool.findConnection(io, .{ |
| 1534 | 1531 | .host = proxied_host, |
| 1535 | 1532 | .port = proxied_port, |
| 1536 | 1533 | .protocol = proxy.protocol, |