mirror of
https://github.com/godruoyi/go-snowflake.git
synced 2025-12-23 05:25:15 +00:00
❄ Change readme and fixed test case
This commit is contained in:
62
.github/workflows/test.yaml
vendored
Normal file
62
.github/workflows/test.yaml
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
name: run tests
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.15.x
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run linters
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
with:
|
||||
version: v1.29
|
||||
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [1.15.x]
|
||||
platform: [ubuntu-latest, macos-latest, windows-latest]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Install Go
|
||||
if: success()
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run tests
|
||||
run: go test -v -covermode=count
|
||||
|
||||
coverage:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install Go
|
||||
if: success()
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.15.x
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Calc coverage
|
||||
run: |
|
||||
go test -v -covermode=count -coverprofile=coverage.out
|
||||
- name: Convert coverage.out to coverage.lcov
|
||||
uses: jandelgado/gcov2lcov-action@v1.0.6
|
||||
- name: Coveralls
|
||||
uses: coverallsapp/github-action@v1.1.2
|
||||
with:
|
||||
github-token: ${{ secrets.github_token }}
|
||||
path-to-lcov: coverage.lcov
|
||||
|
||||
29
readme.md
29
readme.md
@@ -3,26 +3,17 @@
|
||||
<image src="https://www.pngkey.com/png/full/105-1052235_snowflake-png-transparent-background-snowflake-with-clear-background.png" width="250" height="250">
|
||||
</p>
|
||||
<p align="center">An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).</p>
|
||||
<!-- <p align="center">
|
||||
<a href="https://scrutinizer-ci.com/g/godruoyi/go-snowflake/">
|
||||
<image src="https://scrutinizer-ci.com/g/godruoyi/go-snowflake/badges/quality-score.png?b=master" alt="quality score">
|
||||
<p align="center">
|
||||
<a href="https://github.com/godruoyi/go-snowflake/actions?workflow=test%20and%20build">
|
||||
<image src="https://github.com/godruoyi/go-snowflake/actions?workflow=test%20and%20build" alt="test">
|
||||
</a>
|
||||
<a href="https://github.com/godruoyi/go-snowflake">
|
||||
<image src="https://github.styleci.io/repos/201936013/shield?branch=master" alt="godruoyi go-snowflake">
|
||||
<a href="https://goreportcard.com/report/github.com/godruoyi/go-snowflake">
|
||||
<image src="https://goreportcard.com/badge/github.com/godruoyi/go-snowflake" alt="Go report">
|
||||
</a>
|
||||
<a href="https://github.com/godruoyi/go-snowflake">
|
||||
<image src="https://poser.pugx.org/godruoyi/go-snowflake/license" alt="License">
|
||||
<a href="https://coveralls.io/repos/github/godruoyi/go-snowflake/badge.svg?branch=master">
|
||||
<image src="https://coveralls.io/repos/github/godruoyi/go-snowflake/badge.svg?branch=master" alt="Coverage status">
|
||||
</a>
|
||||
<a href="https://packagist.org/packages/godruoyi/go-snowflake">
|
||||
<image src="https://poser.pugx.org/godruoyi/go-snowflake/v/stable" alt="Packagist Version">
|
||||
</a>
|
||||
<a href="https://scrutinizer-ci.com/g/godruoyi/go-snowflake/">
|
||||
<image src="https://scrutinizer-ci.com/g/godruoyi/go-snowflake/badges/build.png?b=master" alt="build passed">
|
||||
</a>
|
||||
<a href="https://github.com/godruoyi/go-snowflake">
|
||||
<image src="https://poser.pugx.org/godruoyi/go-snowflake/downloads" alt="Total Downloads">
|
||||
</a>
|
||||
</p> -->
|
||||
</p>
|
||||
</div>
|
||||
|
||||
## Description
|
||||
@@ -35,7 +26,7 @@ Snowflake is a network service for generating unique ID numbers at high scale wi
|
||||
|
||||
* The first bit is unused sign bit.
|
||||
* The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
|
||||
* The 10 bits machineID, and max value is 2^10 -1 = 1023.
|
||||
* The 10 bits machineID(5 bit workid + 5 bit datacenter id), max value is 2^10 -1 = 1023.
|
||||
* The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond.
|
||||
* The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it.
|
||||
|
||||
@@ -180,8 +171,6 @@ snowflake.SetSequenceResolver(yourSequenceNumber)
|
||||
snowflake.ID()
|
||||
```
|
||||
|
||||
And you can use closure:
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
@@ -194,9 +194,16 @@ func TestParseID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSID_GenerateTime(t *testing.T) {
|
||||
sid := snowflake.ParseID(snowflake.ID())
|
||||
snowflake.SetSequenceResolver(snowflake.AtomicResolver)
|
||||
a, e := snowflake.NextID()
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
return
|
||||
}
|
||||
|
||||
sid := snowflake.ParseID(a)
|
||||
|
||||
if sid.GenerateTime().UTC().Second() != time.Now().UTC().Second() {
|
||||
t.Error("The id generate time should be equal")
|
||||
t.Error("The id generate time should be equal current time")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user