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

RCT: re-add connection sharing #19379

Merged
merged 4 commits into from
Mar 3, 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
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
Loading