-
-
Notifications
You must be signed in to change notification settings - Fork 715
added prioritization intercepter #4878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amyssnippet
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
amyssnippet:c/4766
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,88 @@ | ||
| 'use strict' | ||
|
|
||
| const DecoratorHandler = require('../handler/decorator-handler') | ||
|
|
||
| class PriorityQueue { | ||
| #queue = [] | ||
| #concurrency | ||
| #running = 0 | ||
|
|
||
| constructor (concurrency = 1) { | ||
| this.#concurrency = concurrency | ||
| } | ||
|
|
||
| acquire (callback, priority = 0) { | ||
| this.#queue.push({ callback, priority }) | ||
| this.#queue.sort((a, b) => b.priority - a.priority) | ||
| this.#dispatch() | ||
| } | ||
|
|
||
| release () { | ||
| this.#running-- | ||
| this.#dispatch() | ||
| } | ||
|
|
||
| #dispatch () { | ||
| while (this.#running < this.#concurrency && this.#queue.length > 0) { | ||
| const entry = this.#queue.shift() | ||
| this.#running++ | ||
| entry.callback() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class PriorityHandler extends DecoratorHandler { | ||
| #priorityQueue | ||
|
|
||
| constructor (handler, priorityQueue) { | ||
| super(handler) | ||
| this.#priorityQueue = priorityQueue | ||
| } | ||
|
|
||
| onResponseEnd (controller, trailers) { | ||
| this.#release() | ||
| return super.onResponseEnd(controller, trailers) | ||
| } | ||
|
|
||
| onResponseError (controller, err) { | ||
| this.#release() | ||
| return super.onResponseError(controller, err) | ||
| } | ||
|
|
||
| #release () { | ||
| if (this.#priorityQueue) { | ||
| const priorityQueue = this.#priorityQueue | ||
| this.#priorityQueue = null | ||
| priorityQueue.release() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function createPriorityInterceptor ({ concurrency } = { concurrency: 1 }) { | ||
| return (dispatch) => { | ||
| const queues = new Map() | ||
|
|
||
| return function priorityInterceptor (opts, handler) { | ||
| if (opts.priority == null || !opts.origin) { | ||
| return dispatch(opts, handler) | ||
| } | ||
|
|
||
| let queue = queues.get(opts.origin) | ||
| if (!queue) { | ||
| queue = new PriorityQueue(concurrency) | ||
| queues.set(opts.origin, queue) | ||
| } | ||
|
|
||
| queue.acquire(() => { | ||
| const priorityHandler = new PriorityHandler(handler, queue) | ||
| try { | ||
| dispatch(opts, priorityHandler) | ||
| } catch (err) { | ||
| priorityHandler.onResponseError(null, err) | ||
| } | ||
| }, opts.priority) | ||
amyssnippet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = createPriorityInterceptor | ||
This file contains hidden or 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,165 @@ | ||
| 'use strict' | ||
|
|
||
| const { createServer } = require('node:http') | ||
| const { describe, test, after } = require('node:test') | ||
| const { once } = require('node:events') | ||
| const { strictEqual, deepStrictEqual } = require('node:assert') | ||
| const { setTimeout: sleep } = require('node:timers/promises') | ||
| const { Client, interceptors } = require('../../index') | ||
|
|
||
| describe('Priority Interceptor', () => { | ||
| test('dispatches requests without priority normally', async () => { | ||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| res.end('ok') | ||
| }).listen(0) | ||
|
|
||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.priority()) | ||
|
|
||
| after(async () => { | ||
| await client.close() | ||
| server.close() | ||
| await once(server, 'close') | ||
| }) | ||
|
|
||
| const res = await client.request({ | ||
| origin: `http://localhost:${server.address().port}`, | ||
| method: 'GET', | ||
| path: '/' | ||
| }) | ||
|
|
||
| const body = await res.body.text() | ||
| strictEqual(res.statusCode, 200) | ||
| strictEqual(body, 'ok') | ||
| }) | ||
|
|
||
| test('dispatches requests with priority', async () => { | ||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| res.end('ok') | ||
| }).listen(0) | ||
|
|
||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.priority()) | ||
|
|
||
| after(async () => { | ||
| await client.close() | ||
| server.close() | ||
| await once(server, 'close') | ||
| }) | ||
|
|
||
| const res = await client.request({ | ||
| origin: `http://localhost:${server.address().port}`, | ||
| method: 'GET', | ||
| path: '/', | ||
| priority: 1 | ||
| }) | ||
|
|
||
| const body = await res.body.text() | ||
| strictEqual(res.statusCode, 200) | ||
| strictEqual(body, 'ok') | ||
| }) | ||
|
|
||
| test('higher priority requests are dispatched first', async () => { | ||
| const order = [] | ||
| const server = createServer({ joinDuplicateHeaders: true }, async (req, res) => { | ||
| await sleep(50) | ||
| order.push(req.url) | ||
| res.end(req.url) | ||
amyssnippet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }).listen(0) | ||
|
|
||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.priority({ concurrency: 1 })) | ||
|
|
||
| after(async () => { | ||
| await client.close() | ||
| server.close() | ||
| await once(server, 'close') | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| // Send requests with different priorities | ||
| // With concurrency 1, the first request dispatches immediately. | ||
| // The remaining requests queue by priority (higher = first). | ||
| const results = await Promise.all([ | ||
| client.request({ origin, method: 'GET', path: '/first', priority: 1 }), | ||
| client.request({ origin, method: 'GET', path: '/high', priority: 10 }), | ||
| client.request({ origin, method: 'GET', path: '/low', priority: 0 }), | ||
| client.request({ origin, method: 'GET', path: '/medium', priority: 5 }) | ||
| ]) | ||
|
|
||
| // Read all bodies to ensure completion | ||
| for (const res of results) { | ||
| await res.body.text() | ||
| } | ||
|
|
||
| // The first request dispatched immediately, then high, medium, low | ||
| deepStrictEqual(order, ['/first', '/high', '/medium', '/low']) | ||
| }) | ||
|
|
||
| test('requests without priority bypass the queue', async () => { | ||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| res.end('ok') | ||
| }).listen(0) | ||
|
|
||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.priority()) | ||
|
|
||
| after(async () => { | ||
| await client.close() | ||
| server.close() | ||
| await once(server, 'close') | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| // Request without priority should go through directly | ||
| const res = await client.request({ | ||
| origin, | ||
| method: 'GET', | ||
| path: '/' | ||
| }) | ||
|
|
||
| const body = await res.body.text() | ||
| strictEqual(res.statusCode, 200) | ||
| strictEqual(body, 'ok') | ||
| }) | ||
|
|
||
| test('handles request errors gracefully', async () => { | ||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| res.destroy() | ||
| }).listen(0) | ||
|
|
||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.priority()) | ||
|
|
||
| after(async () => { | ||
| await client.close() | ||
| server.close() | ||
| await once(server, 'close') | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| await client.request({ | ||
| origin, | ||
| method: 'GET', | ||
| path: '/', | ||
| priority: 1 | ||
| }).then(() => { | ||
| throw new Error('should have thrown') | ||
| }).catch((err) => { | ||
| strictEqual(err.code, 'UND_ERR_SOCKET') | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.