Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): Replace second layer of http cache with setting cache headers #18601

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions plugin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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{
andig marked this conversation as resolved.
Show resolved Hide resolved
"Cache-Control": cacheHeader,
}),
Base: p.Client.Transport,
}
}

// ignore the self signed certificate
if insecure {
p.Client.Transport = request.NewTripper(log, transport.Insecure())
Expand Down Expand Up @@ -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)
}
var b io.Reader
if p.method != http.MethodGet {
b = strings.NewReader(body)
}

url := util.DefaultScheme(url, "http")
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
}
// 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
}
val, err := p.DoBody(req)
if err != nil {
if err2 := knownErrors(val); err2 != nil {
err = err2
}
p.updated = time.Now()
}

return p.val, p.err
return val, err
}

var _ Getters = (*HTTP)(nil)
Expand Down
11 changes: 11 additions & 0 deletions templates/definition/meter/enphase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: .consumption[] | select(.measurementType == "net-consumption").wNow
currents:
- source: http
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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" }}
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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" }}
Expand All @@ -120,6 +129,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: .storage[] | .wNow
soc:
source: http
Expand All @@ -130,6 +140,7 @@ render: |
password: {{ .token }}
insecure: true
{{- end }}
cache: 5s
jq: '[.[].devices[] | select(.percentFull != null) | .percentFull] | add / length'
capacity: {{ .capacity }} # kWh
{{- end }}