3

我有一个使用 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上就这样死了......

谢谢!

4

1 回答 1

4

好吧,这是一个明显的 RTFM 案例,在发布之前没有睡在上面。

我在另一个 SO 帖子上看到,我可以通过以下方式将错误处理程序附加到 redis 客户端:

redis.on "error", (err) ->
   console.log("Redis error: #{err}")

这产生了

Redis error: Auth error: Error: Error: ERR max number of clients reached

在日志中,这是因为在每个请求上打开一个新连接而不是关闭它。然后,我将连接实例移至 server.js 文件,然后将其作为参数传递给我的路由处理程序。现在应用程序运行良好,只有一个活动连接......

希望这将帮助未来犯过类似错误的人......

于 2012-04-23T03:00:24.763 回答