1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Thread-local cached shard hint for producer→shard routing.
//!
//! Producers call [`current_shard_hint`] to pick an injector shard. The
//! hint is derived from the producer thread's ID, hashed once and cached
//! for the lifetime of the thread — a thread's ID never changes, so there
//! is nothing to refresh. The goal is purely that a given producer thread
//! routes consistently to the same shard, which keeps that producer's
//! traffic isolated to one injector and minimises cross-shard contention
//! when many producers run concurrently (the dominant workload).
//!
//! This previously queried the running CPU (`sched_getcpu` on Linux,
//! `GetCurrentProcessorNumber` on Windows) for geographic locality, but
//! that bought little over thread-ID stickiness — workers steal across
//! shards regardless — while costing a syscall/vDSO call and a platform
//! dependency (`libc`/`winapi`). Hashing the thread ID is stable,
//! allocation-free, identical on every target, and works under miri
//! (which does not support `sched_getcpu`).
use Cell;
thread_local!
/// Return a stable shard hint for the calling thread. Derived once from
/// the thread ID and cached for the thread's lifetime.
pub
/// Hash the current thread's `ThreadId` into a `usize`. Stable per
/// thread, so a given producer always routes to the same shard.