diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index e917e326..3b2459ac 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -1,5 +1,4 @@ import rethinkdb as r - from bigchaindb.backend.connection import Connection from bigchaindb.backend.exceptions import ConnectionError, OperationError @@ -40,6 +39,9 @@ class RethinkDBConnection(Connection): """ try: - return r.connect(host=self.host, port=self.port, db=self.dbname) - except r.ReqlDriverError as exc: + return r.connect(host=self.host, + port=self.port, + db=self.dbname, + timeout=self.connection_timeout) + except (r.ReqlDriverError, r.ReqlTimeoutError) as exc: raise ConnectionError from exc diff --git a/docs/server/source/server-reference/configuration.md b/docs/server/source/server-reference/configuration.md index 2af4cbb1..3cb62c41 100644 --- a/docs/server/source/server-reference/configuration.md +++ b/docs/server/source/server-reference/configuration.md @@ -100,7 +100,7 @@ The settings with names of the form `database.*` are for the database backend * `database.port` is self-explanatory. * `database.name` is a user-chosen name for the database inside RethinkDB or MongoDB, e.g. `bigchain`. * `database.replicaset` is only relevant if using MongoDB; it's the name of the MongoDB replica set, e.g. `bigchain-rs`. -* `database.connection_timeout` is the maximum number of milliseconds that BigchainDB will wait before giving up on one attempt to connect to the database backend. Note: At the time of writing, this setting was only used by MongoDB; there was an open [issue to make RethinkDB use it as well](https://github.com/bigchaindb/bigchaindb/issues/1337). +* `database.connection_timeout` is the maximum number of milliseconds that BigchainDB will wait before giving up on one attempt to connect to the database backend. * `database.max_tries` is the maximum number of times that BigchainDB will try to establish a connection with the database backend. If 0, then it will try forever. **Example using environment variables** diff --git a/tests/backend/rethinkdb/test_connection.py b/tests/backend/rethinkdb/test_connection.py index df393716..c9703a6b 100644 --- a/tests/backend/rethinkdb/test_connection.py +++ b/tests/backend/rethinkdb/test_connection.py @@ -137,11 +137,28 @@ def test_changefeed_reconnects_when_connection_lost(monkeypatch): @patch('rethinkdb.connect') def test_connection_happens_one_time_if_successful(mock_connect): + import bigchaindb from bigchaindb.backend import connect + timeout = bigchaindb.config['database']['connection_timeout'] query = r.expr('1') conn = connect('rethinkdb', 'localhost', 1337, 'whatev') conn.run(query) mock_connect.assert_called_once_with(host='localhost', port=1337, - db='whatev') + db='whatev', + timeout=timeout) + + +@patch('rethinkdb.connect', side_effect=r.ReqlTimeoutError()) +def test_connection_timeout(mock_connect): + from bigchaindb.backend import connect + from bigchaindb.backend.exceptions import ConnectionError + + query = r.expr('1') + conn = connect() + + # connection should raise a ConnectionError after 3 tries + with pytest.raises(ConnectionError): + conn.run(query) + assert mock_connect.call_count == 3