Skip to content

Fix default machine ID to respect BitsMachineID setting#83

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-default-machine-bits
Draft

Fix default machine ID to respect BitsMachineID setting#83
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-default-machine-bits

Conversation

Copy link

Copilot AI commented Feb 12, 2026

When BitsMachineID < 16 is specified without a custom MachineID function, the default lower16BitPrivateIP() returns all 16 bits from the IP address, which can exceed 1<<BitsMachineID and trigger ErrInvalidMachineID.

Changes

  • v2/sonyflake.go: Mask default machine ID to sf.bitsMachine bits when using IP-based default
  • v2/mock/mock.go: Add NewInterfaceAddrsWithIP() helper for testing with arbitrary IPs
  • v2/machine_bits_test.go: Validate masking behavior with 8, 10, and 16-bit configurations

Example

// Previously failed with ErrInvalidMachineID when IP's lower 16 bits exceeded 1024
sf, err := New(Settings{
    BitsMachineID: 10,  // Max machine ID = 1024
    // MachineID not provided, uses default IP-based ID
})
// Now succeeds - machine ID masked to lower 10 bits of IP

The fix applies only to v2 where BitsMachineID is configurable. v1 remains unchanged with its fixed 16-bit machine ID.

Original prompt

This section details on the original issue you should resolve

<issue_title>The default machine should not always have 16 bits.</issue_title>
<issue_description>So currently, if machineId isn't provided the default is to get the 16 lower bits of the private ip, but in case the user provides BitsMachineId = 10 and not the MachineId in that case also the lib will try to set the machineId to 16 lower bits of the private ip which will raise the ErrInvalidMachineID because of

sf.machine >= 1<<sf.bitsMachine {
		return nil, ErrInvalidMachineID
	}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: YoshiyukiMineo <7577673+YoshiyukiMineo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix default machine bits to handle custom BitsMachineId Fix default machine ID to respect BitsMachineID setting Feb 12, 2026
Copilot AI requested a review from YoshiyukiMineo February 12, 2026 05:45
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

Successfully merging this pull request may close these issues.

The default machine should not always have 16 bits.

2 participants