【发布时间】:2012-05-02 15:53:01
【问题描述】:
我有一个使用 nodejs 和 redis 在 heroku 上运行的非常简单的应用程序。它通过 ajax post 定期获取数据,并将数据存储在 Redis 中的列表中。
我在本地运行应用程序没有问题,它将发送给它的数据记录到 redis 没有投诉。但是,当我在 heroku 上运行它时,在它崩溃之前我收到了大约 5-10 个请求,并出现了一个非常非特定的 redis 错误。
依赖关系:
"redis": "~0.7.1",
"hiredis": "~0.1.14",
"redis-url": "~0.1.0"
写redis的代码(coffeescript):
app.post '/track', (req, res) ->
redis = require('redis-url').connect(app.settings.redis_url)
if(req.body.userid)
key = "locations:#{req.body.userid}"
redis.rpush key, JSON.stringify({time: (new Date()).toString(), lat: req.body.latitude, lon: req.body.longitude})
我得到的错误如下:
Error: Uncaught, unspecified 'error' event.
2012-04-21T06:12:00+00:00 app[web.1]: at Command.callback (/app/node_modules/redis/index.js:159:29)
2012-04-21T06:12:00+00:00 app[web.1]: at HiredisReplyParser.<anonymous> (/app/node_modules/redis/index.js:256:14)
2012-04-21T06:12:00+00:00 app[web.1]: at RedisClient.return_error (/app/node_modules/redis/index.js:446:25)
2012-04-21T06:12:00+00:00 app[web.1]: at HiredisReplyParser.execute (/app/node_modules/redis/lib/parser/hiredis.js:41:18)
2012-04-21T06:12:00+00:00 app[web.1]: at HiredisReplyParser.emit (events.js:67:17)
2012-04-21T06:12:00+00:00 app[web.1]: at RedisClient.on_data (/app/node_modules/redis/index.js:422:27)
2012-04-21T06:12:00+00:00 app[web.1]: at Socket.emit (events.js:67:17)
2012-04-21T06:12:00+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/redis/index.js:66:14)
2012-04-21T06:12:00+00:00 app[web.1]: at TCP.onread (net.js:367:14)
这会使应用程序崩溃,heroku 最终会恢复该应用程序,但它会在几个请求内很快再次崩溃。
以前有人遇到过这种情况吗?我对 node/redis 很陌生,所以这可能是显而易见的。奇怪的是它在本地几乎永远快乐地运行,但在 heroku 上就这样死了......
谢谢!
【问题讨论】:
-
不是因为每次 POST 事件都会建立到 Redis 的连接吗?该应用程序不应该这样做。 Heroku Redis 实例允许多少个连接?
-
你说得对,迪迪埃。我刚刚发现我的客户快用完了。我要将连接移动到 server.js 中,以便它在应用程序范围内而不是在发布请求期间打开。我现在通过每次关闭连接来修复它,但显然更好地为整个应用程序重新使用一个连接。谢谢!