gpt4 book ai didi

node.js - Heroku 上的粘性 session

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:52 27 4
gpt4 key购买 nike

我们有一个使用 SocketIO 运行的 NodeJS 应用程序,并在 heroku 上进行集群。为了让 SocketIO 正常工作,我们使用 redis 适配器,如下所述:https://socket.io/docs/using-multiple-nodes/

然后我们实现了粘性 session ,如粘性 session 文档中所示:https://github.com/elad/node-cluster-socket.io

事实证明,当我们部署到 Heroku 时,connection.remoteAddress 位于:

// Create the outside facing server listening on our port.
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
// We received a connection and need to pass it to the appropriate
// worker. Get the worker for this connection's source IP and pass
// it the connection.
var index = worker_index(connection.remoteAddress, num_processes);
var worker = workers[index];
worker.send('sticky-session:connection', connection);
}).listen(port);

实际上是一些heroku路由服务器的IP地址,而不是客户端IP。我已经看到请求 header “x-forwarded-for”可以用于获取客户端IP,但是当我们以这种方式暂停连接时,我们甚至还没有 header ?

最佳答案

我们到处寻找解决方案,但显然没有好的解决方案。

以下是一些更好的建议:

https://github.com/indutny/sticky-session/issues/6

https://github.com/indutny/sticky-session/pull/45

它们似乎都没有良好的性能,因此我们最终将 SocketIO 通信更改为仅 Websockets。这消除了粘性 session 的需要。

关于node.js - Heroku 上的粘性 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53938147/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com