Skip to content

Commit

Permalink
Merge pull request #46 from torusresearch/develop
Browse files Browse the repository at this point in the history
Release: fix concurrency issue of nonce
  • Loading branch information
chaitanyapotti authored Dec 6, 2023
2 parents 930a136 + 89f5ef8 commit aa31ed0
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 41 deletions.
155 changes: 155 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"elliptic": "^6.5.4",
"express": "^4.18.2",
"helmet": "^6.1.5",
"ioredis": "^5.3.2",
"js-sha3": "^0.8.0",
"json-stable-stringify": "^1.0.2",
"knex": "^2.4.2",
Expand All @@ -46,6 +47,7 @@
"multihashing-async": "^2.1.4",
"mysql": "^2.18.1",
"redis": "^4.6.6",
"redlock": "^5.0.0-beta.2",
"socket.io": "^4.6.1"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions src/database/redis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Client from "ioredis";
import log from "loglevel";
import { createClient } from "redis";
import Redlock, { ResourceLockedError } from "redlock";

const { REDIS_PORT, REDIS_HOSTNAME } = process.env;
const client = createClient({ socket: { host: REDIS_HOSTNAME, port: Number(REDIS_PORT) } });
Expand All @@ -12,4 +14,20 @@ client.on("ready", () => {
log.info("Connected to redis");
});

export const redlock = new Redlock([new Client({ host: REDIS_HOSTNAME, port: Number(REDIS_PORT) })], {
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
retryCount: 10,
retryDelay: 200, // time in ms
retryJitter: 200, // time in ms
automaticExtensionThreshold: 500, // time in ms
});

redlock.on("error", (error) => {
// Ignore cases where a resource is explicitly marked as locked on a client.
if (error instanceof ResourceLockedError) {
return;
}
log.error(error);
});

export default client;
Loading

0 comments on commit aa31ed0

Please sign in to comment.