sony / sonyflake

A distributed unique ID generator inspired by Twitter's Snowflake
MIT License
3.96k stars 305 forks source link

New Sonyflake MachineID error #52

Open kevinmiao opened 7 months ago

kevinmiao commented 7 months ago

an error occurred when I new Sonyflake. the file is github.com/sony/sonyflake@v1.2.0/sonyflake.go.

i'm use MacOs 14.4.1.

After debugging, I found that the isPrivateIPv4() check ip error under lower16BitPrivateIP() method. i not init MachineID when i new Sonyflake, so it will run this code : var err error if st.MachineID == nil { sf.machineID, err = lower16BitPrivateIP(defaultInterfaceAddrs) } else { sf.machineID, err = st.MachineID() } if err != nil { return nil, err }

error Info ` panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x11a1e92]

goroutine 6 [running]: testing.tRunner.func1.2({0x1265300, 0x14d52d0}) /usr/local/go/src/testing/testing.go:1545 +0x238 testing.tRunner.func1() /usr/local/go/src/testing/testing.go:1548 +0x397 panic({0x1265300?, 0x14d52d0?}) /usr/local/go/src/runtime/panic.go:914 +0x21f github.com/sony/sonyflake.(*Sonyflake).NextID(0x0) /Users/pkg/mod/github.com/sony/sonyflake@v1.2.0/sonyflake.go:116 +0x32

`

YoshiyukiMineo commented 7 months ago
// NewSonyflake returns nil in the following cases:
// - Settings.StartTime is ahead of the current time.
// - Settings.MachineID returns an error.
// - Settings.CheckMachineID returns false.

So, if you would like to check the error, you should use func New(st Settings) (*Sonyflake, error) instead of NewSonyflake.