Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed the connection leak

  • Loading branch information...
commit 65df785781ba1f2bc98a1b11b6a59da00465c3ff 1 parent 0d2d3ad
@toantran authored
View
13 coffee/repository/base.coffee
@@ -5,7 +5,7 @@ mongo = require 'mongodb'
dbName = 'wifefoosdb'
host = process.env['MONGO_NODE_DRIVER_HOST'] ? 'localhost'
-port = process.env['MONGO_NODE_DRIVER_PORT'] ? Connection.DEFAULT_PORT;
+port = process.env['MONGO_NODE_DRIVER_PORT'] ? Connection.DEFAULT_PORT
exports.setCollectionName = (@collectionName) ->
@@ -89,6 +89,7 @@ repository::create = (doc, callback = ->) ->
callback.apply this, arguments
db.close()
catch e
+ db.close()
console.trace e
callback e
@@ -109,6 +110,7 @@ repository::read = (findArgs..., callback = ->) ->
catch e
console.trace e
callback e
+ db.close()
repository::getById = (docid, callback = ->) ->
@@ -122,6 +124,10 @@ repository::getById = (docid, callback = ->) ->
return callback(toArrayErr) if toArrayErr
return callback('Not found') unless docs?.length
callback null, docs[0]
+ db = cursor.db
+ cursor.close()
+ db.close()
+
repository::update = (criteria, objNew, options = {}, callback ) ->
@@ -139,6 +145,7 @@ repository::update = (criteria, objNew, options = {}, callback ) ->
db.close()
callback.apply this, arguments
catch e
+ db.close()
console.trace e
callback e if callback?
@@ -155,6 +162,7 @@ repository::save = (doc, callback = ->) ->
db.close()
callback.apply this, arguments
catch e
+ db.close()
console.trace e
callback e if callback?
@@ -168,7 +176,8 @@ repository::remove = (criteria, callback = ->) ->
@getCollection db, (collectionErr, collection) ->
checkError collectionErr, errorFn, ->
collection.remove criteria
- callback()
+ callback()
+ db.close()
exports.repository = repository
View
37 coffee/repository/matches2.coffee
@@ -16,40 +16,3 @@ exports.remove = () ->
exports.getById = () ->
baseRepo.getById.apply baseRepo, arguments
exports.ObjectId = ObjectId
-
-
-exports.addVote = (matchid, vote, callback = ->) ->
- console.assert matchid, 'matchid cannot be null or 0'
- throw 'matchid cannot be null or 0' unless matchid
-
- matchid = new ObjectId(matchid) if typeof matchid is 'string'
-
- findObj =
-
- baseRepo.update
-
- getCollection(db, function(error, collection) {
- checkErrorFn( error, errorFn, function() {
- collection.findAndModify( {
- _id: new ObjectId(matchid)
- }, {
- }, {
- $addToSet: {
- votes: vote
- }
- , $set: {
- updatedat: new Date()
- }
- }, {
- safe: true
- , 'new': true
- }, function(error, m) {
- if (m && (m.length === +m.length)) {
- m = m[0];
- }
- callback( error, m);
- db.close();
- });
- });
- });
-}
View
3  coffee/repository/users2.coffee
@@ -30,6 +30,7 @@ exports.getByUsername = (username, callback = ->) ->
if err?
callback err
else if cursor?
+ db = cursor.db
cursor.toArray (toArrayErr, users) ->
if toArrayErr?
callback toArrayErr
@@ -37,6 +38,8 @@ exports.getByUsername = (username, callback = ->) ->
callback null, users[0]
else
callback null, null
+ cursor.close()
+ db.close()
else
callback 'DB read failed'
View
12 coffee/services/match.coffee
@@ -26,7 +26,11 @@ exports.getCompleteMatches = (callback = ->) ->
if readErr?
callback readErr
else if cursor?
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
else
callback()
@@ -46,7 +50,11 @@ exports.getPendingExpiredMatches = (callback) ->
if readErr?
callback readErr
else if cursor?
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
else
callback()
View
13 coffee/services/news.coffee
@@ -4,13 +4,22 @@ exports.getNews = (callback = ->) ->
newsrepo.read {}, (err, cursor) ->
return callback(err) if err? or not cursor?
cursor.sort createdat: -1
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
exports.getHighlights = (callback = ->) ->
newsrepo.read {highlight: 1}, (err, cursor) ->
return callback(err) if err? or not cursor?
cursor.sort createdat: -1
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
+
createNewsContent = (tpl, data) ->
View
14 coffee/services/team.coffee
@@ -108,7 +108,12 @@ exports.getChallenge = getChallenge = (teamid, opponentid, callback = ->) ->
teamid: opponentid
, {}, (err, cursor) ->
return callback(err) if err
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
+
catch e
console.trace e
throw e
@@ -627,7 +632,12 @@ exports.getAll = (availableOnly, callback = ->) ->
if readErr?
callback readErr
else if cursor?
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
+
else
callback()
View
14 coffee/services/user.coffee
@@ -572,7 +572,12 @@ exports.getAllPlayers = (callback = ->) ->
if readErr?
callback readErr
else if cursor?
- cursor.toArray callback
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
+
else
callback()
@@ -616,7 +621,12 @@ exports.getUserByToken = (token, callback = ->) ->
if err
callback err
else
- cursor.toArray cb
+ cursor.toArray ->
+ db = cursor.db
+ callback.apply null, arguments
+ cursor.close()
+ db.close()
+
.then (err, users, cb = ->) ->
if err
callback err
View
15 repository/base.js
@@ -127,6 +127,7 @@
return db.close();
});
} catch (e) {
+ db.close();
console.trace(e);
return callback(e);
}
@@ -148,7 +149,8 @@
return callback(null, cursor);
} catch (e) {
console.trace(e);
- return callback(e);
+ callback(e);
+ return db.close();
}
});
});
@@ -166,9 +168,13 @@
}, function(readErr, cursor) {
if (readErr) return callback(readErr);
return cursor.toArray(function(toArrayErr, docs) {
+ var db;
if (toArrayErr) return callback(toArrayErr);
if (!(docs != null ? docs.length : void 0)) return callback('Not found');
- return callback(null, docs[0]);
+ callback(null, docs[0]);
+ db = cursor.db;
+ cursor.close();
+ return db.close();
});
});
};
@@ -190,6 +196,7 @@
return callback.apply(this, arguments);
});
} catch (e) {
+ db.close();
console.trace(e);
if (callback != null) return callback(e);
}
@@ -213,6 +220,7 @@
return callback.apply(this, arguments);
});
} catch (e) {
+ db.close();
console.trace(e);
if (callback != null) return callback(e);
}
@@ -229,7 +237,8 @@
return this.getCollection(db, function(collectionErr, collection) {
return checkError(collectionErr, errorFn, function() {
collection.remove(criteria);
- return callback();
+ callback();
+ return db.close();
});
});
};
View
10 repository/users2.js
@@ -39,17 +39,21 @@
return baseRepo.read({
username: username
}, function(err, cursor) {
+ var db;
if (err != null) {
return callback(err);
} else if (cursor != null) {
+ db = cursor.db;
return cursor.toArray(function(toArrayErr, users) {
if (toArrayErr != null) {
- return callback(toArrayErr);
+ callback(toArrayErr);
} else if (users != null ? users.length : void 0) {
- return callback(null, users[0]);
+ callback(null, users[0]);
} else {
- return callback(null, null);
+ callback(null, null);
}
+ cursor.close();
+ return db.close();
});
} else {
return callback('DB read failed');
View
16 services/match.js
@@ -33,7 +33,13 @@
if (readErr != null) {
return callback(readErr);
} else if (cursor != null) {
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
} else {
return callback();
}
@@ -57,7 +63,13 @@
if (readErr != null) {
return callback(readErr);
} else if (cursor != null) {
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
} else {
return callback();
}
View
16 services/news.js
@@ -11,7 +11,13 @@
cursor.sort({
createdat: -1
});
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
});
};
@@ -24,7 +30,13 @@
cursor.sort({
createdat: -1
});
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
});
};
View
16 services/team.js
@@ -146,7 +146,13 @@
}
}, {}, function(err, cursor) {
if (err) return callback(err);
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
});
} catch (e) {
console.trace(e);
@@ -873,7 +879,13 @@
if (readErr != null) {
return callback(readErr);
} else if (cursor != null) {
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
} else {
return callback();
}
View
16 services/user.js
@@ -748,7 +748,13 @@
if (readErr != null) {
return callback(readErr);
} else if (cursor != null) {
- return cursor.toArray(callback);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
} else {
return callback();
}
@@ -802,7 +808,13 @@
if (err) {
return callback(err);
} else {
- return cursor.toArray(cb);
+ return cursor.toArray(function() {
+ var db;
+ db = cursor.db;
+ callback.apply(null, arguments);
+ cursor.close();
+ return db.close();
+ });
}
}).then(function(err, users, cb) {
if (cb == null) cb = function() {};
Please sign in to comment.
Something went wrong with that request. Please try again.