Skip to content

Add support for Priority Hints #1748

Open

Description

@pmeenan

The Priority Hints spec is in the process of being merged into the fetch and HTML specs and allows for site developers to hint at the relative fetch priority for various resources.

The main changes are:

Adds a "priority" to fetch's Request and RequestInit classes that uses a string enum of "auto", "high" or "low"
Adds a "fetchPriority" attribute to the IMG, PICTURE, SCRIPT, LINK and IFRAME html elements with the same enum values
Existing spec: https://wicg.github.io/priority-hints/

This was shipped in Chromium 103 and Mozilla is working on an implementation so it is now in the process of being moved into the HTML and fetch specs.

For node, the main use case is on the fetch interface to pass through prioritization to the underlying networking stack.

Activity

  1. transferred this issue fromnodejs/nodeon Nov 1, 2022
  2. mcollina commented on Nov 1, 2022

    @mcollina
    SponsorMember

    This would be an interesting use case to support. While Node.js does not offer much in terms of prioritization from the network stack, Undici queue logic is a prime candidate for this.

    My take is that a high priority request jumps at the top of the queue and it's not pipelineable.

    Wdyt?

  3. KhafraDev commented on Nov 8, 2022

    @KhafraDev
    Member

    I looked into this but I'm not super familiar with undici's queuing logic. From my understanding, after a request is added to the queue, it's sychronously queued & removed from the queue. So for fetch, assuming that the dispatcher is a default Agent, a priority option wouldn't have an affect, as there isn't a time where multiple requests are in the queue.

    Test code:

    import { fetch } from './index.js'
    
    const result = await Promise.race([
      fetch('https://www.google.com/1').then(() => 'first'),
      fetch('https://www.google.com/2').then(() => 'second')
    ])
    
    console.log(result)

    (of course which request resolves first has no relevance here)

    Where the queue looks like:

    [
      Request {
        headersTimeout: 300000,
        bodyTimeout: 300000,
        throwOnError: false,
        method: 'GET',
        body: null,
        completed: false,
        aborted: false,
        upgrade: null,
        path: '/1',
        origin: 'https://www.google.com',
        idempotent: true,
        blocking: false,
        host: null,
        contentLength: null,
        contentType: null,
        headers: 'accept: */*\r\n' +
          'accept-language: *\r\n' +
          'sec-fetch-mode: cors\r\n' +
          'user-agent: undici\r\n' +
          'accept-encoding: br, gzip, deflate\r\n',
        servername: null,
        [Symbol(handler)]: {
          body: null,
          abort: null,
          onConnect: [Function: onConnect],
          onHeaders: [Function: onHeaders],
          onData: [Function: onData],
          onComplete: [Function: onComplete],
          onError: [Function: onError]
        }
      }
    ]
    [
      Request {
        headersTimeout: 300000,
        bodyTimeout: 300000,
        throwOnError: false,
        method: 'GET',
        body: null,
        completed: false,
        aborted: false,
        upgrade: null,
        path: '/2',
        origin: 'https://www.google.com',
        idempotent: true,
        blocking: false,
        host: null,
        contentLength: null,
        contentType: null,
        headers: 'accept: */*\r\n' +
          'accept-language: *\r\n' +
          'sec-fetch-mode: cors\r\n' +
          'user-agent: undici\r\n' +
          'accept-encoding: br, gzip, deflate\r\n',
        servername: null,
        [Symbol(handler)]: {
          body: null,
          abort: null,
          onConnect: [Function: onConnect],
          onHeaders: [Function: onHeaders],
          onData: [Function: onData],
          onComplete: [Function: onComplete],
          onError: [Function: onError]
        }
      }
    ]

    For both requests, there is only 1 item in the queue. Adding more requests, we see the same result - there's only ever 1 request queued at a time, and always in the order in which they were dispatched.


    I am probably missing something here, but I don't understand how it would be possible to prioritize a certain request. Any help would be appreciated 😄

  4. KhafraDev commented on Sep 2, 2023

    @KhafraDev
    Member

    cc @mcollina @ronag @metcoder95 is this something we can do?

  5. metcoder95 commented on Sep 2, 2023

    @metcoder95
    Member

    @mcollina and @ronag might have better insights than me on this, but my take is that it should be possible.

    AFAICT, we've either of two options:

    1. Do we order the queue at the moment of dispatching the request, right here:
      this[kQueue].push(request)
    2. Or, do we find a better way to manage the runningIdx, here:
      const request = client[kQueue][client[kPendingIdx]]

    Maybe a third one is to keep a second queue that's always ordered, and balance the load between the both? e.g. the ordered queue gets flushed first if it has requests with assigned priority, meanwhile the ones without priority remain at the normal queue, and continue running after the ordered one is empty.

  6. mcollina commented on Sep 3, 2023

    @mcollina
    SponsorMember

    Do we order the queue at the moment of dispatching the request, right here:

    this[kQueue].push(request)

    This is the place I had in in mind.

  7. added
    enhancement
    on Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions