개발업무/개발

Express.js timeout 체크리스트

NickTop 2023. 5. 29. 23:10

서버 timeout

 

request timeout : client의 request를 받는데 걸리는 5분 (default)

server timeout : 서버가 request 처리하는데 쓰는 시간, 무한대기 (옛날 버전은 2분)

 

https://nodejs.org/api/http.html#http_server_timeout

 

HTTP | Node.js v20.1.0 Documentation

HTTP# Source Code: lib/http.js To use the HTTP server and client one must require('node:http'). The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possi

nodejs.org

 

조절하려면

connect-timeout

https://expressjs.com/en/resources/middleware/timeout.html

 

Express timeout middleware

connect-timeout Times out a request in the Connect/Express application framework. Install This is a Node.js module available through the npm registry. Installation is done using the npm install command: $ npm install connect-timeout API NOTE This module is

expressjs.com

또는

app.use('/sample', function(req, res) {
    req.connection.setTimeout( 10 * 60 * 1000)
    res.connection.setTimeout( 10 * 60 * 1000)
    // ..
    res.send(html)
});

 

DB timeout (MySQL)

connectionTimeout : DB connection 최초로 맺을 때 걸리는 시간 (default : 10000ms), pool 생성 시 설정

query timeout : 쿼리 실행완료시까지 걸리는 시간 (default : 무한대기), query 실행 시에도 설정 가능

  너무 옛날 버전은 query 실행시 timeout 자체가 없음, pool 생성시 socketTimeout으로 대체해야함

 

var pool  = mysql.createPool({
   // ...
   connectTimeout : 1000,
   socketTimeout : 6*60*1000,// queryTimeout 쓸수없을때
   queryTimeout : 6*60*1000, // 구 버전은 timeout임
});
pool.query({ query: 'SELECT 1 FROM DUAL;', timeout: 1000 }, callback);

socketTimeout은 pool.query() 를 호출할 때는 query 호출 시간까지 socket 연결시간으로 계산하지만,

connection = pool.getConnection() 을 한 이후 connection.query()에서는 connection을 맺는데까지 socket연결시간으로 계산됨.

pool.query()로는 연결된 connection을 직접 kill할 수 없으므로 (즉, 서버와 DB간 socket만 끊기고 db상에서 프로세스는 돌고 있음),

socketTimeout 파라미터를 넣는 대신,  setTimeout 함수로 일정시간이후 connection.destroy()를 하는 것이 바람직하다

 

Axios

timeout : default 무한대기

const timeoutAxios = axios.create({
  timeout: 10000,
});

 

'개발업무 > 개발' 카테고리의 다른 글

Nginx reverse proxy 설치 및 구성  (0) 2023.11.18
[Git] pull request view - diff  (0) 2023.11.15
[MySQL] Three-valued logic: exists / not exists / in / not in  (0) 2023.08.16
PostMessage 사용하기  (0) 2023.04.13
깃 원복하기  (0) 2023.03.15