Refactored MongoDB connection pool to use weakrefs

This allows connections to be closed when they are not used anymore

Change-Id: I54b0260f92376dd66dfda1ddb9405d6e0971f9d9
This commit is contained in:
Alexei Kornienko 2013-07-26 15:18:56 +03:00
parent 2f69200e98
commit 1aba51ec6e

View File

@ -26,6 +26,7 @@ import datetime
import operator import operator
import os import os
import uuid import uuid
import weakref
import bson.code import bson.code
import bson.objectid import bson.objectid
@ -145,14 +146,17 @@ class ConnectionPool(object):
self._pool = {} self._pool = {}
def connect(self, url): def connect(self, url):
if url not in self._pool: if url in self._pool:
LOG.info('connecting to MongoDB on %s', url) client = self._pool.get(url)()
self._pool[url] = pymongo.MongoClient( if client:
url, return client
use_greenlets=True, LOG.info('connecting to MongoDB on %s', url)
safe=True) client = pymongo.MongoClient(
url,
return self._pool.get(url) use_greenlets=True,
safe=True)
self._pool[url] = weakref.ref(client)
return client
class Connection(base.Connection): class Connection(base.Connection):