Shared MongoDB connection-policy helpers for BetShareMarket Python services
Project description
betshare-infra-mongo
Shared MongoDB connection-policy helpers for BetShareMarket Python services.
These are pure helpers — there is no MongoClient, no driver wrapping, no
runtime dependency. Every function returns a plain str or dict so the
result can be splatted directly into pymongo's MongoClient or motor's
AsyncIOMotorClient:
from pymongo import MongoClient
from betshare_infra_mongo import build_mongo_uri, mongo_options, resolve_db_name
client = MongoClient(build_mongo_uri(os.getenv("MONGO_URI")), **mongo_options(max_pool_size=5))
db = client[resolve_db_name()]
Import name: betshare_infra_mongo. Distribution name: betshare-infra-mongo.
Why
The connection-pool / timeout knobs were copy-pasted (and drifted) across
consumer, joiner-events, and betfair-exchange-scraper. Uncapped or
inconsistent pools contributed to Cosmos vCore connection exhaustion. This
package centralises:
- the fleet socket-timeout policy (120s default),
- a required
max_pool_size(no default) so every call site declares an explicit pool cap that can be audited, - the db-name resolution (
MONGODB_DB_NAME->betsharemarket), - the URI normalisation (insert the db name when missing), kept in parity
with the TS
buildMongoURIhelper inservices/odds-worker.
Install
Production (private registry, version-pinned per service):
# in the service's pyproject.toml
betshare-infra-mongo = "^0.1.0"
Local development (PATH dependency, used by the migration):
betshare-infra-mongo = { path = "../../packages/py-infra-mongo", develop = true }
Or with pip: pip install -e packages/py-infra-mongo.
Public interface
def build_mongo_uri(base_uri: str | None) -> str: ...
def resolve_db_name() -> str: ...
def mongo_options(*, max_pool_size: int,
server_selection_timeout_ms: int = 30000,
socket_timeout_ms: int = 120000,
connect_timeout_ms: int = 10000,
**overrides) -> dict: ...
build_mongo_uri
Ensures the URI carries a database name. Parity with
services/odds-worker/src/utils/buildMongoURI.ts:
- Falsy input (
Noneor empty string"") ->mongodb://localhost:27017/betsharemarket(the TS helper usesif (!baseURI), which treats""the same asundefined). - A URI with an existing db path (anything but a bare
/) is returned unchanged, query string included. - A URI with no db path (or a bare trailing
/) gets/betsharemarketinserted before any query string. mongodb+srv://is handled identically tomongodb://.- A URI not matching the expected shape gets
/betsharemarketappended (same fallback as the TS helper).
resolve_db_name
os.environ.get("MONGODB_DB_NAME") or "betsharemarket".
mongo_options
Returns a camelCase kwargs dict (maxPoolSize, serverSelectionTimeoutMS,
socketTimeoutMS, connectTimeoutMS) — the exact keys both pymongo and motor
accept. max_pool_size is required (omitting it raises TypeError). Extra
**overrides (camelCase, e.g. minPoolSize=1, maxIdleTimeMS=600000) pass
through verbatim and win over the named defaults.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters