Skip to content

Commit

Permalink
chore: update to Zig v0.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tensorush committed Aug 1, 2024
1 parent eced635 commit 2316a94
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 81 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ name: Continuous Integration
on:
push:
branches: [main]
paths: ["**.zig"]

pull_request:
branches: [main]
paths: ["**.zig"]

workflow_dispatch:

Expand All @@ -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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
zig-cache/
.zig-cache/
zig-out/
File renamed without changes.
53 changes: 17 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`.

<details>
```sh
zig fetch --save https://github.com/tensorush/zig-backoff/archive/<git_tag_or_commit_hash>.tar.gz
```

<summary><code>build.zig.zon</code> example</summary>
- Use `backoff` dependency in `build.zig`.

```zig
.{
.name = "<name_of_your_package>",
.version = "<version_of_your_package>",
.dependencies = .{
.backoff = .{
.url = "https://github.com/tensorush/zig-backoff/archive/<git_tag_or_commit_hash>.tar.gz",
.hash = "<package_hash>",
},
},
}
```

Set `<package_hash>` to `12200000000000000000000000000000000000000000000000000000000000000000`, and Zig will provide the correct found value in an error message.

</details>

2. Add `backoff` as a module in your `build.zig`.

<details>

<summary><code>build.zig</code> example</summary>

```zig
const backoff = b.dependency("backoff", .{});
exe.addModule("Backoff", backoff.module("Backoff"));
```

</details>
```zig
const backoff_dep = b.dependency("backoff", .{
.target = target,
.optimize = optimize,
});
const backoff_mod = backoff_dep.module("Backoff");
<compile>.root_module.addImport("Backoff", backoff_mod);
```

<!-- MARKDOWN LINKS -->

[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
40 changes: 15 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,35 @@ 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,
});

const tests_run = b.addRunArtifact(tests);
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);
}
9 changes: 8 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
},
}
26 changes: 21 additions & 5 deletions src/Backoff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
Expand Down

0 comments on commit 2316a94

Please sign in to comment.