RCT: re-add connection sharing (#19379)
This commit is contained in:
parent
22ae11618e
commit
575d20cddb
4 changed files with 22 additions and 15 deletions
2
go.mod
2
go.mod
|
|
@ -222,4 +222,4 @@ replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20241027151
|
|||
|
||||
replace github.com/lorenzodonini/ocpp-go => github.com/evcc-io/ocpp-go v0.0.0-20241230132027-815870498cc3
|
||||
|
||||
replace github.com/mlnoga/rct => github.com/andig/rct v0.1.2-0.20250303102452-38b496bdd2c9
|
||||
replace github.com/mlnoga/rct => github.com/andig/rct v0.1.2-0.20250303165133-baaf38f1674c
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -39,8 +39,8 @@ github.com/andig/gosunspec v0.0.0-20240918203654-860ce51d602b h1:81UMfM949I7StrR
|
|||
github.com/andig/gosunspec v0.0.0-20240918203654-860ce51d602b/go.mod h1:c6P6szcR+ROkqZruOR4f6qbDKFjZX6OitPpj+yJ/r8k=
|
||||
github.com/andig/mbserver v0.0.0-20230310211055-1d29cbb5820e h1:m/NTP3JWpR7M0ljLxiQU4fzR25jjhe1LDtxLMNcoNJQ=
|
||||
github.com/andig/mbserver v0.0.0-20230310211055-1d29cbb5820e/go.mod h1:4VtYzTm//oUipwvO3yh0g/udTE7pYJM+U/kyAuFDsgM=
|
||||
github.com/andig/rct v0.1.2-0.20250303102452-38b496bdd2c9 h1:hHUPfKTJlyHX0i5iZy7UNtIYoNDtcPH/Q1bbOe2OZ+g=
|
||||
github.com/andig/rct v0.1.2-0.20250303102452-38b496bdd2c9/go.mod h1:KRjBDV8kuvGd2kJfN8/F0kjH8JjkFVpBpn7MhW+ACL8=
|
||||
github.com/andig/rct v0.1.2-0.20250303165133-baaf38f1674c h1:J3nV3DPhqZWuKsPDW4vbZ743O9lpRkNb06P2e6bnSgY=
|
||||
github.com/andig/rct v0.1.2-0.20250303165133-baaf38f1674c/go.mod h1:KRjBDV8kuvGd2kJfN8/F0kjH8JjkFVpBpn7MhW+ACL8=
|
||||
github.com/andig/yaml v0.0.0-20240531135838-1ff5761ab467 h1:JqIoHxsQSV39xaemvVRu3HOkSf5AbhQ1YEENJa2cL3Q=
|
||||
github.com/andig/yaml v0.0.0-20240531135838-1ff5761ab467/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
|
||||
|
|
|
|||
29
meter/rct.go
29
meter/rct.go
|
|
@ -6,7 +6,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -22,7 +21,10 @@ type RCT struct {
|
|||
usage string // grid, pv, battery
|
||||
}
|
||||
|
||||
var rctMu sync.Mutex
|
||||
var (
|
||||
rctMu sync.Mutex
|
||||
rctCache = make(map[string]*rct.Connection)
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.AddCtx("rct", NewRCTFromConfig)
|
||||
|
|
@ -56,19 +58,24 @@ func NewRCTFromConfig(ctx context.Context, other map[string]interface{}) (api.Me
|
|||
func NewRCT(ctx context.Context, uri, usage string, minSoc, maxSoc int, cache time.Duration, capacity func() float64) (api.Meter, error) {
|
||||
log := util.NewLogger("rct")
|
||||
|
||||
// prevent concurrent connections
|
||||
// re-use connections
|
||||
rctMu.Lock()
|
||||
time.Sleep(time.Duration(rand.Uint64N(uint64(time.Second))))
|
||||
rctMu.Unlock()
|
||||
|
||||
conn, err := rct.NewConnection(ctx, uri, rct.WithErrorCallback(func(err error) {
|
||||
conn, ok := rctCache[uri]
|
||||
if !ok {
|
||||
var err error
|
||||
conn, err = rct.NewConnection(ctx, uri, rct.WithErrorCallback(func(err error) {
|
||||
if err != nil {
|
||||
log.ERROR.Println(err)
|
||||
}
|
||||
}), rct.WithLogger(log.TRACE.Printf), rct.WithTimeout(cache))
|
||||
if err != nil {
|
||||
log.ERROR.Println(err)
|
||||
rctMu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
}), rct.WithLogger(log.TRACE.Printf), rct.WithTimeout(cache))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
rctCache[uri] = conn
|
||||
}
|
||||
rctMu.Unlock()
|
||||
|
||||
m := &RCT{
|
||||
usage: strings.ToLower(usage),
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ params:
|
|||
advanced: true
|
||||
- name: cache
|
||||
advanced: true
|
||||
default: 5s
|
||||
default: 10s
|
||||
render: |
|
||||
type: rct
|
||||
uri: {{ .host }}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue