FIx bug in http client

This commit is contained in:
Jarred Sumner
2021-12-29 02:32:57 -08:00
parent 565cf23d92
commit fe17d51b03
3 changed files with 31 additions and 14 deletions

View File

@@ -385,9 +385,9 @@ pub const AsyncHTTP = struct {
var timer = std.time.Timer.start() catch @panic("Timer failure");
defer this.elapsed = timer.read();
_ = active_requests_count.fetchAdd(1, .Monotonic);
defer _ = active_requests_count.fetchSub(1, .Monotonic);
this.response = await this.client.sendAsync(this.request_body.list.items, this.response_buffer) catch |err| {
_ = active_requests_count.fetchSub(1, .Monotonic);
this.state.store(.fail, .Monotonic);
this.err = err;
@@ -402,6 +402,7 @@ pub const AsyncHTTP = struct {
this.redirect_count = @intCast(u32, @maximum(127 - this.client.remaining_redirect_count, 0));
this.state.store(.success, .Monotonic);
this.gzip_elapsed = this.client.gzip_elapsed;
_ = active_requests_count.fetchSub(1, .Monotonic);
}
if (sender.http.callback) |callback| {
@@ -536,7 +537,7 @@ pub const AsyncMessage = struct {
pub fn release(self: *AsyncMessage) void {
self.used = 0;
self.sent = 0;
std.debug.assert(!self.released);
if (self.released) return;
self.released = true;
if (self.pooled != null) {
@@ -1462,16 +1463,6 @@ pub fn connect(
client.setReadBufferSize(BufferPool.len) catch {};
client.setQuickACK(true) catch {};
if (comptime Environment.isMac) {
// Don't crash if the server disconnects.
std.os.setsockopt(
client.socket.fd,
std.os.IPPROTO_TCP,
std.os.SO_NOSIGPIPE,
std.mem.asBytes(&@as(u32, @boolToInt(true))),
) catch {};
}
this.tcp_client = client;
if (this.timeout > 0) {
client.setReadTimeout(@truncate(u32, this.timeout / std.time.ns_per_ms)) catch {};

View File

@@ -4846,6 +4846,32 @@ pub const PackageManager = struct {
// this.enable.deduplicate_packages = false;
// }
if (env_loader.map.get("BUN_CONFIG_MAX_HTTP_REQUESTS")) |max_http_requests| {
load: {
AsyncHTTP.max_simultaneous_requests = std.fmt.parseInt(u16, max_http_requests, 10) catch |err| {
log.addErrorFmt(
null,
logger.Loc.Empty,
allocator,
"BUN_CONFIG_MAX_HTTP_REQUESTS value \"{s}\" is not a valid integer between 1 and 65535",
.{max_http_requests},
) catch unreachable;
break :load;
};
if (AsyncHTTP.max_simultaneous_requests == 0) {
log.addWarningFmt(
null,
logger.Loc.Empty,
allocator,
"BUN_CONFIG_MAX_HTTP_REQUESTS value must be a number between 1 and 65535",
.{},
) catch unreachable;
AsyncHTTP.max_simultaneous_requests = 255;
}
}
}
this.do.save_lockfile = strings.eqlComptime((env_loader.map.get("BUN_CONFIG_SKIP_SAVE_LOCKFILE") orelse "0"), "0");
this.do.load_lockfile = strings.eqlComptime((env_loader.map.get("BUN_CONFIG_SKIP_LOAD_LOCKFILE") orelse "0"), "0");
this.do.install_packages = strings.eqlComptime((env_loader.map.get("BUN_CONFIG_SKIP_INSTALL_PACKAGES") orelse "0"), "0");

View File

@@ -248,8 +248,8 @@ noinline fn wait(self: *ThreadPool, _is_waking: bool) error{Shutdown}!bool {
} else {
if (self.io) |io| {
const HTTP = @import("http");
io.run_for_ns(std.time.ns_per_us * 100) catch {};
while (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > 255) {
io.run_for_ns(std.time.ns_per_us * 10) catch {};
while (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > HTTP.AsyncHTTP.max_simultaneous_requests) {
io.tick() catch {};
}
sync = @bitCast(Sync, self.sync.load(.Monotonic));