Minor comments updates
This commit is contained in:
parent
7dcf86e66a
commit
0684ace7ee
18
snowflake.go
18
snowflake.go
@ -48,7 +48,14 @@ func (j JSONSyntaxError) Error() string {
|
|||||||
return fmt.Sprintf("invalid snowflake ID %q", string(j.original))
|
return fmt.Sprintf("invalid snowflake ID %q", string(j.original))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a map for decoding Base58. This speeds up the process tremendously.
|
// ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte
|
||||||
|
var ErrInvalidBase58 = errors.New("invalid base58")
|
||||||
|
|
||||||
|
// ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte
|
||||||
|
var ErrInvalidBase32 = errors.New("invalid base32")
|
||||||
|
|
||||||
|
// Create maps for decoding Base58/Base32.
|
||||||
|
// This speeds up the process tremendously.
|
||||||
func init() {
|
func init() {
|
||||||
|
|
||||||
for i := 0; i < len(encodeBase58Map); i++ {
|
for i := 0; i < len(encodeBase58Map); i++ {
|
||||||
@ -68,12 +75,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte
|
|
||||||
var ErrInvalidBase58 = errors.New("invalid base58")
|
|
||||||
|
|
||||||
// ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte
|
|
||||||
var ErrInvalidBase32 = errors.New("invalid base32")
|
|
||||||
|
|
||||||
// A Node struct holds the basic information needed for a snowflake generator
|
// A Node struct holds the basic information needed for a snowflake generator
|
||||||
// node
|
// node
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -128,6 +129,9 @@ func NewNode(node int64) (*Node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate creates and returns a unique snowflake ID
|
// Generate creates and returns a unique snowflake ID
|
||||||
|
// To help guarantee uniqueness
|
||||||
|
// - Make sure your system is keeping accurate system time
|
||||||
|
// - Make sure you never have multiple nodes running with the same node ID
|
||||||
func (n *Node) Generate() ID {
|
func (n *Node) Generate() ID {
|
||||||
|
|
||||||
n.mu.Lock()
|
n.mu.Lock()
|
||||||
|
@ -6,6 +6,21 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// check if Generate will create duplicate IDs
|
||||||
|
func TestGenerateDuplicateID(t *testing.T) {
|
||||||
|
|
||||||
|
node, _ := NewNode(1)
|
||||||
|
|
||||||
|
var x, y ID
|
||||||
|
for i := 0; i < 100000000; i++ {
|
||||||
|
y = node.Generate()
|
||||||
|
if x == y {
|
||||||
|
t.Errorf("x(%d) & y(%d) are the same", x, y)
|
||||||
|
}
|
||||||
|
x = y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// I feel like there's probably a better way
|
// I feel like there's probably a better way
|
||||||
func TestRace(t *testing.T) {
|
func TestRace(t *testing.T) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user