-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d40dba4
Showing
12 changed files
with
616 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
# Created by https://www.toptal.com/developers/gitignore/api/go,visualstudiocode | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=go,visualstudiocode | ||
|
||
### Go ### | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
### Go Patch ### | ||
/vendor/ | ||
/Godeps/ | ||
|
||
### VisualStudioCode ### | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
*.code-workspace | ||
|
||
# Local History for Visual Studio Code | ||
.history/ | ||
|
||
### VisualStudioCode Patch ### | ||
# Ignore all local history of files | ||
.history | ||
.ionide | ||
|
||
# Support for Project snippet scope | ||
!.vscode/*.code-snippets | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 HotPotatoC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Snowflake | ||
|
||
Dead simple and fast [Twitter's snowflake](https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake) id generator in Go. | ||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/HotPotatoC/snowflake | ||
``` | ||
|
||
## Usage | ||
|
||
1. Generating a snowflake id | ||
|
||
```go | ||
machineID := uint64(1) | ||
sf := snowflake.New(machineID) | ||
|
||
id := sf.NextID() | ||
fmt.Println(id) | ||
// 1292053924173320192 | ||
|
||
// or | ||
|
||
id = snowflake.New(machineID).NextID() | ||
fmt.Println(id) | ||
// 1292053924173320192 | ||
``` | ||
|
||
2. Parsing a snowflake id | ||
|
||
```go | ||
parsed := snowflake.Parse(1292053924173320192) | ||
|
||
fmt.Printf("Timestamp: %d\n", parsed.Timestamp) // 1640942460724 | ||
fmt.Printf("Sequence: %d\n", parsed.Sequence) // 0 | ||
fmt.Printf("Machine ID: %d\n", parsed.Discriminator) // 1 | ||
``` | ||
|
||
3. Generating a snowflake ID with 2 discriminator fields | ||
|
||
```go | ||
machineID := uint64(1) | ||
processID := uint64(os.Getpid()) | ||
sf := snowflake.New2(machineID, processID) | ||
|
||
id := sf.NextID() | ||
fmt.Println(id) | ||
// 1292058526532767744 | ||
|
||
// or | ||
|
||
id = snowflake.New2(machineID, processID).NextID() | ||
fmt.Println(id) | ||
// 1292058526532767744 | ||
``` | ||
|
||
4. Parsing a snowflake id with 2 discriminator fields | ||
|
||
```go | ||
parsed := snowflake.Parse2(1292062458947571712) | ||
|
||
fmt.Printf("Timestamp: %d\n", parsed.Timestamp) // 1640944495572 | ||
fmt.Printf("Sequence: %d\n", parsed.Sequence) // 0 | ||
fmt.Printf("Machine ID: %d\n", parsed.Discriminator1) // 1 | ||
fmt.Printf("Process ID: %d\n", parsed.Discriminator2) // 24 | ||
``` | ||
|
||
## Performance | ||
|
||
> Benched on Windows 10 - WSL 2 Ubuntu, Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz and 12GB of memory | ||
```bash | ||
goos: linux | ||
goarch: amd64 | ||
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz | ||
BenchmarkNewID | ||
BenchmarkNewID/github.com/HotPotatoC/snowflake | ||
BenchmarkNewID/github.com/HotPotatoC/snowflake-8 14012617 87.52 ns/op 0 B/op 0 allocs/op | ||
BenchmarkNewID/github.com/bwmarrin/snowflake | ||
BenchmarkNewID/github.com/bwmarrin/snowflake-8 4918552 244.3 ns/op 0 B/op 0 allocs/op | ||
BenchmarkNewID/github.com/godruoyi/go-snowflake | ||
BenchmarkNewID/github.com/godruoyi/go-snowflake-8 4916791 245.8 ns/op 0 B/op 0 allocs/op | ||
PASS | ||
ok command-line-arguments 4.230s | ||
``` | ||
|
||
Disclaimer: Benchmark results may be faster than other implementations. But I do not guarantee that this library is the safest snowflake id generator. | ||
|
||
## Support | ||
|
||
<a href="https://www.buymeacoffee.com/hotpotato" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/HotPotatoC/snowflake" | ||
) | ||
|
||
func main() { | ||
machineID := uint64(1) | ||
processID := uint64(24) | ||
sf := snowflake.New2(machineID, processID) | ||
|
||
id := sf.NextID() | ||
fmt.Println(id) | ||
// 1292062458947571712 | ||
|
||
// or | ||
id = snowflake.New2(machineID, processID).NextID() | ||
fmt.Println(id) | ||
// 1292062458947571712 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/HotPotatoC/snowflake" | ||
) | ||
|
||
func main() { | ||
machineID := uint64(1) | ||
sf := snowflake.New(machineID) | ||
|
||
id := sf.NextID() | ||
fmt.Println(id) | ||
|
||
// or | ||
id = snowflake.New(machineID).NextID() | ||
fmt.Println(id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/HotPotatoC/snowflake" | ||
) | ||
|
||
func main() { | ||
parsed := snowflake.Parse2(1292062458947571712) | ||
|
||
fmt.Printf("Timestamp: %d\n", parsed.Timestamp) // 1640944495572 | ||
fmt.Printf("Sequence: %d\n", parsed.Sequence) // 0 | ||
fmt.Printf("Machine ID: %d\n", parsed.Discriminator1) // 1 | ||
fmt.Printf("Process ID: %d\n", parsed.Discriminator2) // 24 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/HotPotatoC/snowflake" | ||
) | ||
|
||
func main() { | ||
parsed := snowflake.Parse(1292053924173320192) | ||
|
||
fmt.Printf("Timestamp: %d\n", parsed.Timestamp) // 1640942460724 | ||
fmt.Printf("Sequence: %d\n", parsed.Sequence) // 0 | ||
fmt.Printf("Machine ID: %d\n", parsed.Discriminator) // 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/HotPotatoC/snowflake | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/bwmarrin/snowflake v0.3.0 // indirect | ||
github.com/godruoyi/go-snowflake v0.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= | ||
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= | ||
github.com/godruoyi/go-snowflake v0.0.1 h1:x4Kb7s5MyZDeHasNbm630gBOggJdl6Fq1JDWGntH/ew= | ||
github.com/godruoyi/go-snowflake v0.0.1/go.mod h1:6JXMZzmleLpSK9pYpg4LXTcAz54mdYXTeXUvVks17+4= |
Oops, something went wrong.