【问题标题】:yt-search module causing CPU spikesyt-search 模块导致 CPU 峰值
【发布时间】:2021-08-03 02:02:24
【问题描述】:

有什么方法可以限制模块拥有/使用的资源数量?当它使 CPU 使用率达到峰值并且当前正在播放音频流时,音频会停止一秒钟。我在 AWS EC2 上使用 1 个 vCPU 1G RAM 托管机器人。通常,机器人每个流使用大约 2-3 CPU Utilization,但在使用 yt-search 时它会飙升至大约 60 Utilization。以下是代码片段:

if(ytdl.validateURL(args[0])){
    const song_info = await ytdl.getInfo(args[0]);
    song = {title: song_info.videoDetails.title, url: song_info.videoDetails.video_url};
} else{
    const video_finder = async(query) =>{
        const videoResult = await ytSearch(query);
        return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
    }
    const video = await video_finder(args.join(' '));
    if(video){
        song = {title: video.title, url: video.url}
    } else{
        return message.reply('Error finding vdieo.')
    }
}

第一个 if 语句确定定义歌曲的参数是链接还是搜索词。如果它不是 URL,那么我使用 yt-search 模块来获取第一个结果以及结果的标题和 URL。

【问题讨论】:

  • 您是否尝试过分析以了解哪些功能成本最高?
  • 如果我尝试使用 YouTube URL 请求一首歌曲,机器人不会挂断并且 CPU 图表不会出现峰值,但是使用搜索方法会看到峰值并且流被中断.
  • 使用 youtube-sr 哪个更好

标签: javascript node.js amazon-ec2 discord.js


【解决方案1】:

放弃了 youtube-sr 的 yt-search。这个新模块不会占用 CPU,并且可以配置为只提取第一个结果。

if(ytdl.validateURL(args[0])){
    const song_info = await ytdl.getInfo(args[0]);
    song = {title: song_info.videoDetails.title, url: song_info.videoDetails.video_url};
} else{
    const video_finder = async(query) =>{
        const res = await ytSearch.search(query, {limit: 1, type: 'video'}).catch(e =>{});
        if(!res || !res[0]) return
        const videoResult = res[0];
        return (videoResult.title.length > 1) ? videoResult : null;
    }
    const video = await video_finder(args.join(' '));
    if(video){
        song = {title: video.title, url: video.url};
    } else{
        return message.reply('Error finding video.');
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2016-05-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多