実際にここで行うべきことがいくつかあります。そうでない場合は使用時に最初に注目すべき事は発行され、実際に廃止の警告に示されるように代わりに.stream()
の.cursor()
方法を使用することです:
DeprecationWarning:マングースは:Query.prototype.stream()> = 4.5マングースに廃止されました0.0、Query.prototype.cursor()を代わりに使用し
第二の注目すべき事.cursor()
マニュアルに指定されているように、これは今、基礎となるドライバから直接「ラップストリーム」のインターフェイスを返すことです。したがって、.tailable()
メソッドの代わりに現代.addCursorFlag()
オプションを使用することをお勧めします。Query
両方の対策が整ったら、mongod
とnode
プロセスの両方のアイドルCPUが更新間隔の間に0%
に低下しています。
これは、次のリストで最もよくシミュレートされます。実際の書き込みとしてキャプチャ
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
const uri = 'mongodb://localhost/tailing',
options = { useMongoClient: true };
const subSchema = new Schema({
name: String
},{
capped: { size: 1024, max: 1000 }
});
const Sub = mongoose.model('Sub', subSchema);
function log(data) {
console.log(JSON.stringify(data, undefined, 2))
}
(async function() {
try {
const conn = await mongoose.connect(uri,options);
//await Sub.remove({});
await Sub.insertMany(Array(50).fill(1).map((e,i) => ({ name: i+1 })));
let stream = Sub.find()
.cursor()
.addCursorFlag('tailable',true)
.addCursorFlag('awaitData',true);
/*
.tailable({
await_data: true,
numberOfRetries: -1
})
.cursor();
*/
stream.on('data',function(data) {
log(data);
});
let counter = 50;
setInterval(async function() {
counter++;
await Sub.insertMany({ name: counter });
},10000);
} catch(e) {
console.log(e);
} finally {
//mongoose.disconnect();
}
})();
昔ながらのトップ出力は、場所をとります。
top - 21:38:29 up 12 days, 1:23, 3 users, load average: 0.06, 0.03, 0.04
Tasks: 116 total, 2 running, 114 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 98.6 id, 0.7 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2045968 total, 207452 free, 813908 used, 1024608 buff/cache
KiB Swap: 2097148 total, 2097124 free, 24 used. 1028156 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1257 mongodb 20 0 1946896 487336 34892 S 0.7 23.8 130:37.67 mongod
28233 neillunn 20 0 1021460 41920 22996 S 0.3 2.0 0:00.67 node
30956 neillunn 20 0 101472 4384 3352 S 0.3 0.2 0:20.95 sshd
それは、クライアントが「ポーリング」と、実際に効率的な方法でイベントを待っているではない可能性があります。モンゴースバージョン?そしてMongoDBのバージョンは接続してください。また、私はクライアントとサーバーの両方が同じマシン上で動作していると想定していますか? –
申し訳ありません。私は 'node'バージョンも忘れていました。しかし、実際には何よりも 'mongod'を指し示すように見えます。特にOSX上で動作します。 –
Ubuntuで実行しているときと同じ問題があります。それはUbuntu上では高くはなく、過熱ではありませんが、mongodは〜80%のCPUを使用しています。 –