node-mongodb-native manually aborting running queries

夙愿已清 提交于 2019-12-12 02:05:05

问题


I'm wondering what the most efficient way to manually abort a running query with the official node client is. In my current code I am creating a new DB connection for each remote client and then simply closing those connections if the client disconnects early, which works well but seems like it might be a wasteful way of doing things (edit: indeed it is; detailed here). I imagine if it's possible to directly access the mongodb client connection pool and close specific connections rather than a whole database this might be more performant?

The mongo server is querying several large geospatial collections concurrently, so releasing resources upon clients disconnecting is an important part of performance in this implementation. There will be quite some unnecessary requests coming in - people like to click around a lot on maps even when you are throttling them.

Contrived example of my current code:

var MongoClient = require('mongodb').MongoClient,
    express = require("express"),
    app = express();

app.get('/my-route', function(request, response) {
    MongoClient.connect(connectionUri, function(err, db) {
        if (err) throw err;

        request.on("close", function() {
            db.close(true, function(err, res) {
                if (err) throw err;
            });
        });

        // make async DB queries as normal and send response when done
        // ...
    });
});

来源:https://stackoverflow.com/questions/25734779/node-mongodb-native-manually-aborting-running-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!