-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmplConnPool.py
58 lines (46 loc) · 1.66 KB
/
SmplConnPool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from pymongo import MongoClient
import random
"""
This class implements a very simple MongoDB connection pool
which should be used to get a connection to the MongoDB throughout
the entire application
Usage:
pool = SmplConnPool.get_instance()
connection = pool.get_connection()
collection = connection.database.collection
The get_connection() method of SmplConnPool returns a normal
connection instance from pymongo.
"""
class SmplConnPool(object):
_instance = None
_pool = list()
def __init__(self, address='localhost', port=27017, size=2):
"""
This is the constructor of the SmplConnPool
:param address: the address where the MongoDB is running
:param port: the port the MongoDB is listening on
:param size: the pool size
"""
if SmplConnPool._instance is not None:
raise NotImplemented("This is a singleton class. Use the get_instance() method")
self.address = address
self.port = port
for i in range(size):
SmplConnPool._pool.append(MongoClient(address, port))
@staticmethod
def get_instance():
"""
This method returns an instance of the SmplConnPool class.
:return: an instance of the SmplConnPool class.
"""
if SmplConnPool._instance is None:
SmplConnPool._instance = SmplConnPool()
return SmplConnPool._instance
def get_connection(self):
"""
This method returns a random connection drawn
from the available connections pool.
:return: a random connection.
"""
if SmplConnPool._pool:
return random.choice(SmplConnPool._pool)