Http: when caching allow single inflight GET request only (#22959)

This commit is contained in:
andig 2025-08-15 08:55:22 +02:00 • committed by GitHub
parent 3c11886d35
commit ceb55dcae6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/evcc-io/evcc/plugin/pipeline"
@ -24,6 +25,7 @@ type HTTP struct {
headers map[string]string
body string
pipeline *pipeline.Pipeline
mu *sync.Mutex
}
func init() {
@ -113,6 +115,11 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur
}),
Base: p.Client.Transport,
}
// for cached requests enforce single inflight GET
if method == http.MethodGet {
p.mu = muForKey(p.url)
}
}
// ignore the self signed certificate
@ -170,6 +177,11 @@ var _ Getters = (*HTTP)(nil)
// StringGetter sends string request
func (p *HTTP) StringGetter() (func() (string, error), error) {
return func() (string, error) {
if p.mu != nil {
p.mu.Lock()
defer p.mu.Unlock()
}
url, err := setFormattedValue(p.url, "", "")
if err != nil {
return "", err

21
plugin/http_limit.go Normal file
View file

@ -0,0 +1,21 @@
package plugin
import "sync"
var (
httpMu sync.Mutex
httpMutexes = map[string]*sync.Mutex{}
)
func muForKey(key string) *sync.Mutex {
httpMu.Lock()
defer httpMu.Unlock()
if mu, ok := httpMutexes[key]; ok {
return mu
}
mu := new(sync.Mutex)
httpMutexes[key] = mu
return mu
}