-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
209 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { container, kRedis } from "@yuudachi/framework"; | ||
import type { LocaleParam } from "@yuudachi/framework/types"; | ||
import type { GuildMember } from "discord.js"; | ||
import i18next from "i18next"; | ||
import type { Redis } from "ioredis"; | ||
import { LOCK_MAP_TOKEN, MEMBER_LOCK_INITIAL_EXPIRE_SECONDS } from "../../Constants.js"; | ||
import { memberToLockKey, createLockTimeout } from "./utils.js"; | ||
|
||
export async function acquireMemberLock(member: GuildMember, locale: LocaleParam): Promise<void> { | ||
const redis = container.resolve<Redis>(kRedis); | ||
const lockMap = container.resolve<Map<string, NodeJS.Timeout>>(LOCK_MAP_TOKEN); | ||
|
||
const key = memberToLockKey(member); | ||
|
||
const lock = await redis.set(key, member.user.createdTimestamp, "EX", MEMBER_LOCK_INITIAL_EXPIRE_SECONDS, "NX"); | ||
|
||
if (!lock) { | ||
throw new Error( | ||
i18next.t("command.common.errors.member_lock_acquired", { | ||
lng: locale, | ||
}), | ||
); | ||
} | ||
|
||
lockMap.set(key, createLockTimeout(member, lockMap)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { container, kRedis } from "@yuudachi/framework"; | ||
import type { Redis } from "ioredis"; | ||
|
||
export async function checkMemberLock(guildId: string, memberId: string): Promise<boolean> { | ||
const redis = container.resolve<Redis>(kRedis); | ||
|
||
const key = `guild:${guildId}:member-lock:${memberId}`; | ||
|
||
const lock = await redis.exists(key); | ||
|
||
return Boolean(lock); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { container } from "@yuudachi/framework"; | ||
import { LOCK_MAP_TOKEN } from "../../Constants.js"; | ||
|
||
export function createLockMap() { | ||
container.register(LOCK_MAP_TOKEN, { | ||
useValue: new Map(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { container, kRedis } from "@yuudachi/framework"; | ||
import type { GuildMember } from "discord.js"; | ||
import type { Redis } from "ioredis"; | ||
import { LOCK_MAP_TOKEN, MEMBER_LOCK_EXPIRE_SECONDS } from "../../Constants.js"; | ||
import { memberToLockKey, createLockTimeout } from "./utils.js"; | ||
|
||
export async function extendMemberLock(member: GuildMember): Promise<void> { | ||
const redis = container.resolve<Redis>(kRedis); | ||
const lockMap = container.resolve<Map<string, NodeJS.Timeout>>(LOCK_MAP_TOKEN); | ||
|
||
const key = memberToLockKey(member); | ||
|
||
await redis.expire(key, MEMBER_LOCK_EXPIRE_SECONDS, "GT"); | ||
|
||
const lock = lockMap.get(key); | ||
|
||
if (lock) { | ||
clearTimeout(lock); | ||
lockMap.set(key, createLockTimeout(member, lockMap)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from "./releaseLock.js"; | ||
export * from "./extendLock.js"; | ||
export * from "./acquireLock.js"; | ||
export * from "./checkLock.js"; | ||
export * from "./createLockMap.js"; | ||
export * from "./utils.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { container, kRedis } from "@yuudachi/framework"; | ||
import type { GuildMember } from "discord.js"; | ||
import type { Redis } from "ioredis"; | ||
import { memberToLockKey } from "./utils.js"; | ||
import { LOCK_MAP_TOKEN } from "../../Constants.js"; | ||
|
||
export async function releaseMemberLock(member: GuildMember): Promise<void> { | ||
const redis = container.resolve<Redis>(kRedis); | ||
const lockMap = container.resolve<Map<string, NodeJS.Timeout>>(LOCK_MAP_TOKEN); | ||
|
||
const key = memberToLockKey(member); | ||
|
||
await redis.del(key); | ||
|
||
const lock = lockMap.get(key); | ||
clearTimeout(lock); | ||
lockMap.delete(key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { setTimeout } from "node:timers"; | ||
import { logger } from "@yuudachi/framework"; | ||
import type { GuildMember } from "discord.js"; | ||
import { Counter } from "prom-client"; | ||
|
||
const expiredLocksCounter = new Counter({ | ||
name: "yuudachi_bot_v3_utils_expired_locks_total", | ||
help: "Total number of unreleased keys that expired", | ||
labelNames: ["memberId", "guildId"], | ||
}); | ||
|
||
export function memberToLockKey(member: GuildMember): `guild:${string}:member-lock:${string}` { | ||
return `guild:${member.guild.id}:member-lock:${member.id}`; | ||
} | ||
|
||
export function createLockTimeout(member: GuildMember, map: Map<string, NodeJS.Timeout>): NodeJS.Timeout { | ||
const lockKey = `guild:${member.guild.id}:member-lock:${member.id}`; | ||
|
||
return setTimeout(() => { | ||
logger.warn({ | ||
msg: "Lock expired", | ||
memberId: member.id, | ||
guildId: member.guild.id, | ||
lockKey, | ||
}); | ||
|
||
expiredLocksCounter.inc({ | ||
memberId: member.id, | ||
guildId: member.guild.id, | ||
}); | ||
|
||
map.delete(lockKey); | ||
}, 60_000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.