Skip to content

Commit f002a4c

Browse files
committed
Make max backoff configurable
1 parent 434b02c commit f002a4c

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/argparse.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ fn setFromCli(
106106
std.process.exit(1);
107107
}
108108
switch (t) {
109-
// TODO
110-
.int, .float, .@"enum" => comptime unreachable,
109+
.int => @field(result, field.name) =
110+
try std.fmt.parseInt(field.type, args[i], 0),
111111
.pointer => @field(
112112
result,
113113
field.name,
114114
) = try allocator.dupe(u8, args[i]),
115115
.bool => comptime unreachable,
116116
else => @compileError(
117-
"Disallowed struct field type.",
117+
"Disallowed or unimplemented struct field type.",
118118
),
119119
}
120120
}
@@ -153,13 +153,15 @@ fn setFromEnv(
153153
@field(result, field.name) = value.len > 0 and
154154
!std.ascii.eqlIgnoreCase(value, "false");
155155
},
156-
// TODO
157-
.int, .float, .@"enum" => comptime unreachable,
156+
.int => @field(result, field.name) =
157+
try std.fmt.parseInt(field.type, entry.value_ptr.*, 0),
158158
.pointer => @field(
159159
result,
160160
field.name,
161161
) = try allocator.dupe(u8, entry.value_ptr.*),
162-
else => @compileError("Disallowed struct field type."),
162+
else => @compileError(
163+
"Disallowed or unimplemented struct field type.",
164+
),
163165
}
164166
seen_field.* = true;
165167
}

src/main.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const Args = struct {
4444
languages_output_file: ?[]const u8 = null,
4545
overview_template: ?[]const u8 = null,
4646
languages_template: ?[]const u8 = null,
47+
max_backoff: usize = 600,
4748

4849
const Self = @This();
4950

@@ -212,7 +213,7 @@ pub fn main() !void {
212213
std.log.info("Collecting statistics from GitHub API", .{});
213214
var client: HttpClient = try .init(allocator, api_key);
214215
defer client.deinit();
215-
stats = try Statistics.init(&client, allocator);
216+
stats = try Statistics.init(&client, allocator, args.max_backoff);
216217
} else unreachable;
217218
defer stats.deinit(allocator);
218219

src/statistics.zig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ const Language = struct {
100100
}
101101
};
102102

103-
pub fn init(client: *HttpClient, allocator: std.mem.Allocator) !Statistics {
103+
pub fn init(
104+
client: *HttpClient,
105+
allocator: std.mem.Allocator,
106+
max_backoff: usize,
107+
) !Statistics {
104108
var arena = std.heap.ArenaAllocator.init(allocator);
105109
defer arena.deinit();
106110

107111
var self: Statistics = try getRepos(allocator, &arena, client);
108112
errdefer self.deinit(allocator);
109-
try self.getLinesChanged(&arena, client);
113+
try self.getLinesChanged(&arena, client, max_backoff);
110114
return self;
111115
}
112116

@@ -474,6 +478,7 @@ fn getLinesChanged(
474478
self: *Statistics,
475479
arena: *std.heap.ArenaAllocator,
476480
client: *HttpClient,
481+
max_backoff: usize,
477482
) !void {
478483
const T = struct {
479484
repo: *Repository,
@@ -515,7 +520,7 @@ fn getLinesChanged(
515520
// Exponential backoff (in expectation) with jitter
516521
item.delay +=
517522
std.crypto.random.intRangeAtMost(i64, 2, item.delay);
518-
item.delay = @min(item.delay, 600);
523+
item.delay = @min(item.delay, max_backoff);
519524
try q.add(item);
520525
},
521526
else => |status| {

0 commit comments

Comments
 (0)