@@ -47,6 +47,7 @@ def check_and_add_available_locations(az):
4747 return True
4848 else :
4949 print ("No new available locations found. locations_call_history_tmp or locations_call_history_tmp unchanged." )
50+ return False
5051
5152 except Exception as e :
5253 print (f"Error in check_and_add_available_locations: { e } " )
@@ -70,7 +71,7 @@ def validation_can_call(subscription_id, location):
7071
7172def get_next_available_location ():
7273 """
73- 이 메서드는 사용 가능한 다음 위치를 리턴합니다.
74+ 이 메서드는 사용 가능한 다음 location를 리턴합니다.
7475 호출 이력과 초과 요청 데이터를 기반으로 적절한 위치를 선택합니다.
7576 호출 시 이용하는 location은 구독 내에 지난 호출의 location을 이용 안 해야 하는 로직이 들어갑니다.
7677
@@ -81,34 +82,59 @@ def get_next_available_location():
8182 if SS_Resources .locations_call_history_tmp is None or SS_Resources .locations_over_limit_tmp is None :
8283 return None
8384
84- # Clean expired data
8585 clean_expired_over_limit_locations ()
8686 clean_expired_over_call_history_locations ()
8787
88- start_subscription_index = random .randint (0 , len (SS_Resources .subscriptions ) - 1 )
88+ subs = SS_Resources .subscriptions
89+ locs = SS_Resources .available_locations
90+ if not subs or not locs :
91+ return None
92+
93+ n , m = len (subs ), len (locs )
8994
90- for i in range ( len ( SS_Resources . subscriptions )):
91- subscription_index = ( start_subscription_index + i ) % len ( SS_Resources . subscriptions )
92- subscription_id = SS_Resources . subscriptions [ subscription_index ]
95+ last_pair = getattr ( SS_Resources , "last_subscription_id_and_location" , None ) or {}
96+ last_sub_id = last_pair . get ( "last_subscription_id" )
97+ last_loc = last_pair . get ( "last_location" )
9398
94- start_location_index = random .randint (0 , len (SS_Resources .available_locations ) - 1 )
95- for j in range (len (SS_Resources .available_locations )):
96- location_index = (start_location_index + j ) % len (SS_Resources .available_locations )
97- location = SS_Resources .available_locations [location_index ]
99+ if last_sub_id in subs and last_loc in locs :
100+ s_idx = subs .index (last_sub_id )
101+ l_idx = locs .index (last_loc )
102+ l_idx = (l_idx + 1 ) % m
103+ if l_idx == 0 :
104+ s_idx = (s_idx + 1 ) % n
105+ else :
106+ s_idx , l_idx = 0 , 0
98107
99- if validation_can_call (subscription_id , location ):
100- SS_Resources .succeed_to_get_next_available_location_count += 1
101- SS_Resources .succeed_to_get_next_available_location_count_all += 1
102- SS_Resources .locations_call_history_tmp [subscription_id ][location ].append (datetime .now (timezone .utc ).replace (tzinfo = None ).isoformat ())
103- return subscription_id , location
108+ attempts = 0
109+ while attempts < n * m :
110+ sub_id = subs [s_idx ]
111+ loc = locs [l_idx ]
104112
105- return None
113+ if validation_can_call (sub_id , loc ):
114+ SS_Resources .succeed_to_get_next_available_location_count += 1
115+ SS_Resources .succeed_to_get_next_available_location_count_all += 1
116+
117+ SS_Resources .locations_call_history_tmp [sub_id ][loc ].append (
118+ datetime .now (timezone .utc ).replace (tzinfo = None ).isoformat ())
119+
120+ SS_Resources .last_subscription_id_and_location = {
121+ "last_subscription_id" : sub_id ,
122+ "last_location" : loc ,
123+ }
124+ return sub_id , loc
125+
126+ l_idx = (l_idx + 1 ) % m
127+ if l_idx == 0 :
128+ s_idx = (s_idx + 1 ) % n
129+
130+ attempts += 1
106131
107132 except Exception as e :
108133 print ("\n [ERROR] Exception occurred in get_next_available_location:" )
109134 print (traceback .format_exc ())
110135 print (f"\n [ERROR] Failed to get_next_available_location: { e } " )
111136 return None
137+ return None
112138
113139
114140def collect_available_locations ():
0 commit comments