-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2: fix client async storage persistence
Create and store an AsyncResource for each stream, following a similar approach as used in HttpAgent. Fixes: #55376 PR-URL: #55460 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
- Loading branch information
Showing
2 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const assert = require('assert'); | ||
const http2 = require('http2'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
const storage = new async_hooks.AsyncLocalStorage(); | ||
|
||
const { | ||
HTTP2_HEADER_CONTENT_TYPE, | ||
HTTP2_HEADER_PATH, | ||
HTTP2_HEADER_STATUS, | ||
} = http2.constants; | ||
|
||
const server = http2.createServer(); | ||
server.on('stream', (stream) => { | ||
stream.respond({ | ||
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8', | ||
[HTTP2_HEADER_STATUS]: 200 | ||
}); | ||
stream.on('error', common.mustNotCall()); | ||
stream.end('data'); | ||
}); | ||
|
||
server.listen(0, async () => { | ||
const client = storage.run({ id: 0 }, () => http2.connect(`http://localhost:${server.address().port}`)); | ||
|
||
async function doReq(id) { | ||
const req = client.request({ [HTTP2_HEADER_PATH]: '/' }); | ||
|
||
req.on('response', common.mustCall((headers) => { | ||
assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200); | ||
assert.strictEqual(id, storage.getStore().id); | ||
})); | ||
req.on('data', common.mustCall((data) => { | ||
assert.strictEqual(data.toString(), 'data'); | ||
assert.strictEqual(id, storage.getStore().id); | ||
})); | ||
req.on('end', common.mustCall(() => { | ||
assert.strictEqual(id, storage.getStore().id); | ||
server.close(); | ||
client.close(); | ||
})); | ||
} | ||
|
||
function doReqWith(id) { | ||
storage.run({ id }, () => doReq(id)); | ||
} | ||
|
||
doReqWith(1); | ||
doReqWith(2); | ||
}); |