Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetLocalIpToInt本地ip转负数导致panic异常问题 #1

Open
404name opened this issue Apr 17, 2023 · 0 comments
Open

GetLocalIpToInt本地ip转负数导致panic异常问题 #1

404name opened this issue Apr 17, 2023 · 0 comments

Comments

@404name
Copy link

404name commented Apr 17, 2023

用的一个项目使用了这个包,启动的时候异常了,排查到问题是本地ip可能转换得到负数,除余1023仍然得到负数,然后snowflake约定初始值是0-1023导致panic项目直接启动不了。

// localIp得到负数情况下panic代码 
Node, err = snowflake.NewNode(int64(localIp) % 1023)
if err != nil {
     panic(err)
}

// 雪花算法抛出error逻辑
if n.node < 0 || n.node > n.nodeMax {
    return nil, errors.New("Node number must be between 0 and " + strconv.FormatInt(n.nodeMax, 10))
}

如果确认是负数引起的问题,考虑解决方案

  1. func GetLocalIpToInt() (uint32, error) 参考
  2. 直接在snowflake.NewNode入口处添加localIp转正数的逻辑
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant