Code Monkey home page Code Monkey logo

Comments (5)

sandratatarevicova avatar sandratatarevicova commented on July 18, 2024 1

See the example below.

const fs = require('node:fs');
const { pipeline } = require('node:stream/promises');
const http = require('node:http');
const { request } = require('undici');

const server = http.createServer((req, res) => {
    res.writeHead(200);
    setTimeout(() => {
        res.end();
        console.log('Request closed after 10 seconds');
    }, 10000);
    const writeZeroes = () => {
        res.write(Buffer.alloc(1024, 0));
        setImmediate(writeZeroes);
    };
    writeZeroes();
});

server.listen();
const serverUrl = `http://localhost:${server.address().port}`;
console.log(`Server listening on ${serverUrl}`);

async function test() {
    const { body } = await request(serverUrl, {
        throwOnError: true,
        bodyTimeout: 1000,
    });
    console.log('Reading body...');
    await pipeline(body, fs.createWriteStream('/tmp/body-timeout-test'));
    console.log('Reading body finished');
}

test();

Output:

Server listening on http://localhost:63483
Reading body...
Request closed after 10 seconds
Reading body finished

Reading the response body should timeout after 1 second, but it does not.

from undici.

sandratatarevicova avatar sandratatarevicova commented on July 18, 2024 1

Thank you, we will try the AbortSignal, but I think the documentation is a bit misleading:

  • bodyTimeout number | null (optional) - The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use 0 to disable it entirely. Defaults to 300 seconds.

Based on this, I would expect the request to be aborted after the timeout.

from undici.

mcollina avatar mcollina commented on July 18, 2024

Can you please include a server as well in your example? Otherwise reproducing it is network dependent.

from undici.

metcoder95 avatar metcoder95 commented on July 18, 2024

If I'm not mistaken, the bodyTimeout is only applied on idle situations between receiving body's data.
As you keep writing to the socket in an interval, and for instance the client keeps receiving data from the server, the timeout is reset on every new chunk, delaying its effect.

e.g. if I alter your example like this:

const fs = require('node:fs')
const { pipeline } = require('node:stream/promises')
const http = require('node:http')
const { request } = require('undici')

const server = http.createServer((req, res) => {
  res.writeHead(200)
  setTimeout(() => {
    res.end()
    console.log('Request closed after 10 seconds')
  }, 10000).unref()
  const writeZeroes = () => {
    res.write(Buffer.alloc(1024, 0))
    setInterval(writeZeroes, 2000)
  }
  writeZeroes()
})

server.listen(0, () => {
  const serverUrl = `http://localhost:${server.address().port}`
  console.log(`Server listening on ${serverUrl}`)

  async function test () {
    console.log('Sending request...')
    const { body } = await request(serverUrl, {
      throwOnError: true,
      bodyTimeout: 1000
    })
    console.log('Reading body...')
    await pipeline(body, fs.createWriteStream('/tmp/body-timeout-test'))
    console.log('Reading body finished')
  }

  test()
})

Then the timeout will effectively apply.

You can use AbortSignal.timeout to enforce a specific timeout independent of the bodyTimeout one

from undici.

metcoder95 avatar metcoder95 commented on July 18, 2024

See what you mean, a PR to support us improving the docs is always welcomed!

from undici.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.