코드/dev

NoHttpResponseException 해결 방법

미로처럼 2024. 2. 26. 10:04
728x90

외부 연동 리소스 작업중 간헐적으로 443 NoHttpResponseException 에러가 떨어졌다.

타겟 서버에서 유효하지 않은 HTTP 응답으로 제대로 응답하기를 실패했다는 신호라고 나오는데

더 알아보면 HTTP 1.1 / KEEP -ALIVE가 보장될떄 나올 수 있는 오류이다.

즉 , 커넥션이 유지될떄 호출이 되면 해당 타겟 서버에서는 이미 응답을 줬다는 의미로 해당 exception 이 떨어진다.

최초에는 커넥션 을 빠르게 끊고 다시 맺는 옵션을 추가하였습니다.

 

 

final int INVALID_CONNECTION_CLOSE_TIME = 5 * 1000;
        manager.setValidateAfterInactivity(INVALID_CONNECTION_CLOSE_TIME);
//위 옵션을 restTemplate bean 생성이 추가

 

 

위와 같은 조치를 했지만 여전히 발생을 했습니다.

여러 레퍼런스와 스택오버플로우를 보니 위와 같은 문제 발생시 자제 재 호출하는 방법 밖에 없다는 결론에 도달했습니다.

 

 

 private static HttpRequestRetryHandler retryHandler() {
        return (exception, executionCount, context) -> {
            if (executionCount > 10) {//5
                return false;
            }

            HttpClientContext clientContext = HttpClientContext.adapt(context);
            HttpRequest request = clientContext.getRequest();

            if (exception instanceof java.net.SocketException && exception.getMessage()
                    .equals("Connection reset") || exception instanceof NoHttpResponseException) {
                log.info(
                        "RetryHandler Connection reset executionCount - {}, totalCount - {}, request - {}",
                        executionCount, 10,
                        request);

                // 2번 요청 이후부터는 2초 후 재요청
                if (executionCount >= 2) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        log.warn("Interrupted {} ", e.getMessage(), e);
//                        LOGGER.log(Level.WARN, "Interrupted!", e);
                        /* Clean up whatever needs to be handled before interrupting  */
                        Thread.currentThread().interrupt();
                    }
                }
                return true;
            }

            return false;
        };
    }

 

 

위와 같이 retryHandler에 NoHttpResponseException 재호출 되도록 설정 했습니다.

설정 이후에는 해당 오류는 발생하지 않았습니다.

 

참고 레퍼런스

https://stackoverflow.com/questions/26111331/org-apache-http-nohttpresponseexception-xx-xx-xx-xx443-failed-to-respond

 

org.apache.http.NoHttpResponseException: XX.XX.XX.XX:443 failed to respond

Currently I am using Apache http components client V4.3.5. In my case, I can upload small file(1kb), but it is not working on large file(100kb) when I run the code and get the exception "org.apache...

stackoverflow.com

 

https://hc.apache.org/httpclient-legacy/exception-handling.html

728x90