[go: up one dir, main page]

RPC: implement max-age cache header

What

Adds the Cache-control: public, max-age: <seconds> to head-n query responses for local server.

External RPC process is added in another MR because makes a small but fundamental change to the http cache header tools.

Why

Relative (head-n) queries are always changing which creates friction for integrating Octez RPCs with generic Caches as operators needs extra logic/heuristics to know when to invalidate such a response. To get around this, we introduce the max-age header which sets the invalidation time to the estimated number of seconds until the next round.

How

First, due to the lack of support in Resto of semantically defining headers in Services, the functionality is defined as a module in RPC_middleware. A middleware is simply a mapping from callback -> callback

The implementation is given for this middleware which requires checking that the request url starts with /chains/main/blocks/head* as http cache headers should only be included for head related queries. The Http_cache_headers module is oblivious to how to actually get the max-age value which is obtains via get_estimated_time_to_next_level function.

Next, a flag is exposed for enabling http cache headers, off by default.

Lastly, get the tools exposed via node in the previous MR to create the http cache header middleware and plug it into the local server only when the feature flag is enabled.

Tezt

dune exec tezt/tests/main.exe -- --file http_cache_headers.ml

Manually testing the MR

  1. Apply sandbox_block_time.patch to make sandbox minimal_block_delay=10 and delay_increment_per_round=8
git apply sandbox_block_time.patch
  1. Setup sandbox
make # generate new sandbox parameters
DATA_DIR=/tmp/sandboxnode ./src/bin_node/octez-sandboxed-node.sh 1 --connections 0 --enable-http-cache-headers
eval `./src/bin_client/octez-init-sandboxed-client.sh 1`
octez-activate-alpha
  1. Bake and check max age
# get client base dir
octez-client config show 
>> { "base_dir": "/tmp/tezos-tmp-client.tSf5JMUn" ... 

./octez-baker-alpha -d /tmp/tezos-tmp-client.tSf5JMUn --endpoint http://localhost:18731 run with local node /tmp/sandboxnode

In a separate terminal:

curl -i  http://localhost:18731/chains/main/blocks/head/hash

HTTP/1.1 200 OK
content-type: application/json
cache-control: public, max-age=7
content-length: 54

"BMZ88qKMoRafCC2ZCN9u2oCXnTkkDesnys5rzjiMxPZBYKoXEut"

If no new block arrive after max-age, the field will be omitted.

# ^C the baker

curl -i  http://localhost:18731/chains/main/blocks/head/hash

HTTP/1.1 200 OK
content-type: application/json
content-length: 54

"BMZ88qKMoRafCC2ZCN9u2oCXnTkkDesnys5rzjiMxPZBYKoXEut"

Checklist

  • Document the interface of any function added or modified (see the coding guidelines)
  • Document any change to the user interface, including configuration parameters (see node configuration)
  • Provide automatic testing (see the testing guide).
  • For new features and bug fixes, add an item in the appropriate changelog (docs/protocols/alpha.rst for the protocol and the environment, CHANGES.rst at the root of the repository for everything else).
  • Select suitable reviewers using the Reviewers field below.
  • Select as Assignee the next person who should take action on that MR
Edited by Ryan Tan

Merge request reports

Loading