@@ -235,7 +235,7 @@ def __init__(self):
235235 p .start ()
236236
237237 if self .connection_pool_min_num :
238- p = threading .Thread (target = self .create_connection_daemon )
238+ p = threading .Thread (target = self .keep_connection_daemon )
239239 p .daemon = True
240240 p .start ()
241241
@@ -306,62 +306,40 @@ def save_ssl_connection_for_reuse(self, ssl_sock, host=None, call_time=0):
306306 google_ip .report_connect_closed (ssl_sock .ip , "slowest %d" % ssl_sock .handshake_time )
307307 ssl_sock .close ()
308308
309- def create_connection_daemon (self ):
310- connect_start_num = 0
311-
309+ def keep_connection_daemon (self ):
312310 while connect_control .keep_running :
313- time .sleep (0.1 )
314311 if not connect_control .allow_connect ():
315312 time .sleep (5 )
316313 continue
317314
318- if self .thread_num > self .max_thread_num :
319- continue
320-
321- target_conn_num = self .connection_pool_min_num
322- if self .new_conn_pool .qsize () > target_conn_num :
323- time .sleep (1 )
315+ if self .new_conn_pool .qsize () > self .connection_pool_min_num :
316+ time .sleep (5 )
324317 continue
325318
326- self .thread_num_lock .acquire ()
327- self .thread_num += 1
328- self .thread_num_lock .release ()
329- p = threading .Thread (target = self .connect_process )
330- p .start ()
331- connect_start_num += 1
332-
333- if connect_start_num > 10 :
334- time .sleep (5 )
335- connect_start_num = 0
319+ self .connect_process ()
336320
337321 def connect_process (self ):
338322 try :
339323 ip_str = google_ip .get_gws_ip ()
340324 if not ip_str :
341325 time .sleep (60 )
342- xlog .warning ("no enough ip" )
326+ # xlog.warning("no enough ip")
343327 return
344328
345- port = 443
346329 #xlog.debug("create ssl conn %s", ip_str)
347- ssl_sock = self ._create_ssl_connection ( (ip_str , port ) )
348- if ssl_sock :
349- ssl_sock . last_use_time = time . time ()
330+ ssl_sock = self ._create_ssl_connection ( (ip_str , 443 ) )
331+ if not ssl_sock :
332+ return
350333
351- if self .new_conn_pool .qsize () >= self .connection_pool_max_num and self .ssl_timeout_cb :
352- self .ssl_timeout_cb (ssl_sock )
353- else :
354- self .new_conn_pool .put ((ssl_sock .handshake_time , ssl_sock ))
355- finally :
356- self .thread_num_lock .acquire ()
357- self .thread_num -= 1
358- self .thread_num_lock .release ()
334+ self .new_conn_pool .put ((ssl_sock .handshake_time , ssl_sock ))
335+ except :
336+ pass
359337
360338 def create_more_connection_worker (self ):
361339 while connect_control .allow_connect () and \
362340 self .thread_num < self .max_thread_num and \
363- (self .new_conn_pool .qsize () < self .https_new_connect_num or \
364- self .new_conn_pool .qsize (only_h1 = True ) < 1 ):
341+ (self .new_conn_pool .qsize () < self .https_new_connect_num or
342+ self .new_conn_pool .qsize (only_h1 = True ) < 1 ):
365343
366344 self .thread_num_lock .acquire ()
367345 self .thread_num += 1
@@ -381,25 +359,33 @@ def connect_thread(self, sleep_time=0):
381359 time .sleep (sleep_time )
382360 try :
383361 while self .new_conn_pool .qsize () < self .https_new_connect_num or \
384- self .new_conn_pool .qsize (only_h1 = True ) < 1 :
362+ self .new_conn_pool .qsize (only_h1 = True ) < 1 :
363+
364+ if self .new_conn_pool .qsize () > self .connection_pool_max_num :
365+ break
366+
367+ if not connect_control .allow_connect ():
368+ break
385369
386370 ip_str = google_ip .get_gws_ip ()
387371 if not ip_str :
388- time .sleep (60 )
389372 xlog .warning ("no enough ip" )
373+ time .sleep (60 )
390374 break
391375
392- port = 443
393376 #xlog.debug("create ssl conn %s", ip_str)
394- ssl_sock = self ._create_ssl_connection ( (ip_str , port ) )
395- if ssl_sock :
396- ssl_sock .last_use_time = time .time ()
397- if self .new_conn_pool .qsize () >= self .connection_pool_max_num and self .ssl_timeout_cb :
398- self .ssl_timeout_cb (ssl_sock )
399- else :
400- self .new_conn_pool .put ((ssl_sock .handshake_time , ssl_sock ))
401- elif not connect_control .allow_connect ():
402- break
377+ ssl_sock = self ._create_ssl_connection ( (ip_str , 443 ) )
378+ if not ssl_sock :
379+ continue
380+
381+ if self .ssl_timeout_cb and \
382+ self .new_conn_pool .qsize () > self .connection_pool_max_num + 1 and \
383+ self .new_conn_pool .qsize () > self .https_new_connect_num + 1 and \
384+ self .new_conn_pool .qsize (only_h1 = True ) > 2 :
385+
386+ self .ssl_timeout_cb (ssl_sock )
387+ else :
388+ self .new_conn_pool .put ((ssl_sock .handshake_time , ssl_sock ))
403389 time .sleep (1 )
404390 finally :
405391 self .thread_num_lock .acquire ()
@@ -482,6 +468,7 @@ def verify_SSL_certificate_issuer(ssl_sock):
482468 xlog .debug ("create_ssl update ip:%s time:%d h2:%d" , ip , handshake_time , ssl_sock .h2 )
483469 ssl_sock .fd = sock .fileno ()
484470 ssl_sock .create_time = time_begin
471+ ssl_sock .last_use_time = time .time ()
485472 ssl_sock .received_size = 0
486473 ssl_sock .load = 0
487474 ssl_sock .handshake_time = handshake_time
0 commit comments