grantila / fetch-h2

HTTP/1+2 Fetch API client for Node.js
MIT License
336 stars 16 forks source link

Connections cleanup after timeout #60

Open gnostis opened 5 years ago

gnostis commented 5 years ago

Just tested the behavior of timeout and memory after running below function 100 times cycled. Url has 3 minutes delay in response and timeout happens immideately. I thought that appropriate connection will be closed after promise is rejected with TimeoutError and everything will be cleaned. But memory grows. I tried disconnectAll after catching timeout and there is slightly better memory usage after cycle in case of that. All connections stay alive until 3 minutes passed. If we do nearly the same but using AbortController signal cancelling fetches, memory stays constant and there is no any leaks and pending connections.

So the main question is how to clean up connection immideately after timeout? Under heavy load and under certain conditions, a memory leak will occur. Experimented to abort after timeouts but no result.

async function fetchTest() {
  try {
      let newCtx = context(); 
      var response = await newCtx.fetch('https://httpstat.us/200?sleep=180000',{timeout: 10});
  }
  catch (e) {
    console.log(e.constructor.name);
    newCtx.disconnectAll();  //consumes less memory with disconnecting after timeot
    return {error: e.constructor.name}; 
  } 
  return { body: await response.text() };
}