Meteor在每个订阅请求上打开多个句柄

时间:2013-08-15 08:54:28

标签: javascript node.js meteor

我正在尝试Meteor并想出一些奇怪的事情。我正在试验他们的文档中提到的这个例子(我已经修改了一下):

if (Meteor.isClient) {

  Counts = new Meteor.Collection("counts");

    // client: subscribe to the count for the current room
    Meteor.subscribe("counts-by-room", '1');

}

if (Meteor.isServer) {

  Messages = new Meteor.Collection("messages");

  Meteor.publish("counts-by-room", function (roomId) {
      var self = this;
      check(roomId, String);
      var count = 0;
      var initializing = true;
      var handle = Messages.find({roomId: roomId}).observeChanges({
       added: function (id) {
          count++;
          if (!initializing)
            self.changed("counts", roomId, {count: count});
        },
        removed: function (id) {
          count--;
          self.changed("counts", roomId, {count: count});
        }
        // don't care about moved or changed
      });



      // Observe only returns after the initial added callbacks have
      // run.  Now return an initial value and mark the subscription
      // as ready.
      initializing = false;
      self.added("counts", roomId, {count: count});
      self.ready();

      console.log("opened new handle");

      // Stop observing the cursor when client unsubs.
      // Stopping a subscription automatically takes
      // care of sending the client any removed messages.
      self.onStop(function () {
        console.log("stopping");
        handle.stop();
      });
    });
}

然后我使用Chrome Dev Console多次订阅了同一个房间,在我的服务器控制台上,每次都可以看到它正在打开新的句柄。 服务器控制台:

I20130815-14:15:07.470(5.5)? opened new handle
I20130815-14:15:37.661(5.5)? opened new handle
I20130815-14:15:38.616(5.5)? opened new handle
I20130815-14:15:39.191(5.5)? opened new handle
I20130815-14:15:39.703(5.5)? opened new handle
I20130815-14:15:40.215(5.5)? opened new handle
I20130815-14:15:40.711(5.5)? opened new handle
I20130815-14:15:41.207(5.5)? opened new handle
I20130815-14:15:41.704(5.5)? opened new handle
I20130815-14:15:42.200(5.5)? opened new handle
I20130815-14:15:42.696(5.5)? opened new handle

我想知道这是否正常,因为在他们的文档中他们说停止观察者是非常关键的。当有人故意尝试这样做时,我怎么能阻止它。我认为这个问题是某种内存泄漏,可能会导致我的服务器崩溃。我错了吗?

请帮忙。我是流星的新手:)

1 个答案:

答案 0 :(得分:0)

你不用担心。无论你是否使用Meteor,任何邪恶的人都可以向托管你网站的服务器发送许多请求并使其变慢。你无能为力。但是大多数用户都不是邪恶的,并且以您的方式使用您的网站,所以如果您编写的代码不会使服务器变慢,那么在您制造了一些敌人之前,您将非常安全。

不减慢服务器速度的一种方法是关闭用户不再需要的订阅句柄。