【问题标题】:Meteor - detecting infinite loopMeteor - 检测无限循环
【发布时间】:2017-04-24 10:00:39
【问题描述】:

以下是用于计数事物的 Meteor 应用程序的一些代码(带有相关数据)的精髓。计数可以链接在一起,以便一个可以增加另一个:

// The counting and linking code.
Meteor.methods({
    'counts.increment'(countId) {
        const count = Counts.findOne(countId);
        Counts.update(countId,{ $inc: { count: 1 } })
        Meteor.call('counts.check-links',count._id);
    },
    'counts.check-links'(countId){
        var count = Counts.findOne(countId);
        count.links.forEach(function (linkId) {
            updated = false;
            const link = Links.findOne(linkId);
            const primary = Counts.findOne(link.primary);
            const secondary = Counts.findOne(link.secondary);
            if (primary.count == link.level) {
                updated = true;
                Counts.update(secondary._id, {$inc: {count: 1}});
            }
            if (updated) {
                Meteor.call('counts.check-links',secondary._id);
            }
        })
    }
})

// Some data...
Count: {
  _id: 1,
  value: 0,
  name: A,
  links: [3]
}

Count: {
  _id: 2,
  value: 0,
  name: B,
  links: [4]
}

Link: {
  _id: 3,
  primary: 1,
  secondary: 2
}

Link: {
  _id: 4,
  primary: 2,
  secondary: 1
}

因此,如果调用Meteor.call('projects.increment',1),此代码将崩溃,因为每个计数都链接到另一个计数。检测这样的设置可能相当困难,因为可能存在非常复杂的计数安排,并且链接也可能减少,设置为零,每 N 个计数操作一次等等。 &C。不过,出于提出这个问题的目的,这并不重要。

我想到的一种可能性是在counts.check-links 中添加一个计数器,这将在任意数量的循环后停止执行,例如5. 据推测,为了防止篡改该计数器的值,必须将其存储在数据库中并通过 Meteor 方法进行操作。它需要在任何check-links 调用序列结束时重置。

我不确定这是否是最好的想法,或者如果是,那么如何实现它的好方法,所以我很想知道是否有人有任何建议。

【问题讨论】:

  • 确保验证您的服务器方法,否则很容易伪造此方法调用或进行一些 nosql 注入。
  • 服务器只发布属于登录用户的数据,并且方法调用还检查是否正在对登录用户拥有的数据执行操作,因此有望覆盖它。跨度>
  • 我应该补充一点,在实际应用程序中也会检查所有 Meteor.method 参数 - 我在这里的示例中省略了这类事情。

标签: javascript meteor halting-problem


【解决方案1】:

您可以创建一组已访问过的所有对象(“计数”);因此,如果您点击指向此类对象的链接,则可以避免再次处理它,从而陷入无限递归。

编辑:示例 我对流星不熟悉,所以如果它不能按预期工作,请原谅......这是所有允许对象引用指针的编程语言的常见问题,并且解决方案遵循类似的模式。

// The counting and linking code.
Meteor.methods({
  ...
'counts.check-links'(countId, alreadyVisited){

    if (!alreadyVisited) alreadyVisited = {};
    if (alreadyVisited[countId]) return;
    alreadyVisited[countId] = true;

    var count = Counts.findOne(countId);
    count.links.forEach(function (linkId) {
        updated = false;
        const link = Links.findOne(linkId);
        const primary = Counts.findOne(link.primary);
        const secondary = Counts.findOne(link.secondary);
        if (primary.count == link.level) {
            updated = true;
            Counts.update(secondary._id, {$inc: {count: 1}});
        }
        if (updated) {
            Meteor.call('counts.check-links',secondary._id, alreadyVisited);
        }
    })
}

【讨论】:

  • 谢谢,我会试一试的。大概应该在最后的 else 块中清空 alreadyVisited ,因为如果没有任何更新,则无需检查更多链接。
  • 看起来成功了。出于安全原因,我没有在方法中创建已经访问过,而是将其存储在数据库中,以便我可以应用一些检查(例如登录用户)。否则我就用了你放在那里的东西。
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多