This repository has been archived by the owner on May 28, 2021. It is now read-only.
/
redis.provider.ts
54 lines (45 loc) · 1.68 KB
/
redis.provider.ts
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
import { FactoryProvider } from "@nestjs/common/interfaces";
import Redis from "ioredis";
import Redlock from "redlock";
import { ConfigService } from "../config/config.service";
import { RedisProviderId, RedlockProviderId } from "../constants";
import { CLogger } from "../util";
const logger = new CLogger("RedisService");
export const redisClientFactory: FactoryProvider = {
inject: [ConfigService],
provide: RedisProviderId,
useFactory: (config: ConfigService): Redis.Redis => {
const redisClient = new Redis(config.getRedisUrl(), {
retryStrategy: (times: number): number => {
logger.warn("Lost connection to redis. Retrying to connect...");
const delay = Math.min(times * 50, 2000);
return delay;
},
});
return redisClient;
},
};
export const redlockClientFactory: FactoryProvider = {
inject: [RedisProviderId],
provide: RedlockProviderId,
useFactory: (redis: Redis.Redis): Redlock => {
const redlockClient = new Redlock([redis], {
// the expected clock drift; for more details
// see http://redis.io/topics/distlock
driftFactor: 0.01, // time in ms
// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 350,
// the time in ms between attempts
retryDelay: 100, // time in ms
// the max time in ms randomly added to retries
// to improve performance under high contention
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
retryJitter: 1000, // time in ms
});
redlockClient.on("clientError", (err: any) => {
console.error("A redis error has occurred:", err);
});
return redlockClient;
},
};