Skip to content

Commit

Permalink
support connect and socket timeout options, update the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrabug committed Jan 25, 2013
1 parent ed3e8b1 commit 632c160
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ directives:
``MONGO_MAX_POOL_SIZE`` (optional): The maximum number of idle connections
maintained in the PyMongo connection pool.
Default: PyMongo default.
``MONGO_SOCKET_TIMEOUT_MS`` (optional): (integer) How long (in milliseconds) a send
or receive on a socket can take before timing out.
Default: PyMongo default.
``MONGO_CONNECT_TIMEOUT_MS`` (optional): (integer) How long (in milliseconds) a
connection can take to be opened before timing out.
Default: PyMongo default.
``MONGO_DBNAME`` The database name to make available as the ``db``
attribute. Default: ``app.name``.
``MONGO_USERNAME`` The user name for authentication. Default: ``None``
Expand Down
13 changes: 12 additions & 1 deletion flask_pymongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def key(suffix):
app.config[key('PASSWORD')] = parsed['password']
app.config[key('REPLICA_SET')] = parsed['options'].get('replica_set')
app.config[key('MAX_POOL_SIZE')] = parsed['options'].get('max_pool_size')
app.config[key('SOCKET_TIMEOUT_MS')] = parsed['options'].get('socket_timeout_ms', None)
app.config[key('CONNECT_TIMEOUT_MS')] = parsed['options'].get('connect_timeout_ms', None)

# we will use the URI for connecting instead of HOST/PORT
app.config.pop(key('HOST'), None)
Expand All @@ -158,6 +160,8 @@ def key(suffix):
app.config.setdefault(key('DBNAME'), app.name)
app.config.setdefault(key('READ_PREFERENCE'), None)
app.config.setdefault(key('AUTO_START_REQUEST'), True)
app.config.setdefault(key('SOCKET_TIMEOUT_MS'), None)
app.config.setdefault(key('CONNECT_TIMEOUT_MS'), None)

# these don't have defaults
app.config.setdefault(key('USERNAME'), None)
Expand Down Expand Up @@ -190,6 +194,8 @@ def key(suffix):
dbname = app.config[key('DBNAME')]
auto_start_request = app.config[key('AUTO_START_REQUEST')]
max_pool_size = app.config[key('MAX_POOL_SIZE')]
socket_timeout_ms = app.config[key('SOCKET_TIMEOUT_MS')]
connect_timeout_ms = app.config[key('CONNECT_TIMEOUT_MS')]

# document class is not supported by URI, using setdefault in all cases
document_class = app.config.setdefault(key('DOCUMENT_CLASS'), None)
Expand All @@ -199,11 +205,16 @@ def key(suffix):

args = [host]
kwargs = {
'auto_start_request': auto_start_request,
'read_preference': read_preference,
'tz_aware': True,
}

kwargs['auto_start_request'] = auto_start_request
if socket_timeout_ms is not None:
kwargs['socketTimeoutMS'] = socket_timeout_ms

if connect_timeout_ms is not None:
kwargs['connectTimeoutMS'] = connect_timeout_ms

if replica_set is not None:
kwargs['replicaSet'] = replica_set
Expand Down

0 comments on commit 632c160

Please sign in to comment.