@@ -228,6 +228,7 @@ class CosS3Client(object):
228
228
"""cos客户端类,封装相应请求"""
229
229
230
230
__built_in_sessions = None # 内置的静态连接池,多个Client间共享使用
231
+ __built_in_pid = 0
231
232
232
233
def __init__ (self , conf , retry = 1 , session = None ):
233
234
"""初始化client对象
@@ -239,38 +240,19 @@ def __init__(self, conf, retry=1, session=None):
239
240
self ._conf = conf
240
241
self ._retry = retry # 重试的次数,分片上传时可适当增大
241
242
242
- if not CosS3Client .__built_in_sessions :
243
- with threading .Lock ():
244
- if not CosS3Client .__built_in_sessions : # 加锁后double check
245
- CosS3Client .__built_in_sessions = self .generate_built_in_connection_pool (self ._conf ._pool_connections , self ._conf ._pool_maxsize )
246
-
247
243
if session is None :
244
+ if not CosS3Client .__built_in_sessions :
245
+ with threading .Lock ():
246
+ if not CosS3Client .__built_in_sessions : # 加锁后double check
247
+ CosS3Client .__built_in_sessions = self .generate_built_in_connection_pool (self ._conf ._pool_connections , self ._conf ._pool_maxsize )
248
+ CosS3Client .__built_in_pid = os .getpid ()
249
+
248
250
self ._session = CosS3Client .__built_in_sessions
251
+ self ._use_built_in_pool = True
252
+ logger .info ("bound built-in connection pool when new client. maxsize=%d,%d" % (self ._conf ._pool_connections , self ._conf ._pool_maxsize ))
249
253
else :
250
254
self ._session = session
251
-
252
- def set_built_in_connection_pool_max_size (self , PoolConnections , PoolMaxSize ):
253
- """设置SDK内置的连接池的连接大小,并且重新绑定到client中"""
254
- if not CosS3Client .__built_in_sessions :
255
- return
256
-
257
- if CosS3Client .__built_in_sessions .get_adapter ('http://' )._pool_connections == PoolConnections \
258
- and CosS3Client .__built_in_sessions .get_adapter ('http://' )._pool_maxsize == PoolMaxSize :
259
- return
260
-
261
- # 判断之前是否绑定到内置连接池
262
- rebound = False
263
- if self ._session and self ._session is CosS3Client .__built_in_sessions :
264
- rebound = True
265
-
266
- # 重新生成内置连接池
267
- CosS3Client .__built_in_sessions .close ()
268
- CosS3Client .__built_in_sessions = self .generate_built_in_connection_pool (PoolConnections , PoolMaxSize )
269
-
270
- # 重新绑定到内置连接池
271
- if rebound :
272
- self ._session = CosS3Client .__built_in_sessions
273
- logger .info ("rebound built-in connection pool success. maxsize=%d,%d" % (PoolConnections , PoolMaxSize ))
255
+ self ._use_built_in_pool = False
274
256
275
257
def generate_built_in_connection_pool (self , PoolConnections , PoolMaxSize ):
276
258
"""生成SDK内置的连接池,此连接池是client间共用的"""
@@ -279,7 +261,30 @@ def generate_built_in_connection_pool(self, PoolConnections, PoolMaxSize):
279
261
built_in_sessions .mount ('https://' , requests .adapters .HTTPAdapter (pool_connections = PoolConnections , pool_maxsize = PoolMaxSize ))
280
262
logger .info ("generate built-in connection pool success. maxsize=%d,%d" % (PoolConnections , PoolMaxSize ))
281
263
return built_in_sessions
282
-
264
+
265
+ def handle_built_in_connection_pool_by_pid (self ):
266
+ if not CosS3Client .__built_in_sessions :
267
+ return
268
+
269
+ if not self ._use_built_in_pool :
270
+ return
271
+
272
+ if CosS3Client .__built_in_pid == os .getpid ():
273
+ return
274
+
275
+ with threading .Lock ():
276
+ if CosS3Client .__built_in_pid == os .getpid (): # 加锁后double check
277
+ return
278
+
279
+ # 重新生成内置连接池
280
+ CosS3Client .__built_in_sessions .close ()
281
+ CosS3Client .__built_in_sessions = self .generate_built_in_connection_pool (self ._conf ._pool_connections , self ._conf ._pool_maxsize )
282
+ CosS3Client .__built_in_pid = os .getpid ()
283
+
284
+ # 重新绑定到内置连接池
285
+ self ._session = CosS3Client .__built_in_sessions
286
+ logger .info ("bound built-in connection pool when new processor. maxsize=%d,%d" % (self ._conf ._pool_connections , self ._conf ._pool_maxsize ))
287
+
283
288
def get_conf (self ):
284
289
"""获取配置"""
285
290
return self ._conf
@@ -360,6 +365,10 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, ci_req
360
365
if self ._conf ._allow_redirects is not None :
361
366
kwargs ['allow_redirects' ] = self ._conf ._allow_redirects
362
367
exception_logbuf = list () # 记录每次重试的错误日志
368
+
369
+ # 切换了进程需要重新生成连接池
370
+ self .handle_built_in_connection_pool_by_pid ()
371
+
363
372
for j in range (self ._retry + 1 ):
364
373
try :
365
374
if j != 0 :
0 commit comments