A SpaceTraders Agent

wait call got stuck

altagos.dev ca95640c febe319b

verified
+39 -18
+28 -12
src/st/http.zig
··· 96 96 return l.semaphor.wait(io); 97 97 } 98 98 99 - pub fn timeToReset(l: *Limiter, io: Io) i64 { 99 + pub fn timeToReset(l: *Limiter, io: Io) Io.Duration { 100 100 if (l.time) |t| { 101 - return t.durationTo(Io.Clock.now(.real, io) catch return 0).raw.toMilliseconds(); 101 + return t.durationTo(Io.Clock.now(.real, io) catch return .zero); 102 102 } 103 - return 0; 103 + return .zero; 104 104 } 105 105 106 106 pub fn available(l: *Limiter, io: Io) bool { ··· 112 112 static: Limiter, 113 113 burst: Limiter, 114 114 115 - pub fn wait(bl: *BurstyLimiter, io: Io) !bool { 115 + pub fn wait(bl: *BurstyLimiter, io: Io) !void { 116 116 _ = bl.static.checkReset(io); 117 117 _ = bl.burst.checkReset(io); 118 118 ··· 120 120 if (bl.burst.available(io)) { 121 121 log.debug("Using Burst", .{}); 122 122 try bl.burst.aquire(io); 123 - return true; 123 + return; 124 124 } else { 125 125 log.warn("No request available, waiting", .{}); 126 + 127 + var static = bl.static.checkReset(io); 128 + var burst = bl.burst.checkReset(io); 129 + 130 + while (!static and !burst and !bl.static.available(io)) { 131 + try io.sleep(bl.static.timeToReset(io), .real); 132 + static = bl.static.checkReset(io); 133 + burst = bl.burst.checkReset(io); 134 + } 135 + 136 + log.debug("sleep done", .{}); 137 + 138 + if (burst) { 139 + log.debug("Using Burst", .{}); 140 + try bl.burst.aquire(io); 141 + return; 142 + } 126 143 } 127 144 } 128 145 129 146 try bl.static.aquire(io); 130 - return true; 147 + return; 131 148 } 132 149 }; 133 150 ··· 222 239 fn call( 223 240 cl: *Client, 224 241 url_param: []const u8, 225 - opts_param: *const RequestOptions, 242 + opts_param: RequestOptions, 226 243 ) RequestError!json.Parsed(T) { 227 244 defer cl.allocator.free(url_param); 228 - if (cl.limiter.wait(cl.io) catch return error.RateLimiterError) 229 - return Client.requestRaw(cl, T, url_param, opts_param); 230 - return error.RateLimiterError; 245 + cl.limiter.wait(cl.io) catch return error.RateLimiterError; 246 + return Client.requestRaw(cl, T, url_param, opts_param); 231 247 } 232 248 }; 233 249 234 250 return client.io.concurrent( 235 251 Wrapper.call, 236 - .{ client, url, &opts }, 252 + .{ client, url, opts }, 237 253 ); 238 254 } 239 255 ··· 241 257 client: *Client, 242 258 comptime T: type, 243 259 url: []const u8, 244 - opts: *const RequestOptions, 260 + opts: RequestOptions, 245 261 ) RequestError!json.Parsed(T) { 246 262 const uri = std.Uri.parse(url) catch |err| { 247 263 log.err("Error parsing url: {} - url = {s}", .{ err, url });
+11 -6
src/st/models.zig
··· 29 29 message: []const u8, 30 30 code: ErrorCode, 31 31 data: ?Data = null, 32 + requestId: ?[]const u8 = null, 32 33 33 34 pub const Data = struct { 34 - type: []const u8, 35 - retryAfter: u64, 36 - limitBurst: u64, 37 - limitPerSecond: u64, 38 - remaining: u64, 39 - reset: []const u8, 35 + // Rate Limit 36 + type: ?[]const u8 = null, 37 + retryAfter: ?u64 = null, 38 + limitBurst: ?u64 = null, 39 + limitPerSecond: ?u64 = null, 40 + remaining: ?u64 = null, 41 + reset: ?[]const u8 = null, 42 + // Wrong token 43 + expectedSubject: ?[]const u8 = null, 44 + receivedSubject: ?[]const u8 = null, 40 45 }; 41 46 }; 42 47