-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathkeyProducer.ts
More file actions
175 lines (144 loc) · 5.91 KB
/
keyProducer.ts
File metadata and controls
175 lines (144 loc) · 5.91 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { FairQueueKeyProducer } from "./types.js";
/**
* Default key producer for the fair queue system.
* Uses a configurable prefix and standard key structure.
*
* Key structure:
* - Master queue: {prefix}:master:{shardId} (legacy, drain-only)
* - Dispatch index: {prefix}:dispatch:{shardId} (Level 1: tenantIds)
* - Tenant queue index: {prefix}:tenantq:{tenantId} (Level 2: queueIds)
* - Queue: {prefix}:queue:{queueId}
* - Queue items: {prefix}:queue:{queueId}:items
* - Concurrency: {prefix}:concurrency:{groupName}:{groupId}
* - In-flight: {prefix}:inflight:{shardId}
* - In-flight data: {prefix}:inflight:{shardId}:data
* - Worker queue: {prefix}:worker:{consumerId}
*/
export class DefaultFairQueueKeyProducer implements FairQueueKeyProducer {
private readonly prefix: string;
private readonly separator: string;
constructor(options: { prefix?: string; separator?: string } = {}) {
this.prefix = options.prefix ?? "fq";
this.separator = options.separator ?? ":";
}
// ============================================================================
// Master Queue Keys
// ============================================================================
masterQueueKey(shardId: number): string {
return this.#buildKey("master", shardId.toString());
}
// ============================================================================
// Queue Keys
// ============================================================================
queueKey(queueId: string): string {
return this.#buildKey("queue", queueId);
}
queueItemsKey(queueId: string): string {
return this.#buildKey("queue", queueId, "items");
}
// ============================================================================
// Concurrency Keys
// ============================================================================
concurrencyKey(groupName: string, groupId: string): string {
return this.#buildKey("concurrency", groupName, groupId);
}
// ============================================================================
// In-Flight Keys
// ============================================================================
inflightKey(shardId: number): string {
return this.#buildKey("inflight", shardId.toString());
}
inflightDataKey(shardId: number): string {
return this.#buildKey("inflight", shardId.toString(), "data");
}
// ============================================================================
// Worker Queue Keys
// ============================================================================
workerQueueKey(consumerId: string): string {
return this.#buildKey("worker", consumerId);
}
// ============================================================================
// Tenant Dispatch Keys (Two-Level Index)
// ============================================================================
dispatchKey(shardId: number): string {
return this.#buildKey("dispatch", shardId.toString());
}
tenantQueueIndexKey(tenantId: string): string {
return this.#buildKey("tenantq", tenantId);
}
// ============================================================================
// Dead Letter Queue Keys
// ============================================================================
deadLetterQueueKey(tenantId: string): string {
return this.#buildKey("dlq", tenantId);
}
deadLetterQueueDataKey(tenantId: string): string {
return this.#buildKey("dlq", tenantId, "data");
}
// ============================================================================
// Extraction Methods
// ============================================================================
/**
* Extract tenant ID from a queue ID.
* Default implementation assumes queue IDs are formatted as: tenant:{tenantId}:...
* Override this method for custom queue ID formats.
*/
extractTenantId(queueId: string): string {
const parts = queueId.split(this.separator);
// Expect format: tenant:{tenantId}:...
if (parts.length >= 2 && parts[0] === "tenant" && parts[1]) {
return parts[1];
}
// Fallback: return the first segment
return parts[0] ?? "";
}
/**
* Extract a group ID from a queue ID.
* Default implementation looks for pattern: {groupName}:{groupId}:...
* Override this method for custom queue ID formats.
*/
extractGroupId(groupName: string, queueId: string): string {
const parts = queueId.split(this.separator);
// Look for the group name in the queue ID parts
for (let i = 0; i < parts.length - 1; i++) {
if (parts[i] === groupName) {
const nextPart = parts[i + 1];
if (nextPart) {
return nextPart;
}
}
}
// Fallback: return an empty string
return "";
}
// ============================================================================
// Helper Methods
// ============================================================================
#buildKey(...parts: string[]): string {
return [this.prefix, ...parts].join(this.separator);
}
}
/**
* Key producer with custom extraction logic via callbacks.
* Useful when queue IDs don't follow a standard pattern.
*/
export class CallbackFairQueueKeyProducer extends DefaultFairQueueKeyProducer {
private readonly tenantExtractor: (queueId: string) => string;
private readonly groupExtractor: (groupName: string, queueId: string) => string;
constructor(options: {
prefix?: string;
separator?: string;
extractTenantId: (queueId: string) => string;
extractGroupId: (groupName: string, queueId: string) => string;
}) {
super({ prefix: options.prefix, separator: options.separator });
this.tenantExtractor = options.extractTenantId;
this.groupExtractor = options.extractGroupId;
}
override extractTenantId(queueId: string): string {
return this.tenantExtractor(queueId);
}
override extractGroupId(groupName: string, queueId: string): string {
return this.groupExtractor(groupName, queueId);
}
}