From 4ca43961f499d3bc7cbd48b7eeeef71b12670fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=B0=9A?= Date: Sat, 7 Apr 2018 14:46:40 +0800 Subject: [PATCH 1/2] Update error log for wrong node id. When custom NodeBits was set, the node number may bigger than 1023. --- snowflake.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snowflake.go b/snowflake.go index 4cc5c16..173691a 100644 --- a/snowflake.go +++ b/snowflake.go @@ -89,16 +89,16 @@ type ID int64 // IDs func NewNode(node int64) (*Node, error) { - if node < 0 || node > nodeMax { - return nil, errors.New("Node number must be between 0 and 1023") - } - // re-calc in case custom NodeBits or StepBits were set nodeMax = -1 ^ (-1 << NodeBits) nodeMask = nodeMax << StepBits stepMask = -1 ^ (-1 << StepBits) timeShift = NodeBits + StepBits nodeShift = StepBits + + if node < 0 || node > nodeMax { + return nil, errors.New("Node number must be between 0 and " + strconv.Itoa(nodeMax)) + } return &Node{ time: 0, From c64f7c8e01ebb7f7e73e4ec85e2d6bc7d67e2bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=B0=9A?= Date: Sat, 7 Apr 2018 14:51:49 +0800 Subject: [PATCH 2/2] Int64 to string. --- snowflake.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snowflake.go b/snowflake.go index 173691a..f112a05 100644 --- a/snowflake.go +++ b/snowflake.go @@ -97,7 +97,7 @@ func NewNode(node int64) (*Node, error) { nodeShift = StepBits if node < 0 || node > nodeMax { - return nil, errors.New("Node number must be between 0 and " + strconv.Itoa(nodeMax)) + return nil, errors.New("Node number must be between 0 and " + strconv.FormatInt(nodeMax, 10)) } return &Node{