|
1 | 1 | from pymongo import MongoClient
|
2 | 2 | import os
|
3 | 3 | from dotenv import load_dotenv
|
4 |
| -import threading |
5 |
| -import time |
| 4 | +import pymongo |
6 | 5 |
|
7 | 6 | load_dotenv()
|
8 | 7 |
|
|
14 | 13 | file_name = "/etc/ssl/ca-certificate.crt"
|
15 | 14 | use_tls = True
|
16 | 15 |
|
17 |
| -# Initialize MongoDB client |
| 16 | +# Connection pooling settings |
| 17 | +max_pool_size = 10 # Maximum number of connections in the pool |
| 18 | +min_pool_size = 3 # Minimum number of connections to maintain |
| 19 | +max_idle_time_ms = 60000 # 1 minute |
| 20 | + |
| 21 | +# Initialize MongoDB client with optimized settings |
18 | 22 | if use_tls:
|
19 | 23 | client = MongoClient(
|
20 | 24 | os.getenv("MONGO_URI"),
|
21 | 25 | tls=True,
|
22 | 26 | tlsCAFile=file_name,
|
| 27 | + maxPoolSize=max_pool_size, |
| 28 | + minPoolSize=min_pool_size, |
| 29 | + maxIdleTimeMS=max_idle_time_ms, |
| 30 | + waitQueueTimeoutMS=2000, # Wait 2 seconds for connection before timeout |
| 31 | + retryWrites=True, |
| 32 | + # Remove serverSelectionTimeoutMS to use MongoDB default |
23 | 33 | )
|
24 | 34 | else:
|
25 |
| - client = MongoClient(os.getenv("MONGO_URI")) |
| 35 | + client = MongoClient( |
| 36 | + os.getenv("MONGO_URI"), |
| 37 | + maxPoolSize=max_pool_size, |
| 38 | + minPoolSize=min_pool_size, |
| 39 | + maxIdleTimeMS=max_idle_time_ms, |
| 40 | + waitQueueTimeoutMS=2000, |
| 41 | + retryWrites=True, |
| 42 | + ) |
26 | 43 |
|
27 | 44 | # Force connection on startup
|
28 | 45 | try:
|
|
32 | 49 | print(f"❌ MongoDB connection failed on startup: {e}")
|
33 | 50 | raise e
|
34 | 51 |
|
35 |
| - |
36 |
| -# Start a keep-alive thread |
37 |
| -def keep_connection_alive(): |
38 |
| - while True: |
39 |
| - try: |
40 |
| - client.admin.command("ping") |
41 |
| - print("🔁 MongoDB keep-alive ping successful") |
42 |
| - except Exception as e: |
43 |
| - print(f"⚠️ MongoDB keep-alive ping failed: {e}") |
44 |
| - time.sleep(300) # ping every 5 minutes |
45 |
| - |
46 |
| - |
47 |
| -threading.Thread(target=keep_connection_alive, daemon=True).start() |
48 |
| - |
49 | 52 | # Access the database
|
50 | 53 | db = client[os.getenv("MONGO_DB", "score_db")]
|
51 | 54 |
|
52 | 55 |
|
| 56 | +# Setup indexes only if they don't exist |
53 | 57 | def setup_database_indexes():
|
54 | 58 | """Set up MongoDB indexes for optimal query performance"""
|
55 | 59 | try:
|
56 | 60 | game_collection = db["game"]
|
57 | 61 |
|
58 |
| - # Create single field indexes for commonly queried fields |
59 |
| - game_collection.create_index([("sport", 1)], background=True) |
60 |
| - game_collection.create_index([("date", 1)], background=True) |
61 |
| - game_collection.create_index([("gender", 1)], background=True) |
62 |
| - |
63 |
| - # Create compound indexes for common query combinations |
64 |
| - game_collection.create_index([("sport", 1), ("gender", 1)], background=True) |
65 |
| - |
66 |
| - # Index for sorting operations |
67 |
| - game_collection.create_index([("date", -1)], background=True) |
68 |
| - |
69 |
| - print("✅ MongoDB indexes created successfully") |
| 62 | + # Get existing indexes |
| 63 | + existing_indexes = game_collection.index_information() |
| 64 | + |
| 65 | + # Define indexes we want |
| 66 | + indexes = [ |
| 67 | + ("sport", pymongo.ASCENDING), |
| 68 | + ("date", pymongo.ASCENDING), |
| 69 | + ("gender", pymongo.ASCENDING), |
| 70 | + [("sport", pymongo.ASCENDING), ("gender", pymongo.ASCENDING)], |
| 71 | + ("date", pymongo.DESCENDING), |
| 72 | + ] |
| 73 | + |
| 74 | + # Create only missing indexes |
| 75 | + for index in indexes: |
| 76 | + # Convert single field index to list format for consistency |
| 77 | + index_list = [index] if not isinstance(index, list) else index |
| 78 | + |
| 79 | + # Generate index name |
| 80 | + index_name = "_".join( |
| 81 | + [f"{field}_{direction}" for field, direction in index_list] |
| 82 | + ) |
| 83 | + |
| 84 | + if index_name not in existing_indexes: |
| 85 | + game_collection.create_index(index_list, background=True) |
| 86 | + print(f"Created index: {index_name}") |
| 87 | + |
| 88 | + print("✅ MongoDB indexes verified") |
70 | 89 | except Exception as e:
|
71 |
| - print(f"❌ Failed to create MongoDB indexes: {e}") |
72 |
| - raise e |
| 90 | + print(f"❌ Failed to verify MongoDB indexes: {e}") |
| 91 | + # Don't raise exception here, just log the error |
73 | 92 |
|
74 | 93 |
|
| 94 | +# Setup indexes |
75 | 95 | setup_database_indexes()
|
0 commit comments