diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a25f9bc..b400ed1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -3,11 +3,9 @@ name: Continuous Integration on: push: branches: [main] - paths: ["**.zig"] pull_request: branches: [main] - paths: ["**.zig"] workflow_dispatch: @@ -17,25 +15,23 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Zig - uses: goto-bus-stop/setup-zig@v2 + uses: mlugg/setup-zig@v1 - - name: Run tests - run: | - zig env - zig build test + - name: Run `test` + run: zig build test --summary all - lint: + fmt: runs-on: ubuntu-latest steps: - name: Check out repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Zig - uses: goto-bus-stop/setup-zig@v2 + uses: mlugg/setup-zig@v1 - - name: Run lints - run: zig build lint + - name: Run `fmt` + run: zig build fmt diff --git a/.gitignore b/.gitignore index e73c965..3389c86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -zig-cache/ +.zig-cache/ zig-out/ diff --git a/LICENSE.md b/LICENSE similarity index 100% rename from LICENSE.md rename to LICENSE diff --git a/README.md b/README.md index 197ca75..c67bd97 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,32 @@ -## :lizard: :raised_back_of_hand: **zig backoff** +# zig-backoff [![CI][ci-shd]][ci-url] [![LC][lc-shd]][lc-url] -### Zig implementation of the [exponential backoff algorithm](https://en.wikipedia.org/wiki/Exponential_backoff). +### Zig implementation of [exponential backoff algorithm](https://en.wikipedia.org/wiki/Exponential_backoff). ### :rocket: Usage -1. Add `backoff` as a dependency in your `build.zig.zon`. +- Add `backoff` dependency to `build.zig.zon`. -
+```sh +zig fetch --save https://github.com/tensorush/zig-backoff/archive/.tar.gz +``` - build.zig.zon example +- Use `backoff` dependency in `build.zig`. - ```zig - .{ - .name = "", - .version = "", - .dependencies = .{ - .backoff = .{ - .url = "https://github.com/tensorush/zig-backoff/archive/.tar.gz", - .hash = "", - }, - }, - } - ``` - - Set `` to `12200000000000000000000000000000000000000000000000000000000000000000`, and Zig will provide the correct found value in an error message. - -
- -2. Add `backoff` as a module in your `build.zig`. - -
- - build.zig example - - ```zig - const backoff = b.dependency("backoff", .{}); - exe.addModule("Backoff", backoff.module("Backoff")); - ``` - -
+```zig +const backoff_dep = b.dependency("backoff", .{ + .target = target, + .optimize = optimize, +}); +const backoff_mod = backoff_dep.module("Backoff"); +.root_module.addImport("Backoff", backoff_mod); +``` [ci-shd]: https://img.shields.io/github/actions/workflow/status/tensorush/zig-backoff/ci.yaml?branch=main&style=for-the-badge&logo=github&label=CI&labelColor=black [ci-url]: https://github.com/tensorush/zig-backoff/blob/main/.github/workflows/ci.yaml -[lc-shd]: https://img.shields.io/github/license/tensorush/zig-backoff.svg?style=for-the-badge&labelColor=black&kill_cache=1 -[lc-url]: https://github.com/tensorush/zig-backoff/blob/main/LICENSE.md +[lc-shd]: https://img.shields.io/github/license/tensorush/zig-backoff.svg?style=for-the-badge&labelColor=black +[lc-url]: https://github.com/tensorush/zig-backoff/blob/main/LICENSE diff --git a/build.zig b/build.zig index e8cde0f..3e00c49 100644 --- a/build.zig +++ b/build.zig @@ -2,30 +2,18 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); - const root_source_file = std.Build.LazyPath.relative("src/Backoff.zig"); + const root_source_file = b.path("src/Backoff.zig"); + const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 }; // Module _ = b.addModule("Backoff", .{ .root_source_file = root_source_file }); - // Library - const lib_step = b.step("lib", "Install library"); - - const lib = b.addStaticLibrary(.{ - .name = "backoff", - .root_source_file = root_source_file, - .target = target, - .optimize = b.standardOptimizeOption(.{}), - .version = .{ .major = 0, .minor = 1, .patch = 0 }, - }); - - const lib_install = b.addInstallArtifact(lib, .{}); - lib_step.dependOn(&lib_install.step); - b.default_step.dependOn(lib_step); - - // Tests - const tests_step = b.step("test", "Run tests"); + // Test suite + const tests_step = b.step("test", "Run test suite"); const tests = b.addTest(.{ + .target = target, + .version = version, .root_source_file = root_source_file, }); @@ -33,14 +21,16 @@ pub fn build(b: *std.Build) void { tests_step.dependOn(&tests_run.step); b.default_step.dependOn(tests_step); - // Lints - const lints_step = b.step("lint", "Run lints"); + // Formatting checks + const fmt_step = b.step("fmt", "Run formatting checks"); - const lints = b.addFmt(.{ - .paths = &.{ "src", "build.zig" }, + const fmt = b.addFmt(.{ + .paths = &.{ + "src/", + "build.zig", + }, .check = true, }); - - lints_step.dependOn(&lints.step); - b.default_step.dependOn(lints_step); + fmt_step.dependOn(&fmt.step); + b.default_step.dependOn(fmt_step); } diff --git a/build.zig.zon b/build.zig.zon index ce01690..464b707 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,5 +1,12 @@ .{ .name = "backoff", .version = "0.1.0", - .paths = .{""}, + .minimum_zig_version = "0.13.0", + .paths = .{ + "src/", + "build.zig", + "build.zig.zon", + "LICENSE", + "README.md", + }, } diff --git a/src/Backoff.zig b/src/Backoff.zig index 81a6651..e1c2c4b 100644 --- a/src/Backoff.zig +++ b/src/Backoff.zig @@ -16,7 +16,7 @@ num_tries: u64 = 0, random_factor: f64 = 0.5, multiplier: f64 = 1.5, -random: std.rand.Random, +random: std.Random, pub fn init(self: *Backoff) !void { self.timer = try std.time.Timer.start(); @@ -52,7 +52,11 @@ pub fn next(self: *Backoff) ?u64 { } test "basic" { - var prng = std.rand.DefaultPrng.init(0); + var prng = std.Random.DefaultPrng.init(blk: { + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + break :blk seed; + }); var backoff = Backoff{ .max_interval = 5 * std.time.ns_per_s, @@ -77,7 +81,11 @@ test "basic" { } test "custom_stop_time" { - var prng = std.rand.DefaultPrng.init(0); + var prng = std.Random.DefaultPrng.init(blk: { + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + break :blk seed; + }); var backoff = Backoff{ .max_elapsed_time = std.time.ns_per_us, @@ -91,7 +99,11 @@ test "custom_stop_time" { } test "custom_max_tries" { - var prng = std.rand.DefaultPrng.init(0); + var prng = std.Random.DefaultPrng.init(blk: { + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + break :blk seed; + }); var backoff = Backoff{ .random = prng.random(), @@ -108,7 +120,11 @@ test "custom_max_tries" { } test "interval_bound_overflow" { - var prng = std.rand.DefaultPrng.init(0); + var prng = std.Random.DefaultPrng.init(blk: { + var seed: u64 = undefined; + try std.posix.getrandom(std.mem.asBytes(&seed)); + break :blk seed; + }); var backoff = Backoff{ .cur_interval = std.math.maxInt(i64) / 2,