-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrwlock.ts
More file actions
384 lines (310 loc) · 12 KB
/
rwlock.ts
File metadata and controls
384 lines (310 loc) · 12 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* RwLock Examples
*
* Demonstrates how to use RwLock for read-write locking in async scenarios.
*
* RwLock allows multiple concurrent readers OR a single exclusive writer.
* This is useful when reads are frequent and writes are rare.
*
* **Important Note for JavaScript developers:**
* JavaScript is single-threaded, so RwLock's benefits are limited compared
* to multi-threaded languages like Rust. However, in async scenarios where
* read operations contain `await`, RwLock can still provide:
* 1. Clearer semantics (explicit read vs write intent)
* 2. Multiple async reads proceeding without blocking each other
* 3. Writer priority to prevent starvation
*/
import { RwLock } from '../../../src/mod.ts';
// ============================================================================
// Example 1: Basic Read/Write Operations
// ============================================================================
console.log('=== Example 1: Basic Read/Write Operations ===\n');
const counter = RwLock(0);
// Read the value
const value = await counter.withRead((v) => {
console.log(`Current value: ${v}`);
return v;
});
// Write a new value (need to use set() or modify through guard)
await counter.set(value + 1);
console.log(`After increment: ${await counter.get()}`);
// Or use withWrite with a mutable object
const mutableCounter = RwLock({ value: 0 });
await mutableCounter.withWrite((obj) => {
console.log(`Incrementing from ${obj.value} to ${obj.value + 1}`);
obj.value++;
});
console.log(`Final value: ${(await mutableCounter.get()).value}`);
// ============================================================================
// Example 2: Multiple Concurrent Readers
// ============================================================================
console.log('\n=== Example 2: Multiple Concurrent Readers ===\n');
const data = RwLock({ items: [1, 2, 3, 4, 5] });
console.log('Starting 3 concurrent read operations...');
// These reads can proceed concurrently
const results = await Promise.all([
data.withRead(async (d) => {
console.log('Reader A: starting');
await new Promise(r => setTimeout(r, 50));
const sum = d.items.reduce((a, b) => a + b, 0);
console.log(`Reader A: sum = ${sum}`);
return sum;
}),
data.withRead(async (d) => {
console.log('Reader B: starting');
await new Promise(r => setTimeout(r, 30));
const max = Math.max(...d.items);
console.log(`Reader B: max = ${max}`);
return max;
}),
data.withRead(async (d) => {
console.log('Reader C: starting');
await new Promise(r => setTimeout(r, 10));
const count = d.items.length;
console.log(`Reader C: count = ${count}`);
return count;
}),
]);
console.log(`All readers completed: [${results.join(', ')}]`);
// ============================================================================
// Example 3: Writer Blocks Readers
// ============================================================================
console.log('\n=== Example 3: Writer Blocks Readers ===\n');
const state = RwLock({ version: 1, data: 'initial' });
console.log('Writer acquires lock first...');
// Start a write operation
const writePromise = state.withWrite(async (s) => {
console.log('Writer: starting update');
await new Promise(r => setTimeout(r, 100));
s.version++;
s.data = 'updated';
console.log('Writer: update complete');
});
// These reads will wait for the writer to finish
const readPromises = Promise.all([
state.withRead((s) => {
console.log(`Reader 1: version=${s.version}, data="${s.data}"`);
}),
state.withRead((s) => {
console.log(`Reader 2: version=${s.version}, data="${s.data}"`);
}),
]);
await writePromise;
await readPromises;
console.log('All operations completed - readers saw updated data');
// ============================================================================
// Example 4: Configuration Store Pattern
// ============================================================================
console.log('\n=== Example 4: Configuration Store Pattern ===\n');
interface AppConfig {
apiUrl: string;
timeout: number;
features: Set<string>;
lastUpdated: Date;
}
const config = RwLock<AppConfig>({
apiUrl: 'https://api.example.com',
timeout: 5000,
features: new Set(['feature-a', 'feature-b']),
lastUpdated: new Date(),
});
// Read-only access to check feature flags (frequent operation)
async function isFeatureEnabled(feature: string): Promise<boolean> {
return config.withRead((cfg) => cfg.features.has(feature));
}
// Read-only access to get API URL (frequent operation)
async function getApiUrl(): Promise<string> {
return config.withRead((cfg) => cfg.apiUrl);
}
// Write access to enable a feature (rare operation)
async function enableFeature(feature: string): Promise<void> {
await config.withWrite((cfg) => {
cfg.features.add(feature);
cfg.lastUpdated = new Date();
console.log(`Enabled feature: ${feature}`);
});
}
// Write access to update timeout (rare operation)
async function updateTimeout(timeout: number): Promise<void> {
await config.withWrite((cfg) => {
cfg.timeout = timeout;
cfg.lastUpdated = new Date();
console.log(`Updated timeout to: ${timeout}ms`);
});
}
// Usage
console.log(`Feature A enabled: ${await isFeatureEnabled('feature-a')}`);
console.log(`Feature C enabled: ${await isFeatureEnabled('feature-c')}`);
await enableFeature('feature-c');
console.log(`Feature C enabled: ${await isFeatureEnabled('feature-c')}`);
await updateTimeout(10000);
console.log(`API URL: ${await getApiUrl()}`);
// ============================================================================
// Example 5: Cache with Read-Heavy Workload
// ============================================================================
console.log('\n=== Example 5: Cache with Read-Heavy Workload ===\n');
interface CacheEntry<T> {
value: T;
expiresAt: number;
}
const cache = RwLock(new Map<string, CacheEntry<string>>());
async function cacheGet(key: string): Promise<string | undefined> {
return cache.withRead((map) => {
const entry = map.get(key);
if (!entry) return undefined;
if (Date.now() > entry.expiresAt) return undefined;
return entry.value;
});
}
async function cacheSet(key: string, value: string, ttlMs: number): Promise<void> {
await cache.withWrite((map) => {
map.set(key, {
value,
expiresAt: Date.now() + ttlMs,
});
});
}
async function cacheDelete(key: string): Promise<boolean> {
return cache.withWrite((map) => map.delete(key));
}
// Set some values
await cacheSet('user:1', 'Alice', 60000);
await cacheSet('user:2', 'Bob', 60000);
await cacheSet('user:3', 'Charlie', 60000);
// Many concurrent reads (common pattern)
const users = await Promise.all([
cacheGet('user:1'),
cacheGet('user:2'),
cacheGet('user:3'),
cacheGet('user:1'), // duplicate read
cacheGet('user:2'), // duplicate read
]);
console.log(`Cached users: [${users.join(', ')}]`);
// Occasional write
await cacheDelete('user:2');
console.log(`After delete - user:2 = ${await cacheGet('user:2')}`);
// ============================================================================
// Example 6: Session Store with tryRead/tryWrite
// ============================================================================
console.log('\n=== Example 6: Session Store with tryRead/tryWrite ===\n');
interface Session {
userId: string;
createdAt: Date;
data: Record<string, unknown>;
}
const sessions = RwLock(new Map<string, Session>());
// Non-blocking read - useful when you want to avoid waiting
function tryGetSession(token: string): Session | undefined {
const guard = sessions.tryRead();
if (guard.isNone()) {
console.log('Sessions locked for writing, cannot read now');
return undefined;
}
try {
return guard.unwrap().value.get(token);
} finally {
guard.unwrap().unlock();
}
}
// Non-blocking write - useful for background tasks
function tryCreateSession(token: string, userId: string): boolean {
const guard = sessions.tryWrite();
if (guard.isNone()) {
console.log('Sessions locked, cannot create now');
return false;
}
try {
guard.unwrap().value.set(token, {
userId,
createdAt: new Date(),
data: {},
});
console.log(`Created session for user: ${userId}`);
return true;
} finally {
guard.unwrap().unlock();
}
}
// Create some sessions
tryCreateSession('token-abc', 'user-1');
tryCreateSession('token-xyz', 'user-2');
// Read sessions
const session1 = tryGetSession('token-abc');
console.log(`Session 1: userId=${session1?.userId}`);
const session2 = tryGetSession('token-xyz');
console.log(`Session 2: userId=${session2?.userId}`);
// ============================================================================
// Example 7: Manual Guard Control
// ============================================================================
console.log('\n=== Example 7: Manual Guard Control ===\n');
const resource = RwLock({ count: 0, log: [] as string[] });
// Using manual read guard
async function loggedRead(): Promise<number> {
const guard = await resource.read();
try {
const value = guard.value.count;
console.log(`Read count: ${value}`);
return value;
} finally {
guard.unlock();
}
}
// Using manual write guard
async function loggedIncrement(): Promise<void> {
const guard = await resource.write();
try {
guard.value.count++;
guard.value.log.push(`Incremented at ${new Date().toISOString()}`);
console.log(`Incremented to: ${guard.value.count}`);
} finally {
guard.unlock();
}
}
await loggedRead();
await loggedIncrement();
await loggedIncrement();
await loggedRead();
// ============================================================================
// Example 8: When to Use RwLock vs Mutex
// ============================================================================
console.log('\n=== Example 8: When to Use RwLock vs Mutex ===\n');
console.log('Use RwLock when:');
console.log(' - Reads are MUCH more frequent than writes');
console.log(' - Read operations contain await (async reads)');
console.log(' - You want to express read/write intent explicitly');
console.log('');
console.log('Use Mutex when:');
console.log(' - Read/write frequency is balanced');
console.log(' - Operations are quick (no await inside)');
console.log(' - You want simpler API');
console.log('');
console.log('In JavaScript, the performance difference is minimal');
console.log('because JS is single-threaded. Choose based on:');
console.log(' 1. Semantic clarity (read vs write intent)');
console.log(' 2. API design (RwLock has separate read/write methods)');
// ============================================================================
// Example 9: Status Methods
// ============================================================================
console.log('\n=== Example 9: Status Methods ===\n');
const status = RwLock('data');
console.log(`Initial state: ${status.toString()}`);
console.log(`Reader count: ${status.readerCount()}`);
console.log(`Write locked: ${status.isWriteLocked()}`);
const readGuard = await status.read();
console.log(`\nAfter acquiring read lock:`);
console.log(` ${status.toString()}`);
console.log(` Reader count: ${status.readerCount()}`);
const readGuard2 = await status.read();
console.log(`\nAfter acquiring second read lock:`);
console.log(` ${status.toString()}`);
console.log(` Reader count: ${status.readerCount()}`);
readGuard.unlock();
readGuard2.unlock();
const writeGuard = await status.write();
console.log(`\nAfter acquiring write lock:`);
console.log(` ${status.toString()}`);
console.log(` Write locked: ${status.isWriteLocked()}`);
writeGuard.unlock();
console.log(`\nAfter releasing write lock:`);
console.log(` ${status.toString()}`);
console.log('\n=== RwLock Examples Complete ===');