From 824a01f7c8dd8b4410ea65a9744bd4176e6ff5df Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Wed, 24 Jul 2024 16:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20reactor:=20`=E4=BB=A4=E7=89=8C?= =?UTF-8?q?=E6=A1=B6`=20=E6=B7=B1=E5=BA=A6=E6=B8=85=E6=B4=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/query.js | 21 ----------- apps/tools.js | 46 ++++++------------------ utils/token-bucket.js | 84 ------------------------------------------- 3 files changed, 10 insertions(+), 141 deletions(-) delete mode 100644 utils/token-bucket.js diff --git a/apps/query.js b/apps/query.js index 83016ff..a2a8595 100644 --- a/apps/query.js +++ b/apps/query.js @@ -10,15 +10,8 @@ import { CAT_LIMIT, COMMON_USER_AGENT } from "../constants/constant.js"; import config from "../model/index.js"; // 书库 import { getYiBook, getZBook, getZHelper } from "../utils/books.js"; -// 工具类 -import TokenBucket from '../utils/token-bucket.js' export class query extends plugin { - /** - * 令牌桶 拿来限流 - * @type {TokenBucket} - */ - static #tokenBucket = new TokenBucket(1, 1, 60); constructor() { super({ @@ -320,20 +313,6 @@ export class query extends plugin { return true; } - /** - * 限制用户调用(默认1分钟1次) - * @param e - * @param func - * @return {Promise} - */ - async limitUserUse(e, func) { - if (query.#tokenBucket.consume(e.user_id, 1)) { - await func(); - } else { - e.reply(`🙅‍${ e.nickname }你已经被限流,请稍后再试!`, true); - } - } - // 删除标签 removeTag(title) { const titleRex = /<[^>]+>/g; diff --git a/apps/tools.js b/apps/tools.js index 6125fbd..be7d49a 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -52,7 +52,6 @@ import * as aBogus from "../utils/a-bogus.cjs"; import { getBodianAudio, getBodianMusicInfo, getBodianMv } from "../utils/bodian.js"; import { av2BV } from "../utils/bilibili-bv-av-convert.js"; import querystring from "querystring"; -import TokenBucket from "../utils/token-bucket.js"; import PQueue from 'p-queue'; import { getWbi } from "../utils/biliWbi.js"; import { @@ -94,11 +93,6 @@ export class tools extends plugin { static Constants = { existsTransKey: Object.keys(transMap).join("|"), }; - /** - * 构造令牌桶,防止解析致使服务器宕机(默认限制5s调用一次) - * @type {TokenBucket} - */ - static #tokenBucket = new TokenBucket(1, 1, 5); constructor() { super({ @@ -463,16 +457,8 @@ export class tools extends plugin { return true; } - - // bilibi解析 + // B 站解析 async bili(e) { - // 限制用户密集使用的 AOP - await this.limitUserUse(e, () => { - this.biliCore(e); - }); - } - - async biliCore(e) { const urlRex = /(?:https?:\/\/)?www\.bilibili\.com\/[A-Za-z\d._?%&+\-=\/#]*/g; const bShortRex = /(http:|https:)\/\/b23.tv\/[A-Za-z\d._?%&+\-=\/#]*/g; let url = e.msg === undefined ? e.message.shift().data.replaceAll("\\", "") : e.msg.trim().replaceAll("\\", ""); @@ -659,13 +645,15 @@ export class tools extends plugin { // 下载哔哩哔哩音乐 async biliMusic(e, url) { const videoId = /video\/[^\?\/ ]+/.exec(url)[0].split("/")[1]; - getBiliAudio(videoId, "").then(async audioUrl => { - const path = this.getCurDownloadPath(e); - const biliMusicPath = await m4sToMp3(audioUrl, path) - // 发送语音 - e.reply(segment.record(biliMusicPath)); - // 上传群文件 - await this.uploadGroupFile(e, biliMusicPath); + this.queue.add(() => { + getBiliAudio(videoId, "").then(async audioUrl => { + const path = this.getCurDownloadPath(e); + const biliMusicPath = await m4sToMp3(audioUrl, path) + // 发送语音 + e.reply(segment.record(biliMusicPath)); + // 上传群文件 + await this.uploadGroupFile(e, biliMusicPath); + }) }) return true } @@ -2002,20 +1990,6 @@ export class tools extends plugin { return JSON.parse((await redis.get(REDIS_YUNZAI_LAGRANGE))).driver; } - /** - * 限制用户调用 - * @param e - * @param func - * @return {Promise} - */ - async limitUserUse(e, func) { - if (tools.#tokenBucket.consume(e.user_id, 1)) { - await func(); - } else { - logger.warn(`解析被限制使用`); - } - } - /** * 发送转上传视频 * @param e 交互事件 diff --git a/utils/token-bucket.js b/utils/token-bucket.js deleted file mode 100644 index 1877d6e..0000000 --- a/utils/token-bucket.js +++ /dev/null @@ -1,84 +0,0 @@ -export default class TokenBucket { - constructor(rate, capacity, interval = 1, isMinute = false) { - this.interval = interval; // 生成令牌的时间间隔 - this.rate = isMinute ? rate / 60 : rate; // 修改为每分钟生成的令牌数量 - this.capacity = capacity; // 令牌容量 - this.tokens = capacity; // 令牌容量 - this.tokens = new Map(); // 使用 Map 存储每个用户的令牌桶 - this.lastTime = new Date().getTime(); // 上次使用时间 - - /** - * 核心算法 - * @param tokens - * @param capacity - * @param rate - * @param lastTime - * @param interval - * @param isMinute - * @return {{lastTime: number, tokens: number}} - */ - this.updateTokens = (tokens, capacity, rate, lastTime, interval) => { - // 计算从上次请求到现在经过的时间 - const now = new Date().getTime(); - const elapsed = now - lastTime; - // 根据时间计算出新生成的令牌数量 - const addedTokens = elapsed * (rate / interval / 1000); // 修改为每分钟生成的令牌数量 - tokens = Math.min(tokens + addedTokens, capacity); - lastTime = now; - return { tokens, lastTime }; - } - } - - /** - * 消耗令牌-一个桶 - * @param count - * @return {boolean} - */ - consumeSingle(count = 1) { - const { tokens, lastTime } = this.updateTokens(this.tokens, this.capacity, this.rate, this.lastTime, this.interval); - // 更新令牌桶中的令牌数量 - this.tokens = tokens; - - // 判断请求是否能够被处理(即令牌桶中是否有足够的令牌) - if (count <= this.tokens) { - this.tokens -= count; - return true; // 返回 true 表示请求被处理 - } else { - return false; // 返回 false 表示请求被限流 - } - } - - /** - * 消耗令牌 - * @param id 用户id - * @param count 请求次数 - * @return {boolean} - */ - consume(id, count = 1) { - const { tokens: userTokens, lastTime: userLastTime } = this.tokens.get(id) || { tokens: this.capacity, lastTime: new Date().getTime() }; - const { tokens, lastTime } = this.updateTokens(userTokens, this.capacity, this.rate, userLastTime, this.interval); - // 更新令牌桶中的令牌数量 - this.tokens.set(id, { tokens, lastTime }); - - // 判断请求是否能够被处理(即令牌桶中是否有足够的令牌) - if (count <= tokens) { - this.tokens.set(id, { tokens: tokens - count, lastTime }); - return true; - } else { - return false; - } - } - - /** - * 重置令牌 - * @param newCapacity - */ - resetCapacity(newCapacity) { - if (newCapacity >= this.tokens) { - this.capacity = newCapacity; - this.tokens = newCapacity; - } else { - throw new Error('分配少于当前的容量!'); - } - } -} \ No newline at end of file