V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xnile
V2EX  ›  Node.js

求解一个 nodejs 内存泄露问题

  •  
  •   xnile · 2018-11-27 18:23:32 +08:00 · 2988 次点击
    这是一个创建于 1949 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 里边

    2 条回复    2018-11-28 09:06:07 +08:00
    TomVista
        1
    TomVista  
       2018-11-27 21:52:01 +08:00 via iPhone
    return 函数对象形成的闭包导致,对象用完指定为 null,应该是这个理
    xnile
        2
    xnile  
    OP
       2018-11-28 09:06:07 +08:00 via iPhone
    @TomVista 感谢指点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2770 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:31 · PVG 20:31 · LAX 05:31 · JFK 08:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.