This repository has no description
1.2 kB
41 lines
1import { RuntimeLock } from '@atproto/oauth-client-node';
2import Redis from 'ioredis';
3import Redlock from 'redlock';
4import { ILockService } from './ILockService';
5
6export class RedisLockService implements ILockService {
7 private redlock: Redlock;
8
9 constructor(private redis: Redis) {
10 this.redlock = new Redlock([redis], {
11 // Retry settings
12 retryCount: 3,
13 retryDelay: 200, // ms
14 retryJitter: 200, // ms
15 });
16
17 // Handle Fly.io container shutdown gracefully
18 process.on('SIGTERM', () => {
19 console.log('Received SIGTERM, shutting down gracefully...');
20 // Redlock will automatically release locks when the process exits
21 // No manual cleanup needed due to TTL
22 });
23 }
24
25 createRequestLock(): RuntimeLock {
26 return async (key: string, fn: () => any) => {
27 // Include Fly.io instance info in lock key
28 const instanceId = process.env.FLY_ALLOC_ID || 'local';
29 const lockKey = `oauth:lock:${instanceId}:${key}`;
30
31 // 30 seconds for Fly.io (containers restart more frequently)
32 const lock = await this.redlock.acquire([lockKey], 30000);
33
34 try {
35 return await fn();
36 } finally {
37 await this.redlock.release(lock);
38 }
39 };
40 }
41}