-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlock-with-retry.ts
More file actions
106 lines (87 loc) · 3.22 KB
/
lock-with-retry.ts
File metadata and controls
106 lines (87 loc) · 3.22 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
/**
* Lock with retry example using redlock-universal
*
* This example demonstrates lock acquisition with retry logic for contested resources.
*/
import { createLock, NodeRedisAdapter, LockAcquisitionError } from 'redlock-universal';
import { createClient } from 'redis';
async function retryExample() {
// Setup Redis client
const client = createClient({ url: 'redis://localhost:6379' });
await client.connect();
// Create lock with retry configuration
const lock = createLock({
adapter: new NodeRedisAdapter(client),
key: 'contested-resource',
ttl: 10000, // 10 seconds
retryAttempts: 5,
retryDelay: 200, // 200ms between retries
});
try {
console.log('Attempting to acquire contested lock with retries...');
const startTime = Date.now();
const handle = await lock.acquire();
const acquisitionTime = Date.now() - startTime;
console.log(`Lock acquired after ${acquisitionTime}ms!`);
console.log(`Attempts: ${handle.metadata?.attempts ?? 1}`);
// Simulate work
console.log('Working with contested resource...');
await new Promise(resolve => setTimeout(resolve, 1500));
await lock.release(handle);
console.log('Lock released successfully!');
} catch (error) {
if (error instanceof LockAcquisitionError) {
console.error(`Failed to acquire lock after ${error.attempts} attempts`);
console.error(`Resource is highly contested, consider:`)
console.error('1. Increasing retry attempts');
console.error('2. Adding exponential backoff');
console.error('3. Queueing the operation for later');
} else {
console.error('Unexpected error:', error);
}
} finally {
await client.disconnect();
}
}
// Simulate multiple processes competing for the same resource
async function simulateContention() {
console.log('Simulating contention with multiple processes...');
const processes = Array.from({ length: 3 }, (_, i) =>
new Promise(async (resolve) => {
console.log(`Process ${i + 1} starting...`);
await new Promise(r => setTimeout(r, Math.random() * 1000)); // Random delay
const client = createClient({ url: 'redis://localhost:6379' });
await client.connect();
const lock = createLock({
adapter: new NodeRedisAdapter(client),
key: 'contested-resource',
ttl: 3000,
retryAttempts: 3,
retryDelay: 100,
});
try {
const handle = await lock.acquire();
console.log(`Process ${i + 1} acquired lock!`);
// Hold lock for varying durations
await new Promise(r => setTimeout(r, 1000 + Math.random() * 2000));
await lock.release(handle);
console.log(`Process ${i + 1} released lock!`);
} catch (error) {
console.log(`Process ${i + 1} failed to acquire lock`);
} finally {
await client.disconnect();
}
resolve(void 0);
})
);
await Promise.all(processes);
console.log('All processes completed!');
}
// Run examples
async function main() {
console.log('=== Single Process with Retry ===');
await retryExample();
console.log('\n=== Multiple Processes Contention ===');
await simulateContention();
}
main().catch(console.error);