Time out

Do_It·2023년 10월 6일

Time out
: 프로그램이 일정 시간이 지나도 실행 되지 않는 경우 자동적으로 종료시키는 것

스프링에서는 Time out을 설정해주는 것이 중요하다. 왜냐하면 http 요청시 무한 로딩에 걸릴 수 있기 때문이다.

웹에서는 두 가지의 경우가 있다.

  1. Connection Time out(연결 시간 초과)
    : 서버와 연결 시 설정한 연결 시간 이내에 연결되지 않으면 요청을 취소 시키는 것

2.Read Time out(읽기 시간 초과)
: 서버와 연결된 후 클라언트가 서버로 부터 받은 응답을 처리하는데 설정한 시간 이내에서 처리되지 않는 경우 요청을 취소하는 것

이번 프로젝트에서는 RestTemplate을 사용했었다.

package com.github.commerce.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

@Configuration
public class RestTemplateConfig  {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder
                // RestTemplate 으로 외부 API 호출 시 일정 시간이 지나도 응답이 없을 때
                // 무한 대기 상태 방지를 위해 강제 종료 설정
                .setConnectTimeout(Duration.ofSeconds(5)) // 5초
                .setReadTimeout(Duration.ofSeconds(5)) // 5초
                .build();
    }
}
profile
오늘의 노력이 내일의 성장으로 이어지고 있음을

0개의 댓글