Skip to content

Set cache control in the HTTP-RPC response header#6265

Merged
cmwaters merged 5 commits intotendermint:masterfrom
JayT106:http-cache
Apr 14, 2021
Merged

Set cache control in the HTTP-RPC response header#6265
cmwaters merged 5 commits intotendermint:masterfrom
JayT106:http-cache

Conversation

@JayT106
Copy link
Contributor

@JayT106 JayT106 commented Mar 23, 2021

This PR added the "cache-control" into the HTTP-RPC response header and set the cache expired time to one year

Closes #5764

return fmt.Errorf("json marshal: %w", err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "max-age=31536000") // expired after one year
Copy link

@yihuang yihuang Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's some endpoints are not supposed to be cached, for example /status, the response will change according to current status.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this is advisable. We should instead consider adding a cache policy on individual requests. Also, one year seems too large, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexanderbez Agree with the cache policy that might need to be added. @yihuang, I will check the RPCs and add constrain to them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexanderbez Was my intuition, however, found a link to explain why set the age to one year if we see the data is immutable (but varnish cache doesn't support it)
https://ashton.codes/set-cache-control-max-age-1-year/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. If the response data does not change, then this makes sense 👍

@codecov
Copy link

codecov bot commented Mar 23, 2021

Codecov Report

Merging #6265 (dcee6f1) into master (b517dd5) will decrease coverage by 0.09%.
The diff coverage is 60.00%.

@@            Coverage Diff             @@
##           master    #6265      +/-   ##
==========================================
- Coverage   60.90%   60.80%   -0.10%     
==========================================
  Files         282      282              
  Lines       26866    26885      +19     
==========================================
- Hits        16362    16347      -15     
- Misses       8804     8835      +31     
- Partials     1700     1703       +3     
Impacted Files Coverage Δ
rpc/core/routes.go 0.00% <0.00%> (ø)
rpc/jsonrpc/server/http_uri_handler.go 32.47% <0.00%> (ø)
light/client.go 73.24% <33.33%> (ø)
rpc/jsonrpc/server/http_server.go 64.03% <66.66%> (+0.64%) ⬆️
rpc/jsonrpc/server/http_json_handler.go 61.48% <70.58%> (+1.63%) ⬆️
rpc/jsonrpc/server/rpc_func.go 93.54% <100.00%> (+0.21%) ⬆️
blockchain/v0/pool.go 74.52% <0.00%> (-4.57%) ⬇️
consensus/reactor.go 68.57% <0.00%> (-2.95%) ⬇️
p2p/transport_memory.go 83.78% <0.00%> (-2.71%) ⬇️
blockchain/v0/reactor.go 63.20% <0.00%> (-1.60%) ⬇️
... and 11 more

@JayT106 JayT106 requested a review from cmwaters as a code owner March 23, 2021 15:37
@JayT106
Copy link
Contributor Author

JayT106 commented Mar 23, 2021

@alexanderbez are these routes files not allowed to modify?

@alexanderbez
Copy link
Contributor

Not sure I follow @JayT106? What do you mean exactly? Can they override the HTTP header?

@JayT106
Copy link
Contributor Author

JayT106 commented Mar 24, 2021

Not sure I follow @JayT106? What do you mean exactly? Can they override the HTTP header?
@alexanderbez
My bad, I didn't solve the file conflict (and rebase correctly) in the PR.

@JayT106
Copy link
Contributor Author

JayT106 commented Mar 24, 2021

golangci-lint 2 errors related to the unused code. Can I just remove it?
::error file=light/proxy/routes.go,line=162,col=6::makeBlockSearchFunc is unused (deadcode)
::error file=light/proxy/routes.go,line=154,col=6::type rpcBlockSearchFunc is unused (unused)

@JayT106 JayT106 force-pushed the http-cache branch 5 times, most recently from 1edecb2 to 1feb651 Compare March 24, 2021 15:35
@JayT106 JayT106 requested a review from tychoish as a code owner April 1, 2021 02:34
Comment on lines +60 to +64
// Set the default response cache to true unless we see any error and any rpc request doesn't
// allow to be cached
var c = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one other condition that we have to be wary about. For a lot of RPC requests i.e validators, consensus params, blocks and commits, if a client requests this data with a height of 0 the node responds by sending that data type at the latest height. This means that the same request of height 0 to these RPC endpoints should also over time send different responses and thus shouldn't cache the response

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I will check the condition and revise the logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Once this is done I think the PR should be ready to merge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added default height checking for these types of RPC requests.

@JayT106 JayT106 requested a review from tac0turtle as a code owner April 14, 2021 08:05
Copy link
Contributor

@cmwaters cmwaters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @JayT106 🚀

@cmwaters cmwaters merged commit ca7dbea into tendermint:master Apr 14, 2021
Bro-Chain added a commit to Bro-Chain/tendermint that referenced this pull request Oct 13, 2022
@thanethomson thanethomson mentioned this pull request Oct 31, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rpc: http jsonrpc don't set cache-control in response

4 participants