diff options
-rw-r--r-- | src/bun.js/api/bun/socket.zig | 15 | ||||
-rw-r--r-- | src/deps/uws.zig | 33 | ||||
-rw-r--r-- | src/http_client_async.zig | 35 |
3 files changed, 64 insertions, 19 deletions
diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index a8c0948d9..752adea1e 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -771,7 +771,7 @@ pub const Listener = struct { Socket.dataSetCached(this_socket.getThisValue(globalObject), globalObject, default_data); } socket.ext(**anyopaque).?.* = bun.cast(**anyopaque, this_socket); - socket.timeout(120000); + socket.setTimeout(120000); } // pub fn addServerName(this: *Listener, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSValue { @@ -1599,7 +1599,7 @@ fn NewSocket(comptime ssl: bool) type { return .zero; } - this.socket.timeout(@as(c_uint, @intCast(t))); + this.socket.setTimeout(@as(c_uint, @intCast(t))); return JSValue.jsUndefined(); } @@ -2962,6 +2962,17 @@ pub fn NewWrappedHandler(comptime tls: bool) type { } } + pub fn onLongTimeout( + this: WrappedSocket, + socket: Socket, + ) void { + if (comptime tls) { + TLSSocket.onTimeout(this.tls, socket); + } else { + TLSSocket.onTimeout(this.tcp, socket); + } + } + pub fn onConnectError( this: WrappedSocket, socket: Socket, diff --git a/src/deps/uws.zig b/src/deps/uws.zig index 40a50a448..7a79e9c0f 100644 --- a/src/deps/uws.zig +++ b/src/deps/uws.zig @@ -40,6 +40,20 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type { return us_socket_timeout(comptime ssl_int, this.socket, seconds); } + pub fn setTimeout(this: ThisSocket, seconds: c_uint) void { + if (seconds > 240) { + us_socket_timeout(comptime ssl_int, this.socket, 0); + us_socket_long_timeout(comptime ssl_int, this.socket, seconds / 60); + } else { + us_socket_timeout(comptime ssl_int, this.socket, seconds); + us_socket_long_timeout(comptime ssl_int, this.socket, 0); + } + } + + pub fn setTimeoutMinutes(this: ThisSocket, minutes: c_uint) void { + return us_socket_long_timeout(comptime ssl_int, this.socket, minutes); + } + pub fn startTLS(this: ThisSocket, is_client: bool) void { _ = us_socket_open(comptime ssl_int, this.socket, @intFromBool(is_client), null, 0); } @@ -126,6 +140,13 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type { ); return socket; } + pub fn on_long_timeout(socket: *Socket) callconv(.C) ?*Socket { + Fields.onLongTimeout( + getValue(socket), + TLSSocket{ .socket = socket }, + ); + return socket; + } pub fn on_connect_error(socket: *Socket, code: i32) callconv(.C) ?*Socket { Fields.onConnectError( getValue(socket), @@ -155,6 +176,7 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type { .on_connect_error = SocketHandler.on_connect_error, .on_end = SocketHandler.on_end, .on_handshake = SocketHandler.on_handshake, + .on_long_timeout = SocketHandler.on_long_timeout, }; const socket = us_socket_wrap_with_tls(ssl_int, this.socket, options, events, socket_ext_size) orelse return null; @@ -578,6 +600,13 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type { ); return socket; } + pub fn on_long_timeout(socket: *Socket) callconv(.C) ?*Socket { + Fields.onLongTimeout( + getValue(socket), + ThisSocket{ .socket = socket }, + ); + return socket; + } pub fn on_connect_error(socket: *Socket, code: i32) callconv(.C) ?*Socket { Fields.onConnectError( getValue(socket), @@ -614,6 +643,8 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type { us_socket_context_on_end(ssl_int, ctx, SocketHandler.on_end); if (comptime @hasDecl(Type, "onHandshake") and @typeInfo(@TypeOf(Type.onHandshake)) != .Null) us_socket_context_on_handshake(ssl_int, ctx, SocketHandler.on_handshake, null); + if (comptime @hasDecl(Type, "onLongTimeout") and @typeInfo(@TypeOf(Type.onLongTimeout)) != .Null) + us_socket_context_on_long_timeout(ssl_int, ctx, SocketHandler.on_long_timeout); } pub fn from(socket: *Socket) ThisSocket { @@ -985,6 +1016,7 @@ extern fn us_socket_context_on_writable(ssl: i32, context: ?*SocketContext, on_w extern fn us_socket_context_on_handshake(ssl: i32, context: ?*SocketContext, on_handshake: *const fn (*Socket, i32, us_bun_verify_error_t, ?*anyopaque) callconv(.C) void, ?*anyopaque) void; extern fn us_socket_context_on_timeout(ssl: i32, context: ?*SocketContext, on_timeout: *const fn (*Socket) callconv(.C) ?*Socket) void; +extern fn us_socket_context_on_long_timeout(ssl: i32, context: ?*SocketContext, on_timeout: *const fn (*Socket) callconv(.C) ?*Socket) void; extern fn us_socket_context_on_connect_error(ssl: i32, context: ?*SocketContext, on_connect_error: *const fn (*Socket, i32) callconv(.C) ?*Socket) void; extern fn us_socket_context_on_end(ssl: i32, context: ?*SocketContext, on_end: *const fn (*Socket) callconv(.C) ?*Socket) void; extern fn us_socket_context_ext(ssl: i32, context: ?*SocketContext) ?*anyopaque; @@ -1089,6 +1121,7 @@ pub const Poll = opaque { extern fn us_socket_get_native_handle(ssl: i32, s: ?*Socket) ?*anyopaque; extern fn us_socket_timeout(ssl: i32, s: ?*Socket, seconds: c_uint) void; +extern fn us_socket_long_timeout(ssl: i32, s: ?*Socket, seconds: c_uint) void; extern fn us_socket_ext(ssl: i32, s: ?*Socket) ?*anyopaque; extern fn us_socket_context(ssl: i32, s: ?*Socket) ?*SocketContext; extern fn us_socket_flush(ssl: i32, s: ?*Socket) void; diff --git a/src/http_client_async.zig b/src/http_client_async.zig index 6c731acd5..bbcc28252 100644 --- a/src/http_client_async.zig +++ b/src/http_client_async.zig @@ -400,7 +400,8 @@ fn NewHTTPContext(comptime ssl: bool) type { if (this.pending_sockets.get()) |pending| { socket.ext(**anyopaque).?.* = bun.cast(**anyopaque, ActiveSocket.init(pending).ptr()); socket.flush(); - socket.timeout(300); + socket.timeout(0); + socket.setTimeoutMinutes(5); pending.http_socket = socket; @memcpy(pending.hostname_buf[0..hostname.len], hostname); @@ -535,7 +536,7 @@ fn NewHTTPContext(comptime ssl: bool) type { ); } } - pub fn onTimeout( + pub fn onLongTimeout( ptr: *anyopaque, socket: HTTPSocket, ) void { @@ -2279,7 +2280,7 @@ pub fn onWritable(this: *HTTPClient, comptime is_first_call: bool, comptime is_s defer if (list.capacity > stack_fallback.buffer.len) list.deinit(); var writer = &list.writer(); - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); const request = this.buildRequest(this.state.original_request_body.len()); @@ -2389,7 +2390,7 @@ pub fn onWritable(this: *HTTPClient, comptime is_first_call: bool, comptime is_s } }, .body => { - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); switch (this.state.original_request_body) { .bytes => { @@ -2435,7 +2436,7 @@ pub fn onWritable(this: *HTTPClient, comptime is_first_call: bool, comptime is_s } var proxy = this.proxy_tunnel orelse return; - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); const to_send = this.state.request_body; const amount = proxy.ssl.write(to_send) catch |err| { @@ -2457,15 +2458,13 @@ pub fn onWritable(this: *HTTPClient, comptime is_first_call: bool, comptime is_s .proxy_headers => { const proxy = this.proxy_tunnel orelse return; - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); var stack_fallback = std.heap.stackFallback(16384, default_allocator); var allocator = stack_fallback.get(); var list = std.ArrayList(u8).initCapacity(allocator, stack_fallback.buffer.len) catch unreachable; defer if (list.capacity > stack_fallback.buffer.len) list.deinit(); var writer = &list.writer(); - this.setTimeout(socket, 60); - const request = this.buildRequest(this.state.request_body.len); writeRequest( @TypeOf(writer), @@ -2536,7 +2535,7 @@ pub fn onWritable(this: *HTTPClient, comptime is_first_call: bool, comptime is_s else => { //Just check if need to call SSL_read if requested to be writable var proxy = this.proxy_tunnel orelse return; - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); var data = proxy.getSSLData(null) catch |err| { this.closeAndFail(err, is_ssl, socket); return; @@ -2651,7 +2650,7 @@ pub fn onData(this: *HTTPClient, comptime is_ssl: bool, incoming_data: []const u } } - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); }, else => { this.closeAndFail(err, is_ssl, socket); @@ -2763,7 +2762,7 @@ pub fn onData(this: *HTTPClient, comptime is_ssl: bool, incoming_data: []const u } } } else if (this.state.response_stage == .body_chunk) { - this.setTimeout(socket, 500); + this.setTimeout(socket, 5); { const report_progress = this.handleResponseBodyChunkedEncoding(body_buf) catch |err| { this.closeAndFail(err, is_ssl, socket); @@ -2785,7 +2784,7 @@ pub fn onData(this: *HTTPClient, comptime is_ssl: bool, incoming_data: []const u }, .body => { - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); if (this.proxy_tunnel != null) { var proxy = this.proxy_tunnel.?; @@ -2820,7 +2819,7 @@ pub fn onData(this: *HTTPClient, comptime is_ssl: bool, incoming_data: []const u }, .body_chunk => { - this.setTimeout(socket, 500); + this.setTimeout(socket, 5); if (this.proxy_tunnel != null) { var proxy = this.proxy_tunnel.?; @@ -2857,7 +2856,7 @@ pub fn onData(this: *HTTPClient, comptime is_ssl: bool, incoming_data: []const u .fail => {}, .proxy_headers => { - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); var proxy = this.proxy_tunnel orelse return; var data = proxy.getSSLData(incoming_data) catch |err| { this.closeAndFail(err, is_ssl, socket); @@ -2874,7 +2873,7 @@ pub fn onData(this: *HTTPClient, comptime is_ssl: bool, incoming_data: []const u this.onData(is_ssl, decoded_data, ctx, socket); }, .proxy_handshake => { - this.setTimeout(socket, 60); + this.setTimeout(socket, 5); // put more data into SSL const proxy = this.proxy_tunnel orelse return; @@ -2950,13 +2949,15 @@ fn cloneMetadata(this: *HTTPClient) void { } } -pub fn setTimeout(this: *HTTPClient, socket: anytype, amount: c_uint) void { +pub fn setTimeout(this: *HTTPClient, socket: anytype, minutes: c_uint) void { if (this.disable_timeout) { socket.timeout(0); + socket.setTimeoutMinutes(0); return; } - socket.timeout(amount); + socket.timeout(0); + socket.setTimeoutMinutes(minutes); } pub fn progressUpdate(this: *HTTPClient, comptime is_ssl: bool, ctx: *NewHTTPContext(is_ssl), socket: NewHTTPContext(is_ssl).HTTPSocket) void { |