Skip to content

fix(ids): keep time-ordered IDs increasing within a millisecond - #360

Open
lx3133584 wants to merge 1 commit into
skudra-go:mainfrom
lx3133584:fix/monotonic-time-ordered-ids
Open

lx3133584 wants to merge 1 commit into
skudra-go:mainfrom
lx3133584:fix/monotonic-time-ordered-ids

Conversation

@lx3133584

Copy link
Copy Markdown
Contributor

What and why

Closes #353.

Make ids.UUIDv7, ids.UUID and ids.ULID strictly increase within one process, including in a tight same-millisecond loop.

A shared, mutex-guarded clock returns a 12-bit sub-millisecond fraction in units of 256 ns, advancing the previous pair when the clock does not. The entropy read stays outside the lock. UUIDv7 places the fraction in rand_a, following RFC 9562 section 6.2 method 3; ULID places it in the first 12 bits after the timestamp. Their public signatures, versions, variants, text lengths and remaining random bits are unchanged.

Decision 179 records the behavior and amends decision 168. The skill now states the within-millisecond guarantee.

Checklist

  • The title follows Conventional Commits (feat:, fix:, docs: ...), since it becomes the squashed commit
  • The branch is rebased on main (git pull --rebase origin main), with no merge commits
  • Tests cover the change, and at least one fails without it
  • make fmt, make lint and make test pass
  • make test-pg passes, if the change reaches the database (and the other dialects' targets, if it touches them)
  • docs/api.txt is updated with go test ./tests/api -update, if the exported API changed on purpose (no exported API change)
  • Docs are updated here or in ant-docs, if behaviour changed (decision 179 and the skill)

go test -race -count=1 ./ids passes, including concurrent time-pair generation.

A local probe generated 1,000,000 UUIDv7 values per run, three runs against each implementation. origin/main produced 499,111–499,750 inversions per run at 434.9–438.1 ns/id; this branch produced zero inversions at 447.8–474.4 ns/id.

The skill doccheck type-checks all 14 samples. It still reports the pre-existing gofmt alignment issue in the ant.Raw[int] example: origin/main reports it at line 296 and this branch at line 297 after the added documentation line.

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.

ids.UUIDv7 and ids.ULID order keys only to the millisecond, so a burst of keys fills an index like random UUIDs

1 participant