Skip to content

Commit

Permalink
RCT: re-add connection sharing (#19379)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Mar 3, 2025
1 parent 22ae116 commit 575d20c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
29 changes: 18 additions & 11 deletions meter/rct.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"math"
"math/rand/v2"
"strings"
"sync"
"time"
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion templates/definition/meter/rct-power.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ params:
advanced: true
- name: cache
advanced: true
default: 5s
default: 10s
render: |
type: rct
uri: {{ .host }}
Expand Down

0 comments on commit 575d20c

Please sign in to comment.