@@ -59,13 +59,13 @@ def is_servers_update(self, fd, version):
59
59
return new_version , None
60
60
61
61
def get_servers (self , fd , num ):
62
- if fd not in self ._fd_to_service_name :
62
+ service_name = self ._fd_to_service_name .get (fd )
63
+ if service_name is None :
63
64
# not register
64
65
return []
65
- service_name = self ._fd_to_service_name [fd ]
66
66
67
67
if service_name not in self ._service_name_to_servers or \
68
- self ._service_name_to_update [ service_name ] is True :
68
+ self ._service_name_to_update . get ( service_name , False ) :
69
69
self ._refresh_service (service_name )
70
70
71
71
return list (self ._fd_to_servers [fd ])
@@ -85,12 +85,11 @@ def add_service_name(self, fd, service_name, num):
85
85
self ._service_name_to_update [service_name ] = True
86
86
87
87
def rm_service_name (self , fd ):
88
- # client maybe exit before register
89
- if fd not in self ._fd_to_service_name :
88
+ service_name = self ._fd_to_service_name .get (fd )
89
+ if service_name is None :
90
+ # client maybe exit before register
90
91
return
91
92
92
- service_name = self ._fd_to_service_name [fd ]
93
-
94
93
with self ._mutex :
95
94
if service_name in self ._service_name_to_fds :
96
95
try :
@@ -141,7 +140,7 @@ def _refresh_service(self, service_name):
141
140
142
141
# no change
143
142
if len (rm_servers ) == 0 and len (add_servers ) == 0 and \
144
- self ._service_name_to_update [ service_name ] is False :
143
+ not self ._service_name_to_update . get ( service_name , False ) :
145
144
return
146
145
self ._service_name_to_update [service_name ] = False
147
146
update_fd = set ()
@@ -182,7 +181,7 @@ def _refresh_service(self, service_name):
182
181
# assign: {fd0:32, fd1:32, fd2:32}
183
182
server_max_connect = int ((fd_num + server_num - 1 ) / server_num )
184
183
fd_max_connect = max (1 , int (server_num / fd_num ))
185
- #fd_max_connect = int((server_num + fd_num - 1) / fd_num)
184
+ # fd_max_connect = int((server_num + fd_num - 1) / fd_num)
186
185
print ('fd_num={}, server_num={}, smax={}, mcon={}' .format (
187
186
fd_num , server_num , server_max_connect , fd_max_connect ))
188
187
@@ -253,9 +252,17 @@ def _refresh(self):
253
252
except KeyError :
254
253
pass
255
254
256
- time .sleep (2 )
255
+ time .sleep (3 )
256
+
257
+ def refresh (self ):
258
+ while True :
259
+ try :
260
+ self ._refresh ()
261
+ except Exception as e :
262
+ sys .stderr .write (str (e ) + '\n ' )
263
+ time .sleep (6 )
257
264
258
265
def start (self ):
259
- self ._thread = threading .Thread (target = self ._refresh )
266
+ self ._thread = threading .Thread (target = self .refresh )
260
267
self ._thread .daemon = True
261
268
self ._thread .start ()
0 commit comments