@cdlab996/genid 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-PRESENT WuChenDi<https://github.com/WuChenDi>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,295 @@
1
+ # @cdlab996/genid
2
+
3
+ 一个基于 Snowflake 算法的高性能分布式唯一 ID 生成器。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **漂移算法**:高并发场景下性能优异
8
+ - 🔄 **时钟回拨处理**:优雅处理时钟回拨,不阻塞 ID 生成
9
+ - ⚙️ **灵活配置**:支持自定义位长度分配
10
+ - 📊 **性能监控**:内置统计和调试功能
11
+
12
+ ## 架构设计
13
+
14
+ ### 核心流程
15
+
16
+ ```mermaid
17
+ graph TB
18
+ A[开始生成 ID] --> B{是否处于漂移状态?}
19
+
20
+ B -->|否| C[正常路径]
21
+ B -->|是| D[漂移路径]
22
+
23
+ C --> E{检测时钟}
24
+ E -->|时钟回拨| F[使用保留序列号 0-4]
25
+ E -->|时间前进| G[重置序列号]
26
+ E -->|同一毫秒| H{序列号是否溢出?}
27
+
28
+ H -->|否| I[序列号+1 正常生成]
29
+ H -->|是| J[进入漂移状态 时间戳+1]
30
+
31
+ D --> K{检测时间}
32
+ K -->|时间追上| L[退出漂移 恢复正常]
33
+ K -->|超过最大漂移| M[等待下一毫秒 退出漂移]
34
+ K -->|继续漂移| N{序列号是否溢出?}
35
+
36
+ N -->|否| O[使用当前序列号]
37
+ N -->|是| P[时间戳+1 重置序列号]
38
+
39
+ F --> Q[计算 ID]
40
+ G --> Q
41
+ I --> Q
42
+ J --> Q
43
+ L --> Q
44
+ M --> Q
45
+ O --> Q
46
+ P --> Q
47
+
48
+ Q --> R[更新统计]
49
+ R --> S[返回 ID]
50
+
51
+ style B fill:#fff4e6
52
+ style E fill:#e8f5e9
53
+ style K fill:#fce4ec
54
+ style Q fill:#f3e5f5
55
+ style S fill:#c8e6c9
56
+ ```
57
+
58
+ ### ID 结构(64-bit)
59
+
60
+ ```
61
+ |------------ 时间戳 ------------|-- 工作节点 ID --|-- 序列号 --|
62
+ 42-52 bits 1-15 bits 3-21 bits
63
+ ```
64
+
65
+ **位分配示例(默认配置):**
66
+
67
+ - 时间戳:52 bits(可用约 139 年)
68
+ - 工作节点 ID:6 bits(支持 64 个节点)
69
+ - 序列号:6 bits(每毫秒 59 个 ID,5-63)
70
+
71
+ **序列号分配:**
72
+
73
+ - `0-4`:保留用于时钟回拨
74
+ - `5-maxSeqNumber`:正常使用
75
+
76
+ ## 快速开始
77
+
78
+ ```typescript
79
+ import { GenidOptimized } from '@cdlab996/genid'
80
+
81
+ // 创建实例(每个 Worker/进程使用不同的 workerId)
82
+ const genid = new GenidOptimized({ workerId: 1 })
83
+
84
+ // 生成 ID
85
+ const id = genid.nextId()
86
+ console.log(id) // 123456789012345
87
+ ```
88
+
89
+ ## API 参考
90
+
91
+ ### 构造函数
92
+
93
+ ```typescript
94
+ new GenidOptimized(options: GenidOptions)
95
+ ```
96
+
97
+ **配置选项**
98
+
99
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
100
+ |------|------|------|--------|------|
101
+ | `workerId` | number | ✅ | - | 工作节点 ID(范围:0 到 2^workerIdBitLength-1) |
102
+ | `method` | GenidMethod | ❌ | `DRIFT` | 算法类型:`DRIFT` 或 `TRADITIONAL` |
103
+ | `baseTime` | number | ❌ | `1577836800000` | 起始时间戳(毫秒,默认:2020-01-01) |
104
+ | `workerIdBitLength` | number | ❌ | `6` | 工作节点 ID 位数(1-15) |
105
+ | `seqBitLength` | number | ❌ | `6` | 序列号位数(3-21) |
106
+ | `maxSeqNumber` | number | ❌ | `2^seqBitLength-1` | 最大序列号 |
107
+ | `minSeqNumber` | number | ❌ | `5` | 最小序列号(0-4 保留用于时钟回拨) |
108
+ | `topOverCostCount` | number | ❌ | `2000` | 最大漂移次数 |
109
+
110
+ ### 生成 ID
111
+
112
+ #### `nextId()`
113
+
114
+ 返回 Number 或 BigInt 类型的 ID(自动选择)
115
+
116
+ ```typescript
117
+ const id = genid.nextId()
118
+ ```
119
+
120
+ #### `nextNumber()`
121
+
122
+ 返回 Number 类型的 ID(超出安全范围会抛出错误)
123
+
124
+ ```typescript
125
+ const id = genid.nextNumber()
126
+ ```
127
+
128
+ #### `nextBigId()`
129
+
130
+ 返回 BigInt 类型的 ID
131
+
132
+ ```typescript
133
+ const id = genid.nextBigId()
134
+ ```
135
+
136
+ #### `nextBatch(count, asBigInt?)`
137
+
138
+ 批量生成 ID
139
+
140
+ ```typescript
141
+ const ids = genid.nextBatch(100) // 生成 100 个 ID
142
+ const bigIds = genid.nextBatch(100, true) // 生成 100 个 BigInt ID
143
+ ```
144
+
145
+ ### 解析 ID
146
+
147
+ #### `parse(id)`
148
+
149
+ 解析 ID,提取组成部分
150
+
151
+ ```typescript
152
+ const info = genid.parse(id)
153
+ console.log(info)
154
+ // {
155
+ // timestamp: Date, // 生成时间
156
+ // timestampMs: 1609459200000,
157
+ // workerId: 1, // 工作节点 ID
158
+ // sequence: 42 // 序列号
159
+ // }
160
+ ```
161
+
162
+ ### 统计与配置
163
+
164
+ #### `getStats()`
165
+
166
+ 获取生成器统计信息
167
+
168
+ ```typescript
169
+ const stats = genid.getStats()
170
+ // {
171
+ // totalGenerated: 1000, // 总生成数量
172
+ // overCostCount: 10, // 漂移次数
173
+ // turnBackCount: 2, // 时钟回拨次数
174
+ // uptimeMs: 60000, // 运行时间
175
+ // avgPerSecond: 16, // 每秒平均生成量
176
+ // currentState: 'NORMAL' // 当前状态
177
+ // }
178
+ ```
179
+
180
+ #### `getConfig()`
181
+
182
+ 获取配置信息
183
+
184
+ ```typescript
185
+ const config = genid.getConfig()
186
+ // {
187
+ // method: 'DRIFT',
188
+ // workerId: 1,
189
+ // workerIdRange: '0-63',
190
+ // sequenceRange: '5-63',
191
+ // idsPerMillisecond: 59,
192
+ // baseTime: Date,
193
+ // timestampBits: 52,
194
+ // workerIdBits: 6,
195
+ // sequenceBits: 6
196
+ // }
197
+ ```
198
+
199
+ #### `resetStats()`
200
+
201
+ 重置统计数据
202
+
203
+ ```typescript
204
+ genid.resetStats()
205
+ ```
206
+
207
+ ### 调试工具
208
+
209
+ #### `formatBinary(id)`
210
+
211
+ 格式化 ID 为二进制字符串
212
+
213
+ ```typescript
214
+ console.log(genid.formatBinary(id))
215
+ // ID: 123456789012345
216
+ // Binary (64-bit):
217
+ // 0000000000011010... - 时间戳 (52 bits) = 2025-10-17T...
218
+ // 000001 - 工作节点 ID (6 bits) = 1
219
+ // 101010 - 序列号 (6 bits) = 42
220
+ ```
221
+
222
+ ## 使用示例
223
+
224
+ ### 基础用法
225
+
226
+ ```typescript
227
+ const genid = new GenidOptimized({
228
+ workerId: 1
229
+ })
230
+
231
+ // 生成单个 ID
232
+ const id1 = genid.nextId()
233
+
234
+ // 批量生成
235
+ const ids = genid.nextBatch(1000)
236
+ ```
237
+
238
+ ### 自定义配置
239
+
240
+ ```typescript
241
+ const genid = new GenidOptimized({
242
+ workerId: 1,
243
+ method: GenidMethod.TRADITIONAL,
244
+ baseTime: new Date('2024-01-01').valueOf(),
245
+ workerIdBitLength: 10, // 支持 1024 个节点
246
+ seqBitLength: 12, // 每毫秒 4096 个 ID
247
+ topOverCostCount: 5000
248
+ })
249
+ ```
250
+
251
+ ### 监控性能
252
+
253
+ ```typescript
254
+ // 定期检查统计信息
255
+ setInterval(() => {
256
+ const stats = genid.getStats()
257
+ console.log(`生成速率: ${stats.avgPerSecond} ID/秒`)
258
+ console.log(`漂移次数: ${stats.overCostCount}`)
259
+ }, 10000)
260
+ ```
261
+
262
+ ## 算法模式
263
+
264
+ ### DRIFT(漂移模式,推荐)
265
+
266
+ - 高并发下性能更好
267
+ - 允许时间戳漂移以避免等待
268
+ - 适合高频 ID 生成场景
269
+
270
+ ### TRADITIONAL(传统模式)
271
+
272
+ - 严格按时间戳递增
273
+ - 序列号耗尽时等待下一毫秒
274
+ - 适合对时间顺序要求严格的场景
275
+
276
+ ## 注意事项
277
+
278
+ ⚠️ **重要提示**
279
+
280
+ - 每个 Worker/进程必须使用**不同的 workerId**
281
+ - 实例**不是线程安全的**,不要跨线程共享
282
+ - `workerIdBitLength + seqBitLength` 不能超过 22
283
+ - 序列号 0-4 保留用于时钟回拨处理
284
+ - JavaScript 安全整数范围:`-(2^53-1)` 到 `2^53-1`
285
+
286
+ ## 性能指标
287
+
288
+ - 单实例吞吐量:> 50,000 ID/秒
289
+ - 默认配置下每毫秒可生成:59 个唯一 ID
290
+ - 支持的最大节点数:2^workerIdBitLength(默认 64 个)
291
+ - 时间戳可用时长:约 139 年(52 bits,从 baseTime 起算)
292
+
293
+ ## 📜 License
294
+
295
+ [MIT](./LICENSE) License © 2025-PRESENT [wudi](https://github.com/WuChenDi)