|
13 | 13 | import threading |
14 | 14 | import time |
15 | 15 | import weakref |
16 | | -from .configs import BaseOperationRetryConfig, BaseRetryConfig |
| 16 | +from .configs import BaseOperationRetryConfig, BaseRetryConfig, merge_config_with_settings |
17 | 17 | from .retry import retry_operation, retry_operation_async |
18 | 18 | from .settings import ( |
19 | 19 | get_mongodb_settings, |
@@ -49,6 +49,13 @@ class PersistentConnectionConfig: |
49 | 49 | auto_reconnect: bool | None = None |
50 | 50 |
|
51 | 51 |
|
| 52 | +_PERSISTENT_CONFIG_FIELD_MAP: dict[str, str] = { |
| 53 | + "idle_timeout": "persistent_idle_timeout", |
| 54 | + "health_check_interval": "persistent_health_check_interval", |
| 55 | + "auto_reconnect": "persistent_auto_reconnect", |
| 56 | +} |
| 57 | + |
| 58 | + |
52 | 59 | # Global registry for persistent connections (weak references to allow cleanup) |
53 | 60 | _persistent_connections: weakref.WeakValueDictionary[str, BasePersistentConnection | PersistentMongoDBConnection] = ( |
54 | 61 | weakref.WeakValueDictionary() |
@@ -772,19 +779,7 @@ def __new__( |
772 | 779 | if schema and schema != "public": |
773 | 780 | connection_key += f"?schema={schema}" |
774 | 781 |
|
775 | | - # Build config from settings, allowing partial overrides |
776 | | - _cfg = config or PersistentConnectionConfig() |
777 | | - config = PersistentConnectionConfig( |
778 | | - idle_timeout=_cfg.idle_timeout if _cfg.idle_timeout is not None else _settings.persistent_idle_timeout, |
779 | | - health_check_interval=( |
780 | | - _cfg.health_check_interval |
781 | | - if _cfg.health_check_interval is not None |
782 | | - else _settings.persistent_health_check_interval |
783 | | - ), |
784 | | - auto_reconnect=( |
785 | | - _cfg.auto_reconnect if _cfg.auto_reconnect is not None else _settings.persistent_auto_reconnect |
786 | | - ), |
787 | | - ) |
| 782 | + config = merge_config_with_settings(PersistentConnectionConfig, config, _settings, _PERSISTENT_CONFIG_FIELD_MAP) |
788 | 783 |
|
789 | 784 | # Build SSL connect_args from settings |
790 | 785 | ssl_mode = _settings.ssl_mode |
@@ -960,19 +955,7 @@ def __new__( |
960 | 955 | database = database or _settings.database |
961 | 956 | connection_key = f"mysql://{user}@{host}:{port}/{database}" # NOSONAR |
962 | 957 |
|
963 | | - # Build config from settings, allowing partial overrides |
964 | | - _cfg = config or PersistentConnectionConfig() |
965 | | - config = PersistentConnectionConfig( |
966 | | - idle_timeout=_cfg.idle_timeout if _cfg.idle_timeout is not None else _settings.persistent_idle_timeout, |
967 | | - health_check_interval=( |
968 | | - _cfg.health_check_interval |
969 | | - if _cfg.health_check_interval is not None |
970 | | - else _settings.persistent_health_check_interval |
971 | | - ), |
972 | | - auto_reconnect=( |
973 | | - _cfg.auto_reconnect if _cfg.auto_reconnect is not None else _settings.persistent_auto_reconnect |
974 | | - ), |
975 | | - ) |
| 958 | + config = merge_config_with_settings(PersistentConnectionConfig, config, _settings, _PERSISTENT_CONFIG_FIELD_MAP) |
976 | 959 |
|
977 | 960 | # Build SSL connect_args from settings |
978 | 961 | ssl_mode = _settings.ssl_mode |
@@ -1121,19 +1104,7 @@ def __new__( |
1121 | 1104 | database = database or _settings.database |
1122 | 1105 | connection_key = f"mssql://{user}@{host}:{port}/{database}" # NOSONAR |
1123 | 1106 |
|
1124 | | - # Build config from settings, allowing partial overrides |
1125 | | - _cfg = config or PersistentConnectionConfig() |
1126 | | - config = PersistentConnectionConfig( |
1127 | | - idle_timeout=_cfg.idle_timeout if _cfg.idle_timeout is not None else _settings.persistent_idle_timeout, |
1128 | | - health_check_interval=( |
1129 | | - _cfg.health_check_interval |
1130 | | - if _cfg.health_check_interval is not None |
1131 | | - else _settings.persistent_health_check_interval |
1132 | | - ), |
1133 | | - auto_reconnect=( |
1134 | | - _cfg.auto_reconnect if _cfg.auto_reconnect is not None else _settings.persistent_auto_reconnect |
1135 | | - ), |
1136 | | - ) |
| 1107 | + config = merge_config_with_settings(PersistentConnectionConfig, config, _settings, _PERSISTENT_CONFIG_FIELD_MAP) |
1137 | 1108 |
|
1138 | 1109 | # Build SSL query params from settings |
1139 | 1110 | _query: dict[str, str] = {"driver": "ODBC Driver 18 for SQL Server"} |
@@ -1227,19 +1198,7 @@ def __new__( |
1227 | 1198 | servicename = servicename or _settings.servicename |
1228 | 1199 | connection_key = f"oracle://{user}@{host}:{port}/{servicename}" # NOSONAR |
1229 | 1200 |
|
1230 | | - # Build config from settings, allowing partial overrides |
1231 | | - _cfg = config or PersistentConnectionConfig() |
1232 | | - config = PersistentConnectionConfig( |
1233 | | - idle_timeout=_cfg.idle_timeout if _cfg.idle_timeout is not None else _settings.persistent_idle_timeout, |
1234 | | - health_check_interval=( |
1235 | | - _cfg.health_check_interval |
1236 | | - if _cfg.health_check_interval is not None |
1237 | | - else _settings.persistent_health_check_interval |
1238 | | - ), |
1239 | | - auto_reconnect=( |
1240 | | - _cfg.auto_reconnect if _cfg.auto_reconnect is not None else _settings.persistent_auto_reconnect |
1241 | | - ), |
1242 | | - ) |
| 1201 | + config = merge_config_with_settings(PersistentConnectionConfig, config, _settings, _PERSISTENT_CONFIG_FIELD_MAP) |
1243 | 1202 |
|
1244 | 1203 | with _registry_lock: |
1245 | 1204 | if connection_key in _persistent_connections: |
@@ -1301,19 +1260,7 @@ def __new__( |
1301 | 1260 | database = database or _settings.database |
1302 | 1261 | connection_key = f"mongodb://{user}@{host}:{port}/{database}" # NOSONAR |
1303 | 1262 |
|
1304 | | - # Build config from settings, allowing partial overrides |
1305 | | - _cfg = config or PersistentConnectionConfig() |
1306 | | - config = PersistentConnectionConfig( |
1307 | | - idle_timeout=_cfg.idle_timeout if _cfg.idle_timeout is not None else _settings.persistent_idle_timeout, |
1308 | | - health_check_interval=( |
1309 | | - _cfg.health_check_interval |
1310 | | - if _cfg.health_check_interval is not None |
1311 | | - else _settings.persistent_health_check_interval |
1312 | | - ), |
1313 | | - auto_reconnect=( |
1314 | | - _cfg.auto_reconnect if _cfg.auto_reconnect is not None else _settings.persistent_auto_reconnect |
1315 | | - ), |
1316 | | - ) |
| 1263 | + config = merge_config_with_settings(PersistentConnectionConfig, config, _settings, _PERSISTENT_CONFIG_FIELD_MAP) |
1317 | 1264 |
|
1318 | 1265 | with _registry_lock: |
1319 | 1266 | if connection_key in _persistent_connections: |
|
0 commit comments