Skip to content

Commit 72f92ae

Browse files
agent: add option to disable agent cache for HTTP endpoints (#8023)
This allows the operator to disable agent caching for the http endpoint. It is on by default for backwards compatibility and if disabled will ignore the url parameter `cached`.
1 parent 9262d7a commit 72f92ae

File tree

9 files changed

+69
-7
lines changed

9 files changed

+69
-7
lines changed

agent/catalog_endpoint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *HTTPServer) CatalogDatacenters(resp http.ResponseWriter, req *http.Requ
8484
parseCacheControl(resp, req, &args.QueryOptions)
8585
var out []string
8686

87-
if args.QueryOptions.UseCache {
87+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
8888
raw, m, err := s.agent.cache.Get(cachetype.CatalogDatacentersName, &args)
8989
if err != nil {
9090
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_datacenters"}, 1,
@@ -166,7 +166,7 @@ func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request
166166
var out structs.IndexedServices
167167
defer setMeta(resp, &out.QueryMeta)
168168

169-
if args.QueryOptions.UseCache {
169+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
170170
raw, m, err := s.agent.cache.Get(cachetype.CatalogListServicesName, &args)
171171
if err != nil {
172172
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_services"}, 1,
@@ -255,7 +255,7 @@ func (s *HTTPServer) catalogServiceNodes(resp http.ResponseWriter, req *http.Req
255255
var out structs.IndexedServiceNodes
256256
defer setMeta(resp, &out.QueryMeta)
257257

258-
if args.QueryOptions.UseCache {
258+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
259259
raw, m, err := s.agent.cache.Get(cachetype.CatalogServicesName, &args)
260260
if err != nil {
261261
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_service_nodes"}, 1,

agent/config/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
858858
HTTPBlockEndpoints: c.HTTPConfig.BlockEndpoints,
859859
HTTPResponseHeaders: c.HTTPConfig.ResponseHeaders,
860860
AllowWriteHTTPFrom: b.cidrsVal("allow_write_http_from", c.HTTPConfig.AllowWriteHTTPFrom),
861+
HTTPUseCache: b.boolValWithDefault(c.HTTPConfig.UseCache, true),
861862

862863
// Telemetry
863864
Telemetry: lib.TelemetryConfig{

agent/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ type HTTPConfig struct {
611611
BlockEndpoints []string `json:"block_endpoints,omitempty" hcl:"block_endpoints" mapstructure:"block_endpoints"`
612612
AllowWriteHTTPFrom []string `json:"allow_write_http_from,omitempty" hcl:"allow_write_http_from" mapstructure:"allow_write_http_from"`
613613
ResponseHeaders map[string]string `json:"response_headers,omitempty" hcl:"response_headers" mapstructure:"response_headers"`
614+
UseCache *bool `json:"use_cache,omitempty" hcl:"use_cache" mapstructure:"use_cache"`
614615
}
615616

616617
type Performance struct {

agent/config/runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ type RuntimeConfig struct {
342342
// hcl: dns_config { cache_max_age = "duration" }
343343
DNSCacheMaxAge time.Duration
344344

345+
// HTTPUseCache whether or not to use cache for http queries. Defaults
346+
// to true.
347+
//
348+
// hcl: http_config { use_cache = (true|false) }
349+
HTTPUseCache bool
350+
345351
// HTTPBlockEndpoints is a list of endpoint prefixes to block in the
346352
// HTTP API. Any requests to these will get a 403 response.
347353
//

agent/config/runtime_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,54 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
21182118
`},
21192119
err: "Serf Advertise WAN address 10.0.0.1:1000 already configured for RPC Advertise",
21202120
},
2121+
{
2122+
desc: "http use_cache defaults to true",
2123+
args: []string{
2124+
`-data-dir=` + dataDir,
2125+
},
2126+
json: []string{`{
2127+
"http_config": {}
2128+
}`},
2129+
hcl: []string{`
2130+
http_config = {}
2131+
`},
2132+
patch: func(rt *RuntimeConfig) {
2133+
rt.DataDir = dataDir
2134+
rt.HTTPUseCache = true
2135+
},
2136+
},
2137+
{
2138+
desc: "http use_cache is enabled when true",
2139+
args: []string{
2140+
`-data-dir=` + dataDir,
2141+
},
2142+
json: []string{`{
2143+
"http_config": { "use_cache": true }
2144+
}`},
2145+
hcl: []string{`
2146+
http_config = { use_cache = true }
2147+
`},
2148+
patch: func(rt *RuntimeConfig) {
2149+
rt.DataDir = dataDir
2150+
rt.HTTPUseCache = true
2151+
},
2152+
},
2153+
{
2154+
desc: "http use_cache is disabled when false",
2155+
args: []string{
2156+
`-data-dir=` + dataDir,
2157+
},
2158+
json: []string{`{
2159+
"http_config": { "use_cache": false }
2160+
}`},
2161+
hcl: []string{`
2162+
http_config = { use_cache = false }
2163+
`},
2164+
patch: func(rt *RuntimeConfig) {
2165+
rt.DataDir = dataDir
2166+
rt.HTTPUseCache = false
2167+
},
2168+
},
21212169
{
21222170
desc: "sidecar_service can't have ID",
21232171
args: []string{
@@ -4167,7 +4215,8 @@ func TestFullConfig(t *testing.T) {
41674215
"response_headers": {
41684216
"M6TKa9NP": "xjuxjOzQ",
41694217
"JRCrHZed": "rl0mTx81"
4170-
}
4218+
},
4219+
"use_cache": false
41714220
},
41724221
"key_file": "IEkkwgIA",
41734222
"leave_on_terminate": true,
@@ -4804,6 +4853,7 @@ func TestFullConfig(t *testing.T) {
48044853
"M6TKa9NP" = "xjuxjOzQ"
48054854
"JRCrHZed" = "rl0mTx81"
48064855
}
4856+
use_cache = false
48074857
}
48084858
key_file = "IEkkwgIA"
48094859
leave_on_terminate = true
@@ -5517,6 +5567,7 @@ func TestFullConfig(t *testing.T) {
55175567
HTTPMaxConnsPerClient: 100,
55185568
HTTPSHandshakeTimeout: 2391 * time.Millisecond,
55195569
HTTPSPort: 15127,
5570+
HTTPUseCache: false,
55205571
KeyFile: "IEkkwgIA",
55215572
KVMaxValueSize: 1234567800000000,
55225573
LeaveDrainTime: 8265 * time.Second,
@@ -6402,6 +6453,7 @@ func TestSanitize(t *testing.T) {
64026453
"HTTPMaxConnsPerClient": 0,
64036454
"HTTPPort": 0,
64046455
"HTTPResponseHeaders": {},
6456+
"HTTPUseCache": false,
64056457
"HTTPSAddrs": [],
64066458
"HTTPSHandshakeTimeout": "0s",
64076459
"HTTPSPort": 0,

agent/discovery_chain_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *HTTPServer) DiscoveryChainRead(resp http.ResponseWriter, req *http.Requ
6060
var out structs.DiscoveryChainResponse
6161
defer setMeta(resp, &out.QueryMeta)
6262

63-
if args.QueryOptions.UseCache {
63+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
6464
raw, m, err := s.agent.cache.Get(cachetype.CompiledDiscoveryChainName, &args)
6565
if err != nil {
6666
return nil, err

agent/health_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (s *HTTPServer) healthServiceNodes(resp http.ResponseWriter, req *http.Requ
198198
var out structs.IndexedCheckServiceNodes
199199
defer setMeta(resp, &out.QueryMeta)
200200

201-
if args.QueryOptions.UseCache {
201+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
202202
raw, m, err := s.agent.cache.Get(cachetype.HealthServicesName, &args)
203203
if err != nil {
204204
return nil, err

agent/prepared_query_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r
121121
var reply structs.PreparedQueryExecuteResponse
122122
defer setMeta(resp, &reply.QueryMeta)
123123

124-
if args.QueryOptions.UseCache {
124+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
125125
raw, m, err := s.agent.cache.Get(cachetype.PreparedQueryName, &args)
126126
if err != nil {
127127
// Don't return error if StaleIfError is set and we are within it and had

website/pages/docs/agent/options.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,8 @@ Valid time units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'."
14151415

14161416
- `allow_write_http_from` This object is a list of networks in CIDR notation (eg "127.0.0.0/8") that are allowed to call the agent write endpoints. It defaults to an empty list, which means all networks are allowed. This is used to make the agent read-only, except for select ip ranges. - To block write calls from anywhere, use `[ "255.255.255.255/32" ]`. - To only allow write calls from localhost, use `[ "127.0.0.0/8" ]` - To only allow specific IPs, use `[ "10.0.0.1/32", "10.0.0.2/32" ]`
14171417

1418+
- `use_cache` Defaults to true. If disabled, the agent won't be using [agent caching](/api/features/caching) to answer the request. Even when the url parameter is provided.
1419+
14181420
- `leave_on_terminate` If enabled, when the agent receives a TERM signal, it will send a `Leave` message to the rest of the cluster and gracefully leave. The default behavior for this feature varies based on whether or not the agent is running as a client or a server (prior to Consul 0.7 the default value was unconditionally set to `false`). On agents in client-mode, this defaults to `true` and for agents in server-mode, this defaults to `false`.
14191421

14201422
- `limits` Available in Consul 0.9.3 and later, this is a nested

0 commit comments

Comments
 (0)