两台服务器之间的 DDP 不会重新连接 [英] DDP between two servers doesn't reconnect

查看:35
本文介绍了两台服务器之间的 DDP 不会重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的服务器上通过 DDP 连接了两个流星应用程序,服务器 A 将数据发送到服务器 B.这就是它们的工作方式.

I have two meteor applications connected via DDP on different servers and server A send data to server B. This is the way they work.

服务器 A

Items = new Meteor.Collection('items');
Items.insert({name: 'item 1'});
if (Meteor.isServer) {
  Meteor.publish('items', function() {
    return Items.find();
  });
}

服务器 B

var remote = DDP.connect('http://server-a/');
Items = new Meteor.Collection('items', remote); 

remote.subscribe('items');
Items.find().observe({
  added: function(item) {
    console.log(item);
  }
});

每次我在服务器 A 上调用 Items.insert(something) 时,服务器 BI 都会在控制台上获得一个日志,其中包含我保存在服务器 A 上的对象.但是如果服务器 B 失去 Internet 连接,服务器 A 上插入的数据在重新连接到 Internet 时不再出现在服务器 B 上.

Every time I call Items.insert(something) on server A, on Server B I got a log on the console with the object I saved on Server A. But if Server B lost Internet connection, the data inserted on Server A doesn't appear anymore on Server B when it reconnect to Internet.

服务器 B 通过路由器连接到 Internet.这个问题只发生在我断开和重新连接路由器时,而不是当我从路由器断开和重新连接服务器时.两台服务器位于不同的网络上并通过 Internet 连接.

Server B is connected to Internet through a router. This problem only happen when I disconnect and reconnect the router, not when I disconnect and reconnect the server from the router. Both servers are on different networks and connect via Internet.

我在服务器 B 上创建了一个计时器,它调用 remote.status() 但总是得到 { status: 'connected', connected: true, retryCount: 0 } 当与互联网连接或断开连接.

I created a timer on Server B that call remote.status() but always get { status: 'connected', connected: true, retryCount: 0 } when connected or disconnected from Internet.

更新:重现步骤

我使用测试代码在 github 上创建了一个项目 https://github.com/camilosw/ddp-服务器-测试.服务器 A 安装在 http://ddpserverstest-9592.onmodulus.net/

I created a project on github with the testing code https://github.com/camilosw/ddp-servers-test. Server A is installed on http://ddpserverstest-9592.onmodulus.net/

我的电脑通过无线电缆调制解调器连接到互联网.

My computer is connected to Internet through a wireless cable modem.

  1. 在 server-b 文件夹上运行 mrt
  2. 转到 http://ddpserverstest-9592.onmodulus.net/ 并单击链接插入(您可以单击删除以删除所有以前的插入).您必须在本地控制台上看到一条消息,其中包含添加的项目.
  3. 关闭计算机上的无线并再次单击插入链接.(您需要点击另一台可以访问互联网的计算机,我使用智能手机点击链接)
  4. 打开计算机上的无线网络.您必须在本地控制台上看到带有第二项的消息.
  5. 现在,关闭电缆调制解调器并再次单击插入链接.
  6. 打开电缆调制解调器.这一次,新项目没有出现在控制台上.
  1. Run mrt on server-b folder
  2. Go to http://ddpserverstest-9592.onmodulus.net/ and click the link Insert (you can click delete to remove all previous inserts). You must see a message on your local console with the added item.
  3. Turn off the wireless on the computer and click the insert link again. (You will need to click on another computer with Internet access, I used an smartphone to click the link)
  4. Turn on the wireless on the computer. You must see a message on your local console with the second item.
  5. Now, turn off the cable modem and click the insert link again.
  6. Turn on the cable modem. This time, the new item doesn't appear on the console.

我还使用 Android 智能手机完成了这项工作,使用了通过无线将互联网共享到我的计算机的选项.首先,我关闭并打开计算机上的无线网络并正常工作.然后我关闭并打开智能手机上的互联网连接,我遇到了同样的问题.

I also did it with an android smartphone using the option to share Internet to my computer via wireless. First I turned off and on the wireless on my computer and worked right. Then I turned off and on the Internet connection on the smartphone and I got the same problem.

更新 2

我的办公室有两个无线路由器.我发现如果我在路由器之间移动也会发生同样的问题.

I have two wireless router on my office. I found that the same problem happen if I move between routers.

推荐答案

Emily Stark,来自 Meteor 团队,确认这是由于当前实现中缺少一个特性(我写这个答案时的版本是 0.7.0.1).他们的答案在这里https://github.com/meteor/meteor/issues/1543.以下是他们的回答以及她建议的解决方法:

Emily Stark, from the Meteor Team, confirmed that this is due to a missing feature on the current implementation (version 0.7.0.1 at the moment I write this answer). Their answer is here https://github.com/meteor/meteor/issues/1543. Below is their answer and a workaround she suggest:

服务器到服务器的连接没有重新连接,因为 Meteor 目前不会对服务器到服务器的 DDP 连接进行任何心跳.就像在任何其他 TCP 连接中一样,一旦您切换到不同的路由器,连接上就无法发送或接收数据,但客户端不会注意到,除非它尝试发送一些数据并超时.这不同于在 SockJS 上运行的浏览器到服务器 DDP 连接.SockJS 执行自己的心跳,我们可以使用它来检测死连接.

The server-to-server connection is not reconnecting because Meteor currently doesn't do any heartbeating on server-to-server DDP connections. Just as in any other TCP connection, once you switch to a different router, no data can be sent or received on the connection, but the client will not notice unless it attempts to send some data and times out. This differs from browser-to-server DDP connections, which run over SockJS. SockJS does its own heartbeating that we can use to detect dead connections.

要查看此操作,以下是我在示例中添加到 server-b 的一些代码:

To see this in action, here is some code that I added to server-b in your example:

var heartbeatOutstanding = false;
Meteor.setInterval(function () {
  if (! heartbeatOutstanding) {
   console.log("Sending heartbeat");
    remote.call("heartbeat", function () {
      console.log("Heartbeat returned");
      heartbeatOutstanding = false;
    });
    heartbeatOutstanding = true;
  }
}, 3000);

remote.onReconnect = function () {
  console.log("RECONNECTING REMOTE");
};

在那里添加此代码后,server-b 将在足够长的时间过去后重新连接,而 server-a 将重新连接传递心跳方法调用的 TCP 段.在我的机器上,这只是几分钟,然后我收到了 ETIMEDOUT,然后重新连接.

With this code added in there, server-b will reconnect after a long enough time goes by without an ACK from server-a for the TCP segments that are delivering the heartbeat method call. On my machine, this is just a couple minutes, and I get an ETIMEDOUT followed by a reconnect.

我为我们打开了一个单独的任务,让我们考虑在下一个 bug 周期间在服务器到服务器 DDP 连接上实现心跳.同时,您始终可以在应用程序中实现心跳,以确保在客户端无法再与服务器通信时发生 DDP 重新连接.

I've opened a separate task for us to think about implementing heartbeating on server-to-server DDP connections during our next bug week. In the meantime, you can always implement heartbeating in your application to ensure that a DDP reconnection happens if the client can no longer talk to the server.

这篇关于两台服务器之间的 DDP 不会重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆