Tengo un proceso de E/S pesado que inicio al usar un punto final. Ese punto final debe responder inmediatamente al cliente que se ha iniciado el proceso.
La pesada tarea de E/S podría demorar hasta 30 minutos, por lo que me pregunto en qué estado deja la conexión entre el servidor y el cliente después de res.json(...) ? ¿La conexión del enchufe sigue abierta/medio abierta? ¿Debo, en este caso, destruir el socket manualmente después de res.json(...) ? ¿Está bien que la tarea heavyIO falle después de 29 minutos y vaya al controlador de errores express? Y finalmente; ¿Este patrón es incorrecto, por ejemplo, debo procesar las tareas de E/S de forma totalmente diferente?
A continuación se muestra un ejemplo mínimo:
import express from 'express' const app = express() const asyncHandler = (handler) => (req, res, next) => Promise.resolve(handler(req, res, next)).catch(next) const heavyIO = () => new Promise((resolve, reject) => setTimeout(() => Math.random() > 0.5 ? resolve() : reject('FAILED'), 1000)) app.get('/start', asyncHandler(async (_req, res) => { // I want to be able to initiate the process and send back // confirmation that the process has been started. I want the // client to be able forget about this request. res.json('ok...started process') // Then we start to run the heavy IO process. Without the "await" this could // lead to unhandled promise rejection. // If this fails, the rejected promise will flow to express default // error handling which is I think "finalhandler" by default. // Because the headers as sent (res.headersSent === true) the error // handler will log the error and destroy the socket connection. // If this succeeds, the server will probably close the socket connection as well // internally. // Up until this point the socket has been open (or half-open?) so is this a problem? // Does it mean that the client has been "up-keeping" this socket connection // somehow or has/can it have been timed out before this succeeds or fails. await heavyIO() console.log(`FINISHED I/O at ${new Date().toISOString()}! Exiting handler...`) })) app.listen(8080)