【问题标题】:Connection timeout HTTP put request连接超时 HTTP put 请求
【发布时间】:2022-01-17 19:58:17
【问题描述】:

我在 GitHub 上遇到了一个非常不错的项目,并想试一试,但它并没有按应有的方式工作:https://github.com/jack3898/discord-youtube-bot-2

这是一个支持 YouTube 的 Discord 音乐机器人,它使用 docker、docker-compose 和 redis,当我几天前第一次发现它时,所有这 3 个东西对我来说都是新的。

在 Windows 上使用 docker 和 docker-compose 时,该机器人的工作方式非常棒,但是当我尝试在我的 Debian 11 服务器上运行它时出现问题。

Creating redis ... done
Creating bot   ... done
Attaching to redis, bot
redis    | 1:C 17 Jan 2022 19:40:56.879 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis    | 1:C 17 Jan 2022 19:40:56.879 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis    | 1:C 17 Jan 2022 19:40:56.880 # Configuration loaded
redis    | 1:M 17 Jan 2022 19:40:56.881 * monotonic clock: POSIX clock_gettime
redis    | 1:M 17 Jan 2022 19:40:56.884 * Running mode=standalone, port=6379.
redis    | 1:M 17 Jan 2022 19:40:56.884 # Server initialized
redis    | 1:M 17 Jan 2022 19:40:56.884 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis    | 1:M 17 Jan 2022 19:40:56.886 * <ReJSON> version: 20006 git sha: db3329c branch: heads/v2.0.6
redis    | 1:M 17 Jan 2022 19:40:56.886 * <ReJSON> Exported RedisJSON_V1 API
redis    | 1:M 17 Jan 2022 19:40:56.895 * <ReJSON> Enabled diskless replication
redis    | 1:M 17 Jan 2022 19:40:56.895 * <ReJSON> Created new data type 'ReJSON-RL'
redis    | 1:M 17 Jan 2022 19:40:56.896 * Module 'ReJSON' loaded from /usr/lib/redis/modules/rejson.so
redis    | 1:M 17 Jan 2022 19:40:56.899 * <search> Redis version found by RedisSearch : 6.2.5 - oss
redis    | 1:M 17 Jan 2022 19:40:56.899 * <search> RediSearch version 2.2.5 (Git=v1.99.5-386-g93a916c4)
redis    | 1:M 17 Jan 2022 19:40:56.899 * <search> Low level api version 1 initialized successfully
redis    | 1:M 17 Jan 2022 19:40:56.900 * <search> concurrent writes: OFF, gc: ON, prefix min length: 2, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, search pool size: 20, index pool size: 8,
redis    | 1:M 17 Jan 2022 19:40:56.905 * <search> Initialized thread pool!
redis    | 1:M 17 Jan 2022 19:40:56.905 * <search> Acquired RedisJSON_V1 API
redis    | 1:M 17 Jan 2022 19:40:56.906 * <search> Enabled diskless replication
redis    | 1:M 17 Jan 2022 19:40:56.906 * Module 'search' loaded from /usr/lib/redis/modules/redisearch.so
redis    | 1:M 17 Jan 2022 19:40:56.907 * Ready to accept connections
bot      |
bot      | > discord-youtube-bot-2@1.0.0 bot
bot      | > cross-env NODE_ENV=production ts-node src/index.ts -p tsconfig.builds.json
bot      |
bot      | Environment: production
bot      | Redis host: redis
bot      | Bot logged in as MusicBOT!
bot      | Started refreshing application slash commands.
bot      | 19 unique commands have been found on the file system.
bot      | /home/app/node_modules/@node-redis/client/dist/lib/client/socket.js:163
bot      |             socket.setTimeout(__classPrivateFieldGet(this, _RedisSocket_options, "f").connectTimeout, () => socket.destroy(new errors_1.ConnectionTimeoutError()));
bot      |                                                                                                                            ^
bot      | ConnectionTimeoutError: Connection timeout
bot      |     at Socket.<anonymous> (/home/app/node_modules/@node-redis/client/dist/lib/client/socket.js:163:124)
bot      |     at Object.onceWrapper (node:events:509:28)
bot      |     at Socket.emit (node:events:390:28)
bot      |     at Socket.emit (node:domain:475:12)
bot      |     at Socket._onTimeout (node:net:501:8)
bot      |     at listOnTimeout (node:internal/timers:557:17)
bot      |     at processTimers (node:internal/timers:500:7)

如您所见,它基本上会因错误“连接超时”而崩溃。经过一些快速挖掘代码后,我发现它在 HTTP 请求时崩溃。 (倒数第二行代码)

import { REST } from '@discordjs/rest';
import { config, globals } from 'bot-config';
import { Routes } from 'discord-api-types/v9';
import fs from 'fs';
import path from 'path';

/**
 * This function registers all slash commands to the Discord API.
 * It also stores all command classes in the globals object to be used later.
 */
export async function registerCommands() {
    const { discordToken, clientId, devGuildId } = config;
    const rest = new REST({ version: '9' }).setToken(discordToken as string);

    console.log('Started refreshing application slash commands.');

    // Create an endpoint to the Discord API that has the relevant Bot ID.
    // If not in production, the development Guild ID is added to speed up the command registration process.
    const route =
        config.environment === 'production'
            ? Routes.applicationCommands(clientId as string)
            : Routes.applicationGuildCommands(clientId as string, devGuildId as string);

    // This section simply finds all command classes.
    const commandModulePath = path.resolve('src', 'commands', 'modules');
    const commandModuleResolvingEntries = fs.readdirSync(commandModulePath).map(async moduleName => {
        const module = await import(`${commandModulePath}/${moduleName}`);
        return [moduleName, module];
    });

    const commandModules = await Promise.all(commandModuleResolvingEntries);

    // Register all commands to the Discord API and add the commands to the globals commandModules Map() instance.
    const slashCommandRegistrations = commandModules.map(module => {
        const commandName = module[0].split('.')[0].toLowerCase(); // Remove the '.ts' at the end and force lowercase.
        const commandClass = module[1].default;
        globals.commandModules.set(commandName, commandClass);
        return new commandClass().register().toJSON();
    });

    console.log(`${globals.commandModules.size} unique commands have been found on the file system.`);

    // Send a HTTP PUT request to the Discord API to register all slash commands.
    await rest.put(route, { body: slashCommandRegistrations });

    console.log('Successfully reloaded application slash commands.');
}

我真的无法找出是什么导致了这样的问题。这可能只是一些我看不到的菜鸟。

感谢您的宝贵时间。

【问题讨论】:

  • 感谢 vparys 提交这个问题!!这里是机器人作者。我想为这个问题添加一些细节。 - 我认为这与专用网络上的主机名解析有关。 - 我认为它可以本地化到 *NIX 操作系统,但我还没有在任何其他操作系统上尝试过。 - 我有点认为这可能是一个错误。不确定。
  • 您还会发现连接字符串是这样的:static client = createClient({ url: `redis://${config.redisHost}:${config.redisPort}` });。如果你访问 repo,你可以在 src/classes/modules/Cache.ts 中找到它

标签: node.js linux docker redis debian


【解决方案1】:

全部解决!

问题与声明多个 Redis 客户端有关。

在Cache.ts 和QueueManager.ts 之间可以找到两个。只需创建一个 Redis 客户端,即可解决超时问题。

我认为我的做法有点糟糕!但我学到了一些新东西。

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多