Skip to main content

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 buildMongoURI helper in services/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 (None or empty string "") -> mongodb://localhost:27017/betsharemarket (the TS helper uses if (!baseURI), which treats "" the same as undefined).
  • 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 /betsharemarket inserted before any query string.
  • mongodb+srv:// is handled identically to mongodb://.
  • A URI not matching the expected shape gets /betsharemarket appended (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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betshare_infra_mongo-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

betshare_infra_mongo-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file betshare_infra_mongo-0.1.0.tar.gz.

File metadata

  • Download URL: betshare_infra_mongo-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/25.5.0

File hashes

Hashes for betshare_infra_mongo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f060c3c2485002e43bf464967e5a8adc22d302966bd6043f1114900fe1dbccff
MD5 b20ce2ab7709365e1ca912fc1e192564
BLAKE2b-256 68b77c08ba11f6f381c4cc468b731c519d82497df098002c1e2e9bfda7663e78

See more details on using hashes here.

File details

Details for the file betshare_infra_mongo-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for betshare_infra_mongo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bceffe6005238a84a297fc0ab3f7b7faae6ed8270d9106da7366ea948e58bfd
MD5 a4fe2b56540585a9a54f79e0f01da25c
BLAKE2b-256 7189a56332de02cb1318a6f1bf3c1d553374091446040e75e8b152aaeae21dc1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page