Conversation
In containerized environments such as Docker, the process ID is almost always 1, which reduces the uniqueness guarantee of generated Snowflake IDs when running multiple containers. This change introduces two ways to configure a custom process ID: 1. A static `Id.ProcessId` property that can be set programmatically at application startup. 2. A `FLAKEID_PROCESS_ID` environment variable, which is especially convenient for containerized deployments (e.g., `docker run -e FLAKEID_PROCESS_ID=7 my-app`). The process ID is resolved in the following precedence: a. The value set through `Id.ProcessId` b. The `FLAKEID_PROCESS_ID` environment variable c. The OS-assigned process ID (existing behavior) The resolved value is masked to 5 bits (range 0-31) as before. All existing comments and documentation are preserved. New XML doc comments are added for the new property and method. A new "Process ID Configuration" section is added to the README. A new `ProcessIdTests` test class covers all configuration paths.
Add .gitattributes to normalize line endings to LF across the repository. This resolves the full-file diff caused by the original CRLF line endings in Id.cs conflicting with the LF line endings in the pushed content. Re-push Id.cs with clean LF line endings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #30
In containerized environments such as Docker, the process ID of the entrypoint is almost always
1. Since FlakeId uses the process ID as part of its uniqueness composite (5 bits), every Dockerized instance shares the same process ID component, reducing the uniqueness space and risking collisions when running multiple containers.This PR introduces two ways to configure a custom process ID:
1. Static Property (
Id.ProcessId)2. Environment Variable (
FLAKEID_PROCESS_ID)Resolution Precedence
When multiple sources are configured, the process ID is resolved in the following order:
Id.ProcessIdFLAKEID_PROCESS_IDenvironment variableThe resolved value is masked to 5 bits (range 0–31) as before.
Changes
src/FlakeId/Id.css_configuredProcessIdstatic field.ProcessIdEnvironmentVariableinternal constant ("FLAKEID_PROCESS_ID").ProcessIdproperty (get/set) with XML documentation.ResolveProcessId()private method that checks the property → env var → OS PID.README.mdsrc/FlakeId.Tests/ProcessIdTests.cs(new file)ProcessId_WhenSet_IsUsedInIdGeneration— verifies the configured PID appears in generated IDs.ProcessId_WhenSetToNull_FallsBackToOsProcessId— verifies fallback to OS PID.ProcessId_WhenSetOutOfRange_IsMaskedCorrectly— verifies values exceeding 5 bits are masked (e.g.,100 & 31 = 4).ProcessId_WhenSetViaEnvironmentVariable_IsUsedInIdGeneration— verifies env var is picked up.ProcessId_WhenSetViaProperty_TakesPrecedenceOverEnvironmentVariable— verifies precedence.ProcessId_Get_ReturnsConfiguredValue— verifies the getter returns what was set.ProcessIdand env var state, and resets internals_prevId/s_processIdcaches..gitattributes(new file)* text=auto eol=lfto normalize line endings across the repository. This prevents future line-ending diff noise.Id.csDiffThe original
Id.csonmasteruses CRLF (Windows) line endings, while the content pushed through the GitHub API uses LF (Unix) line endings. This causes the diff to show every line as changed (-/+), even though most lines are identical.The actual code changes in
Id.csare only:s_configuredProcessId(with 3 comment lines)ProcessIdEnvironmentVariable(with 2 comment lines)ProcessIdget/set (with XML doc comments)CreateInternal()—Process.GetCurrentProcess().Id & ProcessIdMask→ResolveProcessId()ResolveProcessId()(with XML doc comments)That's ~40 lines of actual additions and 1 changed line out of the 278-line file. The rest is purely a CRLF → LF line-ending conversion.
The
.gitattributesfile added in this PR will prevent this issue for all future commits.Backward Compatibility
This is a non-breaking change:
ProcessIdproperty defaults tonull, meaning no configuration is required for existing users.