11import re
22import requests
33import traceback
4- import random
54from sps_module import sps_shared_resources
65from datetime import datetime , timedelta , timezone
76from utils .pub_service import S3 , AZURE_CONST
@@ -47,6 +46,7 @@ def check_and_add_available_locations(az):
4746 return True
4847 else :
4948 print ("No new available locations found. locations_call_history_tmp or locations_call_history_tmp unchanged." )
49+ return True
5050
5151 except Exception as e :
5252 print (f"Error in check_and_add_available_locations: { e } " )
@@ -69,46 +69,63 @@ def validation_can_call(subscription_id, location):
6969
7070
7171def get_next_available_location ():
72- """
73- 이 메서드는 사용 가능한 다음 위치를 리턴합니다.
74- 호출 이력과 초과 요청 데이터를 기반으로 적절한 위치를 선택합니다.
75- 호출 시 이용하는 location은 구독 내에 지난 호출의 location을 이용 안 해야 하는 로직이 들어갑니다.
76-
77- 이유:
78- 단 기간에 같은 location으로 호출 시, timeout율이 놉습니다.
79- """
8072 try :
8173 if SS_Resources .locations_call_history_tmp is None or SS_Resources .locations_over_limit_tmp is None :
8274 return None
8375
84- # Clean expired data
8576 clean_expired_over_limit_locations ()
8677 clean_expired_over_call_history_locations ()
8778
88- start_subscription_index = random .randint (0 , len (SS_Resources .subscriptions ) - 1 )
79+ subs = SS_Resources .subscriptions
80+ locs = SS_Resources .available_locations
81+ if not subs or not locs :
82+ return None
8983
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 ]
84+ n , m = len (subs ), len (locs )
9385
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 ]
86+ last_pair = getattr (SS_Resources , "last_subscription_id_and_location_tmp" , None ) or {}
87+ last_sub_id = last_pair .get ("last_subscription_id" )
88+ last_loc = last_pair .get ("last_location" )
9889
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
90+ if last_sub_id in subs and last_loc in locs :
91+ s_idx = subs .index (last_sub_id )
92+ l_idx = locs .index (last_loc )
93+ l_idx = (l_idx + 1 ) % m
94+ if l_idx == 0 :
95+ s_idx = (s_idx + 1 ) % n
96+ else :
97+ s_idx , l_idx = 0 , 0
10498
105- return None
99+ attempts = 0
100+ while attempts < n * m :
101+ sub_id = subs [s_idx ]
102+ loc = locs [l_idx ]
103+
104+ if validation_can_call (sub_id , loc ):
105+ SS_Resources .succeed_to_get_next_available_location_count += 1
106+ SS_Resources .succeed_to_get_next_available_location_count_all += 1
107+
108+ SS_Resources .locations_call_history_tmp [sub_id ][loc ].append (
109+ datetime .now (timezone .utc ).replace (tzinfo = None ).isoformat ())
110+
111+ SS_Resources .last_subscription_id_and_location = {
112+ "last_subscription_id" : sub_id ,
113+ "last_location" : loc ,
114+ }
115+ return sub_id , loc
116+
117+ l_idx = (l_idx + 1 ) % m
118+ if l_idx == 0 :
119+ s_idx = (s_idx + 1 ) % n
120+
121+ attempts += 1
106122
107123 except Exception as e :
108124 print ("\n [ERROR] Exception occurred in get_next_available_location:" )
109125 print (traceback .format_exc ())
110126 print (f"\n [ERROR] Failed to get_next_available_location: { e } " )
111127 return None
128+ return None
112129
113130
114131def collect_available_locations ():
@@ -144,6 +161,7 @@ def collect_available_locations():
144161 except Exception as e :
145162 print (f"Failed to collect_available_locations, Error: { e } " )
146163 return None
164+ return None
147165
148166def clean_expired_over_limit_locations ():
149167 '''
0 commit comments