You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My redis connection using ioredis is able to connect and write data to it
redis.ts
import{scribe}from"@/logger/scribe";import*asIORedisfrom"ioredis";import{LockOptions}from"redis-semaphore";importConfigfrom"../../config";// This class also maintains references to all the locks and semaphores// And while going down, it will release all the locks and semaphores// This will help us to run multiple replicas of the same serviceclassRedis{privatestatic_instance: Redis;privatereadonlycluster: IORedis.Cluster;privatelocks: Map<string,Lock>=newMap();privatesemaphores: Map<string,Semaphore>=newMap();constructor(){// Config.Redis.ClusterNodes contains host and port array [{host:"",port:6789}...]this.cluster=newIORedis.Cluster(Config.Redis.ClusterNodes,{redisOptions: {password: Config.Redis.Password,},});this.cluster.on("ready",()=>{scribe.info("[ ✅ ] Redis ready");});this.cluster.on("error",(err)=>{scribe.error("[ 💥 ] Redis error",err);});}publicasyncping(): Promise<void>{awaitthis.cluster.ping();awaitthis.cluster.set("lastPing",newDate().toISOString());}}
When I run ping(), I can see the corresponding key being set
functioncreateCluster(){constnodes=[{host: 'localhost',port: 6379},{host: 'localhost',port: 6380},{host: 'localhost',port: 6381}]console.log('-----',nodes)constclient=newRedis.Cluster(nodes,{redisOptions: {password: 'test',lazyConnect: true,autoResendUnfulfilledCommands: false,// dont queue commands while server is offline (dont break test logic)maxRetriesPerRequest: 0// dont retry, fail faster (default is 20)// https://github.com/luin/ioredis#auto-reconnect// retryStrategy is a function that will be called when the connection is lost.// The argument times means this is the nth reconnection being made and the return value represents how long (in ms) to wait to reconnect.},lazyConnect: true,enableOfflineQueue: false,clusterRetryStrategy: ()=>{return100// for tests we disable increasing timeout}})client.on('error',err=>{console.log('Redis client error:',err.message)})returnclient}exportconstcluster=createCluster()
test case - RedlockMutex.test.ts
import{allClients,client1,client2,client3,cluster}from'../redisClient'consttimeoutOptions: TimeoutOptions={lockTimeout: 300,acquireTimeout: 100,refreshInterval: 80,retryInterval: 10}asyncfunctionexpectGetAll(key: string,value: string|null){awaitexpect(Promise.all([client1.get(key),client2.get(key),client3.get(key)])).to.become([value,value,value])}describe('RedlockMutex',()=>{it('should acquire and release lock using cluster',async()=>{constmutex=newRedlockMutex(cluster.nodes('master'),'key')expect(mutex.isAcquired).to.be.falseawaitmutex.acquire()expect(mutex.isAcquired).to.be.trueawaitexpectGetAll('mutex:key',mutex.identifier)awaitmutex.release()expect(mutex.isAcquired).to.be.falseawaitexpectGetAll('mutex:key',null)})
Result
RedlockMutex
1) should acquire and release lock using cluster
0 passing (10s)
1 failing
1) RedlockMutex
should acquire and release lock using cluster:
Error: Acquire redlock-mutex mutex:key timeout
at RedlockMutex.acquire (src/Lock.ts:140:13)
at async Context.<anonymous> (test/src/RedlockMutex.test.ts:59:5)
➜ redis-semaphore git:(master) ✗
The text was updated successfully, but these errors were encountered:
Setup
docker-compose.yml
My redis connection using ioredis is able to connect and write data to it
redis.ts
When I run
ping()
, I can see the corresponding key being setI initialize a mutex like this
It always results in
Test setup
Result
The text was updated successfully, but these errors were encountered: