Skip to main content

A pure-Python implementation of the HTTP/2 priority tree

Project description

Build Status Code Coverage Documentation Status Chat community https://raw.github.com/python-hyper/documentation/master/source/logo/hyper-black-bg-white.png

Priority is a pure-Python implementation of the priority logic for HTTP/2, set out in RFC 7540 Section 5.3 (Stream Priority). This logic allows for clients to express a preference for how the server allocates its (limited) resources to the many outstanding HTTP requests that may be running over a single HTTP/2 connection.

Specifically, this Python implementation uses a variant of the implementation used in the excellent H2O project. This original implementation is also the inspiration for nghttp2’s priority implementation, and generally produces a very clean and even priority stream. The only notable changes from H2O’s implementation are small modifications to allow the priority implementation to work cleanly as a separate implementation, rather than being embedded in a HTTP/2 stack directly.

While priority information in HTTP/2 is only a suggestion, rather than an enforceable constraint, where possible servers should respect the priority requests of their clients.

Using Priority

Priority has a simple API. Streams are inserted into the tree: when they are inserted, they may optionally have a weight, depend on another stream, or become an exclusive dependent of another stream.

>>> p = priority.PriorityTree()
>>> p.insert_stream(stream_id=1)
>>> p.insert_stream(stream_id=3)
>>> p.insert_stream(stream_id=5, depends_on=1)
>>> p.insert_stream(stream_id=7, weight=32)
>>> p.insert_stream(stream_id=9, depends_on=7, weight=8)
>>> p.insert_stream(stream_id=11, depends_on=7, exclusive=True)

Once streams are inserted, the stream priorities can be requested. This allows the server to make decisions about how to allocate resources.

Iterating The Tree

The tree in this algorithm acts as a gate. Its goal is to allow one stream “through” at a time, in such a manner that all the active streams are served as evenly as possible in proportion to their weights.

This is handled in Priority by iterating over the tree. The tree itself is an iterator, and each time it is advanced it will yield a stream ID. This is the ID of the stream that should next send data.

This looks like this:

>>> for stream_id in p:
...     send_data(stream_id)

If each stream only sends when it is ‘ungated’ by this mechanism, the server will automatically be emitting stream data in conformance to RFC 7540.

Updating The Tree

If for any reason a stream is unable to proceed (for example, it is blocked on HTTP/2 flow control, or it is waiting for more data from another service), that stream is blocked. The PriorityTree should be informed that the stream is blocked so that other dependent streams get a chance to proceed. This can be done by calling the block method of the tree with the stream ID that is currently unable to proceed. This will automatically update the tree, and it will adjust on the fly to correctly allow any streams that were dependent on the blocked one to progress.

For example:

>>> for stream_id in p:
...     send_data(stream_id)
...     if blocked(stream_id):
...         p.block(stream_id)

When a stream goes from being blocked to being unblocked, call the unblock method to place it back into the sequence. Both the block and unblock methods are idempotent and safe to call repeatedly.

Additionally, the priority of a stream may change. When it does, the reprioritize method can be used to update the tree in the wake of that change. reprioritize has the same signature as insert_stream, but applies only to streams already in the tree.

Removing Streams

A stream can be entirely removed from the tree by calling remove_stream. Note that this is not idempotent. Further, calling remove_stream and then re-adding it may cause a substantial change in the shape of the priority tree, and will cause the iteration order to change.

License

Priority is made available under the MIT License. For more details, see the LICENSE file in the repository.

Authors

Priority is maintained by Cory Benfield, with contributions from others. For more details about the contributors, please see CONTRIBUTORS.rst in the repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

priority-2.0.0.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

priority-2.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file priority-2.0.0.tar.gz.

File metadata

  • Download URL: priority-2.0.0.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for priority-2.0.0.tar.gz
Algorithm Hash digest
SHA256 c965d54f1b8d0d0b19479db3924c7c36cf672dbf2aec92d43fbdaf4492ba18c0
MD5 149531bf0e754782cf218bfc8cc994ae
BLAKE2b-256 f53ceb7c35f4dcede96fca1842dac5f4f5d15511aa4b52f3a961219e68ae9204

See more details on using hashes here.

File details

Details for the file priority-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: priority-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for priority-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f8eefce5f3ad59baf2c080a664037bb4725cd0a790d53d59ab4059288faf6aa
MD5 4b37219b8468723cfe978a541b941a7b
BLAKE2b-256 5e5f82c8074f7e84978129347c2c6ec8b6c59f3584ff1a20bc3c940a3e061790

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page