-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex.ts
More file actions
280 lines (228 loc) · 8.14 KB
/
mutex.ts
File metadata and controls
280 lines (228 loc) · 8.14 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Mutex example: Async mutual exclusion for serializing operations
*
* Demonstrates using Mutex to prevent race conditions in async code,
* protecting shared resources from concurrent access.
*/
import { Mutex } from '../../../src/mod.ts';
// ============================================================================
// Example 1: Basic counter protection
// ============================================================================
console.log('=== Example 1: Basic counter protection ===\n');
const counter = Mutex({ count: 0 });
// Without mutex, this would cause race conditions
async function incrementCounter() {
await counter.withLock(async (state) => {
const current = state.count;
// Simulate some async work
await new Promise(r => setTimeout(r, 10));
state.count = current + 1;
});
}
console.log('Starting 10 concurrent increments...');
await Promise.all(Array.from({ length: 10 }, incrementCounter));
await counter.withLock((state) => {
console.log(`Final count: ${state.count} (expected: 10)`);
});
// ============================================================================
// Example 2: Token refresh pattern
// ============================================================================
console.log('\n=== Example 2: Token refresh pattern ===\n');
interface AuthState {
token: string;
expiresAt: number;
}
const authState = Mutex<AuthState>({
token: '',
expiresAt: 0,
});
let refreshCount = 0;
async function refreshToken(): Promise<{ token: string; expiresIn: number; }> {
refreshCount++;
console.log(` [Auth] Refreshing token (call #${refreshCount})...`);
await new Promise(r => setTimeout(r, 50));
return { token: `token-${Date.now()}`, expiresIn: 3600 };
}
async function getValidToken(): Promise<string> {
return await authState.withLock(async (state) => {
if (Date.now() >= state.expiresAt) {
const { token, expiresIn } = await refreshToken();
state.token = token;
state.expiresAt = Date.now() + expiresIn * 1000;
}
return state.token;
});
}
console.log('Making 5 concurrent API calls (all need token)...');
const tokens = await Promise.all([
getValidToken(),
getValidToken(),
getValidToken(),
getValidToken(),
getValidToken(),
]);
console.log(`Tokens received: ${tokens.length}`);
console.log(`All tokens identical: ${tokens.every(t => t === tokens[0])}`);
console.log(`Refresh was called: ${refreshCount} time(s)\n`);
// ============================================================================
// Example 3: Database transaction simulation
// ============================================================================
console.log('=== Example 3: Database transaction simulation ===\n');
interface Account {
id: string;
balance: number;
}
interface Database {
accounts: Map<string, Account>;
}
const db = Mutex<Database>({
accounts: new Map([
['alice', { id: 'alice', balance: 1000 }],
['bob', { id: 'bob', balance: 500 }],
]),
});
async function transfer(from: string, to: string, amount: number): Promise<boolean> {
return await db.withLock(async (database) => {
const fromAccount = database.accounts.get(from);
const toAccount = database.accounts.get(to);
if (!fromAccount || !toAccount) {
console.log(` Transfer failed: Account not found`);
return false;
}
if (fromAccount.balance < amount) {
console.log(` Transfer failed: Insufficient funds`);
return false;
}
// Simulate some processing time
await new Promise(r => setTimeout(r, 20));
fromAccount.balance -= amount;
toAccount.balance += amount;
console.log(` Transferred $${amount} from ${from} to ${to}`);
return true;
});
}
console.log('Initial balances:');
await db.withLock((database) => {
for (const [id, account] of database.accounts) {
console.log(` ${id}: $${account.balance}`);
}
});
console.log('\nExecuting concurrent transfers...');
await Promise.all([
transfer('alice', 'bob', 200),
transfer('alice', 'bob', 300),
transfer('bob', 'alice', 100),
]);
console.log('\nFinal balances:');
await db.withLock((database) => {
for (const [id, account] of database.accounts) {
console.log(` ${id}: $${account.balance}`);
}
});
// ============================================================================
// Example 4: Manual guard control
// ============================================================================
console.log('\n=== Example 4: Manual guard control ===\n');
const resource = Mutex({ data: 'initial' });
console.log('Acquiring lock manually...');
const guard = await resource.lock();
console.log(`Current value: ${guard.value.data}`);
console.log('Modifying value...');
guard.value = { data: 'modified' };
console.log('Releasing lock...');
guard.unlock();
await resource.withLock((value) => {
console.log(`After release: ${value.data}`);
});
// ============================================================================
// Example 5: tryLock for non-blocking check
// ============================================================================
console.log('\n=== Example 5: tryLock for non-blocking check ===\n');
const busyResource = Mutex({ status: 'idle' });
// Hold the lock
const holder = await busyResource.lock();
holder.value.status = 'busy';
// Try to lock without blocking
const tryResult = busyResource.tryLock();
if (tryResult.isSome()) {
console.log('Acquired lock successfully');
tryResult.unwrap().unlock();
} else {
console.log('Resource is busy, doing something else...');
}
console.log(`isLocked: ${busyResource.isLocked()}`);
holder.unlock();
console.log(`After unlock, isLocked: ${busyResource.isLocked()}`);
// Now tryLock should succeed
const retryResult = busyResource.tryLock();
if (retryResult.isSome()) {
console.log('Retry: Acquired lock successfully');
retryResult.unwrap().unlock();
}
// ============================================================================
// Example 6: File write simulation
// ============================================================================
console.log('\n=== Example 6: File write simulation ===\n');
interface FileContent {
lines: string[];
}
const fileContent = Mutex<FileContent>({ lines: [] });
async function appendLine(line: string) {
await fileContent.withLock(async (file) => {
console.log(` Writing: "${line}"`);
// Simulate disk I/O
await new Promise(r => setTimeout(r, 20));
file.lines.push(line);
});
}
console.log('Writing lines concurrently...');
await Promise.all([
appendLine('First line'),
appendLine('Second line'),
appendLine('Third line'),
]);
console.log('\nFile contents (in order):');
await fileContent.withLock((file) => {
file.lines.forEach((line, i) => console.log(` ${i + 1}. ${line}`));
});
// ============================================================================
// Example 7: Queue consumer
// ============================================================================
console.log('\n=== Example 7: Queue consumer ===\n');
interface MessageQueue {
messages: string[];
processed: string[];
}
const queue = Mutex<MessageQueue>({
messages: ['msg1', 'msg2', 'msg3', 'msg4', 'msg5'],
processed: [],
});
async function processNext(): Promise<boolean> {
return await queue.withLock(async (q) => {
const msg = q.messages.shift();
if (!msg) {
return false;
}
// Simulate processing
await new Promise(r => setTimeout(r, 10));
q.processed.push(`processed-${msg}`);
console.log(` Processed: ${msg}`);
return true;
});
}
console.log('Starting 3 concurrent consumers...');
async function consumer(id: string) {
while (await processNext()) {
// Continue processing
}
console.log(` Consumer ${id} finished`);
}
await Promise.all([
consumer('A'),
consumer('B'),
consumer('C'),
]);
await queue.withLock((q) => {
console.log(`\nTotal processed: ${q.processed.length}`);
console.log(`Remaining in queue: ${q.messages.length}`);
});