求解一个 nodejs 内存泄露问题

2018-11-27 18:23:32 +08:00
 xnile

tail () { const ts = this.lastTs const nss = this.defs.map(def => def.ns) const filters = { ns: { $in: nss }, ts: { $gt: Timestamp.fromNumber(ts) } } const curOpts = { tailable: true, awaitdata: true, numberOfRetries: 60 * 60 * 24, // Number.MAX_VALUE, tailableRetryInterval: 1000 }

util.log(`Begin to watch... (from ${ts})`)
return new Promise((resolve, reject) =>
  mongodb.connect(this.url2).then(db => {
    this.db = db
    const stream = db.collection('oplog.rs').find(filters, curOpts).stream()
    stream
      .on('data', log => {
        if (log.op === 'n' || log.ts.toNumber() === ts) return
        this.process(log)
      })
      .on('close', () => {
        util.log('Stream closed....')
        this.db = null
        db.close()
        resolve()
      })
      .on('error', err => {
        this.db = null
        db.close()
        reject(err)
      })
  }))

}

目的是读取 monogodb 的 oplog 将数据同步到 mysql 里边

3016 次点击
所在节点    Node.js
2 条回复
TomVista
2018-11-27 21:52:01 +08:00
return 函数对象形成的闭包导致,对象用完指定为 null,应该是这个理
xnile
2018-11-28 09:06:07 +08:00
@TomVista 感谢指点

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/512053

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX