mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import time
|
|
import logging
|
|
|
|
from pymongo import MongoClient
|
|
from pymongo.errors import ConnectionFailure
|
|
|
|
import bigchaindb
|
|
from bigchaindb.backend.connection import Connection
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class MongoDBConnection(Connection):
|
|
|
|
def __init__(self, host=None, port=None, dbname=None, max_tries=3):
|
|
"""Create a new Connection instance.
|
|
|
|
Args:
|
|
host (str, optional): the host to connect to.
|
|
port (int, optional): the port to connect to.
|
|
dbname (str, optional): the database to use.
|
|
max_tries (int, optional): how many tries before giving up.
|
|
"""
|
|
|
|
self.host = host or bigchaindb.config['database']['host']
|
|
self.port = port or bigchaindb.config['database']['port']
|
|
self.dbname = dbname or bigchaindb.config['database']['name']
|
|
self.max_tries = max_tries
|
|
self.conn = None
|
|
|
|
@property
|
|
def db(self):
|
|
if self.conn is None:
|
|
self._connect()
|
|
|
|
else:
|
|
return self.conn[self.dbname]
|
|
|
|
def _connect(self):
|
|
for i in range(self.max_tries):
|
|
try:
|
|
self.conn = MongoClient(self.host, self.port)
|
|
except ConnectionFailure as exc:
|
|
if i + 1 == self.max_tries:
|
|
raise
|
|
else:
|
|
time.sleep(2**i)
|