Skip to content

Commit

Permalink
prod
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesward committed Aug 30, 2023
1 parent e1cc229 commit d0f5ff1
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions javascript-stdlib/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ async function raceWithCancellation(racers) {

const racerPromises = racersAndSignals.map(racer => racer.promise)
return Promise.any(racerPromises).then((winner) => {
console.debug("canceling losers")
racersAndSignals.forEach((racer) => {
if (racer.controller.isFinished === undefined) {
racer.controller.abort()
}
})
console.debug("losers canceled")
// wait for all the promises to settle before completing the winner
// note that this doesn't actually wait for all the loser connections to close
return Promise.allSettled(racerPromises).then(() => winner)
})
}
Expand Down Expand Up @@ -61,32 +60,26 @@ export async function scenario3(port) {

/*
On slow machines this one can't use fetch for the timeout'd request because:
scenario3 finishes and the connections are still being closed (there doesn't seem to be a way to wait until they are all closed to continue)
Then when scenario4 runs, fetch doesn't immediately make a connection.
scenario3 finishes and the connections are still being closed (there doesn't seem to be a way to wait until they are all closed to continue).
Then when scenario4 runs, fetch doesn't immediately make a connection (because the libuv thread pool is exhausted).
This causes the timeout to stop making the second connection before it actually makes the connection.
So instead of using a fetch for the timeout'd request, use a Socket and do not start the timeout until the request connects.
*/
export async function scenario4(port) {
console.debug("starting 4")
const req = async () => {
console.debug("making req")
const resp = await fetch(url(port, 4))
console.debug("got resp")
return resp.text()
}

const reqWithTimeout = () => {
console.debug("making timeout req")
const client = net.createConnection({ port }, () => {
console.debug("connected")
client.write('GET /4 HTTP/1.1\n\n');
})

// don't worry about this request returning a result, cause it will always be the loser
return new Promise((_, reject) => {
client.on("connect", () => {
setTimeout(() => {
console.debug("hit timeout")
client.end()
reject(new Error("Request timed out"))
}, 1000)
Expand Down

0 comments on commit d0f5ff1

Please sign in to comment.