Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yankeguo/snowid
A concurrent-safe lock-free implementation of snowflake algorithm in Golang
https://github.com/yankeguo/snowid
algorithm golang id-generation
Last synced: about 22 hours ago
JSON representation
A concurrent-safe lock-free implementation of snowflake algorithm in Golang
- Host: GitHub
- URL: https://github.com/yankeguo/snowid
- Owner: yankeguo
- License: mit
- Created: 2022-09-07T01:16:48.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T10:35:31.000Z (5 months ago)
- Last Synced: 2024-08-28T11:59:18.120Z (5 months ago)
- Topics: algorithm, golang, id-generation
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# snowid
[![workflow badge](https://github.com/yankeguo/snowid/actions/workflows/go.yml/badge.svg)](https://github.com/yankeguo/snowid/actions)
[![Go Reference](https://pkg.go.dev/badge/github.com/yankeguo/snowid.svg)](https://pkg.go.dev/github.com/yankeguo/snowid)
[![codecov](https://codecov.io/gh/yankeguo/snowid/graph/badge.svg?token=MAMV3GWQ8R)](https://codecov.io/gh/yankeguo/snowid)A concurrent-safe lock-free implementation of snowflake algorithm in Golang
## Install
`go get -u github.com/yankeguo/snowid`
## Usage
```go
// create an unique identifier
id, _ := strconv.ParseUint(os.Getenv("WORKER_ID"), 10, 64)// create an instance (a sonyflake like instance)
s := snowid.New(snowflake.Options{
Epoch: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
ID: id,
Grain: time.Millisecond*10,
LeadingBit: true,
})// get a id
s.NewID()// stop and release all related resource
s.Stop()```
## Performance
Less than `1us/op` on **Apple MacBook Air (M1)**
```
goos: darwin
goarch: arm64
pkg: github.com/yankeguo/snowid
BenchmarkGenerator_NewID-8 2465515 469.5 ns/op
PASS
ok github.com/yankeguo/snowid 1.742s
```## Credits
GUO YANKE, MIT License