Skip to content

Commit

Permalink
refactor: split out last h1 specific code from core (#2876)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 28, 2024
1 parent 799aedf commit 00f1914
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
34 changes: 34 additions & 0 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,40 @@ async function connectH1 (client, socket) {
resumeH1(client)
},
destroy () {
},
busy (request) {
if (socket[kWriting] || socket[kReset] || socket[kBlocking]) {
return true
}

if (client[kRunning] > 0 && !request.idempotent) {
// Non-idempotent request cannot be retried.
// Ensure that no other requests are inflight and
// could cause failure.
return true
}

if (client[kRunning] > 0 && (request.upgrade || request.method === 'CONNECT')) {
// Don't dispatch an upgrade until all preceding requests have completed.
// A misbehaving server might upgrade the connection before all pipelined
// request has completed.
return true
}

if (client[kRunning] > 0 && util.bodyLength(request.body) !== 0 &&
(util.isStream(request.body) || util.isAsyncIterable(request.body) || util.isFormDataLike(request.body))) {
// Request with stream or iterator body can error while other requests
// are inflight and indirectly error those as well.
// Ensure this doesn't happen by waiting for inflight
// to complete before dispatching.

// Request with stream or iterator body cannot be retried.
// Ensure that no other requests are inflight and
// could cause failure.
return true
}

return false
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ async function connectH2 (client, socket) {
},
destroy (err) {
session.destroy(err)
},
busy () {
return false
}
}
}
Expand Down
35 changes: 5 additions & 30 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,37 +598,12 @@ function _resume (client, sync) {
return
}

if (client[kHTTPContext].version === 'h1') {
if (socket[kWriting] || socket[kReset] || socket[kBlocking]) {
return
}

if (client[kRunning] > 0 && !request.idempotent) {
// Non-idempotent request cannot be retried.
// Ensure that no other requests are inflight and
// could cause failure.
return
}

if (client[kRunning] > 0 && (request.upgrade || request.method === 'CONNECT')) {
// Don't dispatch an upgrade until all preceding requests have completed.
// A misbehaving server might upgrade the connection before all pipelined
// request has completed.
return
}

if (client[kRunning] > 0 && util.bodyLength(request.body) !== 0 &&
(util.isStream(request.body) || util.isAsyncIterable(request.body) || util.isFormDataLike(request.body))) {
// Request with stream or iterator body can error while other requests
// are inflight and indirectly error those as well.
// Ensure this doesn't happen by waiting for inflight
// to complete before dispatching.
if (!client[kHTTPContext]) {
return
}

// Request with stream or iterator body cannot be retried.
// Ensure that no other requests are inflight and
// could cause failure.
return
}
if (client[kHTTPContext].busy(request)) {
return
}

if (!request.aborted && client[kHTTPContext].write(request)) {
Expand Down

0 comments on commit 00f1914

Please sign in to comment.