fix: 🐛 start transaction with multi

This commit is contained in:
ThatOneCalculator 2023-06-23 18:21:00 -07:00
parent 8bb5844feb
commit be83ac8f43
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
1 changed files with 25 additions and 19 deletions

View File

@ -468,17 +468,19 @@ export default async (
} else if (boostedByRelay && data.renote?.uri) {
// Use Redis transaction for atomicity
await redisClient.watch(`publishedNote:${data.renote.uri}`);
const exists = await redisClient.exists(
`publishedNote:${data.renote.uri}`,
);
const exists = await redisClient.exists(`publishedNote:${data.renote.uri}`);
if (exists === 0) {
// Start the transaction
redisClient.multi();
publishNotesStream(data.renote);
const transaction = redisClient.multi();
const key = `publishedNote:${data.renote.uri}`;
await redisClient.set(key, 1, "EX", 30);
transaction.set(key, 1, "EX", 30);
// Execute the transaction
redisClient.exec();
transaction.exec((err, replies) => {
// Publish after setting the key in Redis
if (!err && data.renote) {
publishNotesStream(data.renote);
}
});
} else {
// Abort the transaction
redisClient.unwatch();
@ -489,12 +491,16 @@ export default async (
const exists = await redisClient.exists(`publishedNote:${note.uri}`);
if (exists === 0) {
// Start the transaction
redisClient.multi();
publishNotesStream(note);
const transaction = redisClient.multi();
const key = `publishedNote:${note.uri}`;
await redisClient.set(key, 1, "EX", 30);
transaction.set(key, 1, "EX", 30);
// Execute the transaction
redisClient.exec();
transaction.exec((err, replies) => {
// Publish after setting the key in Redis
if (!err) {
publishNotesStream(note);
}
});
} else {
// Abort the transaction
redisClient.unwatch();