node-mongodb-native manually aborting running queries
Asked Answered
H

0

0

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
        // ...
    });
});
Howlond answered 8/9, 2014 at 23:51 Comment(2)
possible duplicate of How to manage mongodb connections in a nodejs webapp?Conventioner
Actually I am now thinking this isn't so much a best-practises connection management question as it is a specific requirement for manual intervention.Howlond

© 2022 - 2024 — McMap. All rights reserved.