재요청·대체값 3단계 처리
2026.03 ~ 2026.06 · RoadSafe
- 해결
실패한 지역을 빈칸으로 두지 않도록 세 단계로 처리합니다.
- 20초 안에 응답이 없으면 끊고 최대 3번 요청합니다. 간격은 1.5초, 3초로 늘립니다.
- 그래도 못 받으면 직전 수집에서 받은 그 지역의 정상값을 씁니다.
- 직전 값도 없으면 가장 가까운 지역의 값을 가져옵니다.
- 대체값 표시
대체한 값에는 어떤 오류 때문에 무엇으로 채웠는지(
api_status,fallback_note)를 같이 저장해, 실제로 받은 날씨와 구분합니다.- 호출 간격
지역 사이에는 2초씩 쉬고 호출합니다.
fetch_forecast.py · 최대 3번 요청L125–157GitHub ↗
def request_items(url: str, params: Dict[str, str], timeout: int = 20, max_retries: int = REQUEST_RETRY_COUNT) -> List[dict]:
last_error: Exception | None = None
for attempt in range(max_retries):
try:
response = requests.get(url, params=params, timeout=timeout)
response.raise_for_status()
data = response.json()
header = data.get("response", {}).get("header", {})
result_code = header.get("resultCode")
result_msg = header.get("resultMsg")
if result_code != "00":
raise RuntimeError(f"API 오류: {result_code} / {result_msg}")
items = (
data.get("response", {})
.get("body", {})
.get("items", {})
.get("item", [])
)
return items
except Exception as exc:
last_error = exc
if attempt >= max_retries - 1:
break
error_status = normalize_api_error(exc)
if error_status in {"LIMITED_429", "TIMEOUT", "ERROR"}:
time.sleep(RETRY_BASE_DELAY_SECONDS * (attempt + 1))
continue
break
raise last_error or RuntimeError("API 요청 실패")