Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/karlseguin/singleflight.zig
Duplicate function call suppression for Zig
https://github.com/karlseguin/singleflight.zig
zig zig-library
Last synced: 3 months ago
JSON representation
Duplicate function call suppression for Zig
- Host: GitHub
- URL: https://github.com/karlseguin/singleflight.zig
- Owner: karlseguin
- License: mit
- Created: 2023-06-03T10:20:06.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-26T01:28:53.000Z (5 months ago)
- Last Synced: 2024-06-26T02:42:42.677Z (5 months ago)
- Topics: zig, zig-library
- Language: Zig
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
Duplicate function call suppression for Zig
```zig
const singleflight = @import("singleflight");// group is thread-safe
const group = singleflight.Group(*User).init(allocator);
defer group.deinit();// multiple threads can call group.load on the same key
const result = try group.load(UserLoadState, "user:4", &loadUser, UserLoadState{.id = 4});// result.value is the value returned by loadUser
// result.caller is true when this is the thread that called loadUser
// result.last is true when all waiting threads for this key have unblocked
...const UserLoadState = struct {
id: u32,
}fn loadUser(state: UserLoadState, _key: []const u8) !User {
// load the user, using either the app-specific state
// or the key
// ...
return user;
}
```An application-specific "state" is provided to the `group.load` function and then passed back to the callback function. This allows the application to pass itself information necessary to load the object.
This library is not a cache. Assume thread 1, thread 2 and thread 3 all attempt to load "user:1". It is possible for one, two or all three threads to call `loadUser`. This is more likely when the callback, `loadUser` in this case, executes quickly. This happens because the Singleflight group tracks threads waiting for the result and cleans itself when the last blocked thread gets the result (on a per-key basis). Thread 1 can call `loadUser`, get the result before thread 2's intent in the key is registered. In this case, thread 1 will clean up the group and return the value.
In other words, this library is not a cache. See [cache.zig](https://github.com/karlseguin/cache.zig) if you want a cache (combining the two libraries makes sense).