Plugins: force http cache by setting cache headers (#18601)

This commit is contained in:
Lenart Kos 2025-02-05 09:10:25 +01:00 • committed by GitHub
parent c6ddc9b856
commit 87e1c929a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 27 deletions

View file

@ -23,11 +23,7 @@ type HTTP struct {
url, method string
headers map[string]string
body string
cache time.Duration
updated time.Time
pipeline *pipeline.Pipeline
val []byte // Cached http response value
err error // Cached http response error
}
func init() {
@ -99,7 +95,6 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur
Helper: request.NewHelper(log),
url: uri,
method: method,
cache: cache,
}
// http cache
@ -108,6 +103,16 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur
Transport: p.Client.Transport,
}
if cache > 0 {
cacheHeader := fmt.Sprintf("max-age=%d, must-revalidate", int(cache.Seconds()))
p.Client.Transport = &transport.Decorator{
Decorator: transport.DecorateHeaders(map[string]string{
"Cache-Control": cacheHeader,
}),
Base: p.Client.Transport,
}
}
// ignore the self signed certificate
if insecure {
p.Client.Transport = request.NewTripper(log, transport.Insecure())
@ -152,30 +157,27 @@ func (p *HTTP) WithAuth(typ, user, password string) (*HTTP, error) {
// request executes the configured request or returns the cached value
func (p *HTTP) request(url string, body string) ([]byte, error) {
if time.Since(p.updated) >= p.cache {
var b io.Reader
if p.method != http.MethodGet {
b = strings.NewReader(body)
}
url := util.DefaultScheme(url, "http")
// empty method becomes GET
req, err := request.New(p.method, url, b, p.headers)
if err != nil {
return []byte{}, err
}
p.val, p.err = p.DoBody(req)
if p.err != nil {
if err := knownErrors(p.val); err != nil {
p.err = err
}
}
p.updated = time.Now()
var b io.Reader
if p.method != http.MethodGet {
b = strings.NewReader(body)
}
return p.val, p.err
url = util.DefaultScheme(url, "http")
// empty method becomes GET
req, err := request.New(p.method, url, b, p.headers)
if err != nil {
return []byte{}, err
}
val, err := p.DoBody(req)
if err != nil {
if err2 := knownErrors(val); err2 != nil {
err = err2
}
}
return val, err
}
var _ Getters = (*HTTP)(nil)

View file

@ -29,6 +29,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: .consumption[] | select(.measurementType == "net-consumption").wNow
currents:
- source: http
@ -39,6 +40,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (( .consumption[] | select(.measurementType == "net-consumption").activeCount >= 1 ) and ( .consumption[] | select(.measurementType == "net-consumption").lines | length >= 1 )) then .consumption[] | select(.measurementType == "net-consumption").lines[0].rmsCurrent else 0 end
- source: http
uri: http://{{ .host }}/production.json?details=1
@ -48,6 +50,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (( .consumption[] | select(.measurementType == "net-consumption").activeCount >= 1 ) and ( .consumption[] | select(.measurementType == "net-consumption").lines | length >= 2 )) then .consumption[] | select(.measurementType == "net-consumption").lines[1].rmsCurrent else 0 end
- source: http
uri: http://{{ .host }}/production.json?details=1
@ -57,6 +60,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (( .consumption[] | select(.measurementType == "net-consumption").activeCount >= 1 ) and ( .consumption[] | select(.measurementType == "net-consumption").lines | length >= 3 )) then .consumption[] | select(.measurementType == "net-consumption").lines[2].rmsCurrent else 0 end
{{- end }}
{{- if eq .usage "pv" }}
@ -69,6 +73,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (.production | length) > 1 and (.production[] | select(.measurementType == "production").activeCount >= 1) then .production[] | select(.measurementType == "production").wNow else .production[] | select(.type == "inverters").wNow end
energy:
source: http
@ -79,6 +84,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (.production | length) > 1 and (.production[] | select(.measurementType == "production").activeCount >= 1) then .production[] | select(.measurementType == "production").whLifetime else .production[] | select(.type == "inverters").whLifetime end
scale: 0.001
currents:
@ -90,6 +96,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (( .production[] | select(.measurementType == "production").activeCount >= 1 ) and ( .production[] | select(.measurementType == "production").lines | length >= 1 )) then .production[] | select(.measurementType == "production").lines[0].rmsCurrent else 0 end
- source: http
uri: http://{{ .host }}/production.json?details=1
@ -99,6 +106,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (( .production[] | select(.measurementType == "production").activeCount >= 1 ) and ( .production[] | select(.measurementType == "production").lines | length >= 2 )) then .production[] | select(.measurementType == "production").lines[1].rmsCurrent else 0 end
- source: http
uri: http://{{ .host }}/production.json?details=1
@ -108,6 +116,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: if (( .production[] | select(.measurementType == "production").activeCount >= 1 ) and ( .production[] | select(.measurementType == "production").lines | length >= 3 )) then .production[] | select(.measurementType == "production").lines[2].rmsCurrent else 0 end
{{- end }}
{{- if eq .usage "battery" }}
@ -120,6 +129,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: .storage[] | .wNow
soc:
source: http
@ -130,6 +140,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: '[.[].devices[] | select(.percentFull != null) | .percentFull] | add / length'
capacity: {{ .capacity }} # kWh
{{- end }}