多次调用函数后redis连接丢失

Redis connection is lost after multiple calls to function

我写的这个程序是一个告警的状态显示画面,每一个都用一个通道来表示。

当服务器启动时(运行 在 vagrant 虚拟机上),访问 Influx 数据库,数据(包括 1574 'channels')被处理并放入 Redis 数据库。这 运行 很好,刷新网页时 GUI 显示没有问题,尽管加载时间很长(最多 20 秒),而且几乎所有时间都花在了下面的方法中。

然而,在站点周围 refreshes/moving 之后,它经常崩溃并出现以下错误:

{ AbortError: Redis connection lost and command aborted. It might have been processed. at RedisClient.flush_and_error (/vagrant/node_modules/redis/index.js:362:23) at RedisClient.connection_gone (/vagrant/node_modules/redis/index.js:664:14) at RedisClient.on_error (/vagrant/node_modules/redis/index.js:410:10) at Socket. (/vagrant/node_modules/redis/index.js:279:14) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at onwriteError (_stream_writable.js:417:12) at onwrite (_stream_writable.js:439:5) at _destroy (internal/streams/destroy.js:39:7) at Socket._destroy (net.js:568:3) code: 'UNCERTAIN_STATE', command: 'HGETALL', args: [ 'vista:hash:Result:44f59707-c873-11e8-93b9-7f551d0bdd1f' ], origin: { Error: Redis connection to 127.0.0.1:6379 failed - write EPIPE at WriteWrap.afterWrite (net.js:868:14) errno: 'EPIPE', code: 'EPIPE', syscall: 'write' } }

这个错误显示1574次(每个通道一次),当程序执行到这个函数时出现:

 Result.getFormattedResults = async function (cycle) {
    const channels = await Channel.findAndLoad()
    const formattedResults = await mapAsyncParallel(channels, async channel => {
        const result = await this.findAndLoadByChannel(channel, cycle)
        const formattedResult = await result.format(channel)
        return formattedResult
    })
    return formattedResults
} 

mapAsyncParallel()如下:

export const mapAsyncParallel = (arr, fn, thisArg) => {
    return Promise.all(arr.map(fn, thisArg))
}

findAndLoadByChannel() 找到频道并使用此行加载它:

const resultModel = await this.load(resultId)

并且 format() 采用模型并以 JSON 格式输出数据

前端有两个'fetch(...)'命令(需要,不能合并),我注释掉其中一个(其中一个)很少出现这个问题。这让我觉得这可能是最大内存或最大连接问题? (增加配置文件中的 maxmemory 没有帮助)。或者使用这么多承诺的问题(我对这个概念还很陌生)。

这只是在我添加了更多功能后才开始出现,我认为该功能需要优化,但我已经从其他人那里接管了这个项目,并且对 node.js 和 redis 仍然很陌生。

版本:

我现在已经将所有 'getting' 数据(从 Redis)移动到服务器端 channels.controller 文件。

那么,之前我会在哪里:

renderPage: async (req, res) => {
    res.render('page')
},

我现在有这样的方法:

renderPage: async (req, res) => {
        const data1 = getData1()
        const data2 = getData2()
        res.render('page',  {data1, data2})
}, 

(别担心,这些不是我实际的变量名)

之前使用 'fetch' 方法检索了两个 'data' 变量。

export 数据一旦加载到 redis 中,import 它就在控制器文件中,我有吸气剂将它们全部组合成一个 return 数组。

页面现在需要几毫秒来刷新,而且我没有遇到任何崩溃