|
@@ -2,6 +2,7 @@ import bson
|
|
|
import pymongo
|
|
|
import redis
|
|
|
from elasticsearch import Elasticsearch
|
|
|
+from pymongo.uri_parser import SCHEME
|
|
|
from rediscluster import RedisCluster
|
|
|
|
|
|
from config.load import mongo_conf, redis_conf, es_conf, redis_startup_nodes
|
|
@@ -10,15 +11,20 @@ from config.load import mongo_conf, redis_conf, es_conf, redis_startup_nodes
|
|
|
MONGO_URI_CLIENTS = {} # a dictionary hold all client with uri as key
|
|
|
|
|
|
|
|
|
-def mongo_client(cfg=None, host=None, port=None, fork=False, **kwargs):
|
|
|
- if host is not None and port is not None:
|
|
|
- uri = f'mongodb://{host}:{port}'
|
|
|
+def mongo_client(host=None, port=None, fork=False, **kwargs):
|
|
|
+ if host is None:
|
|
|
+ host = mongo_conf["host"]
|
|
|
+ if port is None:
|
|
|
+ port = mongo_conf["port"]
|
|
|
+
|
|
|
+ if host is not None and host.startswith(SCHEME):
|
|
|
+ uri = host
|
|
|
else:
|
|
|
- _cfg = (cfg or mongo_conf)
|
|
|
- uri = f'mongodb://{_cfg["host"]}:{_cfg["port"]}'
|
|
|
+ uri = f'mongodb://{host}:{port}'
|
|
|
|
|
|
if fork:
|
|
|
return pymongo.MongoClient(uri, **kwargs)
|
|
|
+
|
|
|
global MONGO_URI_CLIENTS
|
|
|
matched_client = MONGO_URI_CLIENTS.get(uri)
|
|
|
if matched_client is None:
|