Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yeqown/snowflake
twitter's global ID generator alg implementation based c, and it provide extension for lua and CLI program.
https://github.com/yeqown/snowflake
c cli cmake library lua shared-library static-library
Last synced: 5 days ago
JSON representation
twitter's global ID generator alg implementation based c, and it provide extension for lua and CLI program.
- Host: GitHub
- URL: https://github.com/yeqown/snowflake
- Owner: yeqown
- License: mit
- Created: 2019-04-02T02:07:11.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-08-25T01:57:29.000Z (over 1 year ago)
- Last Synced: 2024-11-07T18:53:58.795Z (about 2 months ago)
- Topics: c, cli, cmake, library, lua, shared-library, static-library
- Language: C
- Homepage:
- Size: 40 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# snowflake
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
### Goal
- [x] developing a `c` library package.
- [x] quickly use as a CLI application.
- [x] extension to `lua`.
- [ ] extension to `go`.### Installation
There are three ways to use `snowflake`:
- [Library](#library)
- [CLI application](#cli-application)
- [Lua extension](#lua-extension)#### Library
In this way, you can use `snowflake` as a static library in your project.
```bash
mkdir build && cd build
cmake ..make snowflake_lib
```Then you can copy the `build/libsnowflake.a` to your project or install it to your system manually.
#### CLI application
In this way, you can use `snowflake` as a CLI application.
```bash
mkdir build && cd build
cmake ..make snowflake_cli
```Then you can copy the `build/cmd/snowflake/snowflake` to your system path.
### Lua extension
In this way, you can use `snowflake` as a lua extension.
```bash
mkdir build && cd build
cmake ..make snowflake_lua
```Then you can copy the `build/contrib/lua/snowflake.so` to your lua `package.cpath`.
### Generation process
Each time you generate an ID, it works, like this.
* A `timestamp` with millisecond precision is stored using 41 bits of the ID.
* Then the `machineID` is added in subsequent bits.
* Then the `counter` is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.### References
* [twitter-snowflake](https://github.com/twitter-archive/snowflake)
* thanks to [bwmarrin/snowflake](https://github.com/bwmarrin/snowflake)