Skip to content

flowerinthenight/zbackoff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Main

zbackoff implements jittered backoff. Useful when retrying operations that can potentially fail (i.e. network calls). The implementation is based on this article from the AWS Architecture Blog.

You can use it like so:

const std = @import("std");
const zbackoff = @import("zbackoff");

fn funcThatFails() !u64 {
    _ = try std.time.Instant.now();
    std.debug.print("funcThatFails()\n", .{});
    return error.UnwantedResult1;
}

pub fn main() void {
    var bo = zbackoff.Backoff{};
    for (0..3) |_| {
        const result = funcThatFails() catch {
            std.time.sleep(bo.pause());
            continue;
        };
        _ = result;
        break;
    }
}