tangled
alpha
login
or
join now
altagos.dev
/
space
0
fork
atom
A SpaceTraders Agent
0
fork
atom
overview
issues
pulls
pipelines
wait call got stuck
altagos.dev
4 months ago
ca95640c
febe319b
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+39
-18
2 changed files
expand all
collapse all
unified
split
src
st
http.zig
models.zig
+28
-12
src/st/http.zig
···
96
96
return l.semaphor.wait(io);
97
97
}
98
98
99
99
-
pub fn timeToReset(l: *Limiter, io: Io) i64 {
99
99
+
pub fn timeToReset(l: *Limiter, io: Io) Io.Duration {
100
100
if (l.time) |t| {
101
101
-
return t.durationTo(Io.Clock.now(.real, io) catch return 0).raw.toMilliseconds();
101
101
+
return t.durationTo(Io.Clock.now(.real, io) catch return .zero);
102
102
}
103
103
-
return 0;
103
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
115
-
pub fn wait(bl: *BurstyLimiter, io: Io) !bool {
115
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
123
-
return true;
123
123
+
return;
124
124
} else {
125
125
log.warn("No request available, waiting", .{});
126
126
+
127
127
+
var static = bl.static.checkReset(io);
128
128
+
var burst = bl.burst.checkReset(io);
129
129
+
130
130
+
while (!static and !burst and !bl.static.available(io)) {
131
131
+
try io.sleep(bl.static.timeToReset(io), .real);
132
132
+
static = bl.static.checkReset(io);
133
133
+
burst = bl.burst.checkReset(io);
134
134
+
}
135
135
+
136
136
+
log.debug("sleep done", .{});
137
137
+
138
138
+
if (burst) {
139
139
+
log.debug("Using Burst", .{});
140
140
+
try bl.burst.aquire(io);
141
141
+
return;
142
142
+
}
126
143
}
127
144
}
128
145
129
146
try bl.static.aquire(io);
130
130
-
return true;
147
147
+
return;
131
148
}
132
149
};
133
150
···
222
239
fn call(
223
240
cl: *Client,
224
241
url_param: []const u8,
225
225
-
opts_param: *const RequestOptions,
242
242
+
opts_param: RequestOptions,
226
243
) RequestError!json.Parsed(T) {
227
244
defer cl.allocator.free(url_param);
228
228
-
if (cl.limiter.wait(cl.io) catch return error.RateLimiterError)
229
229
-
return Client.requestRaw(cl, T, url_param, opts_param);
230
230
-
return error.RateLimiterError;
245
245
+
cl.limiter.wait(cl.io) catch return error.RateLimiterError;
246
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
236
-
.{ client, url, &opts },
252
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
244
-
opts: *const RequestOptions,
260
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
32
+
requestId: ?[]const u8 = null,
32
33
33
34
pub const Data = struct {
34
34
-
type: []const u8,
35
35
-
retryAfter: u64,
36
36
-
limitBurst: u64,
37
37
-
limitPerSecond: u64,
38
38
-
remaining: u64,
39
39
-
reset: []const u8,
35
35
+
// Rate Limit
36
36
+
type: ?[]const u8 = null,
37
37
+
retryAfter: ?u64 = null,
38
38
+
limitBurst: ?u64 = null,
39
39
+
limitPerSecond: ?u64 = null,
40
40
+
remaining: ?u64 = null,
41
41
+
reset: ?[]const u8 = null,
42
42
+
// Wrong token
43
43
+
expectedSubject: ?[]const u8 = null,
44
44
+
receivedSubject: ?[]const u8 = null,
40
45
};
41
46
};
42
47