evcc-io/util/tee.go
andig 4718e5f90f
Some checks failed
Release / Hassio Addon (push) Has been cancelled
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Demo (push) Has been cancelled
Tariffs: reduce published data volume by x10 (#24375)
2025-10-14 15:14:39 +00:00

56 lines
1.3 KiB
Go

package util
import (
"sync"
"github.com/evcc-io/evcc/api"
)
// TeeAttacher allows attaching a listener to a tee
type TeeAttacher interface {
Attach() <-chan Param
}
// Tee distributes parameters to subscribers
type Tee struct {
mu sync.Mutex
recv []chan<- Param
}
// Attach creates a new receiver channel and attaches it to the tee
func (t *Tee) Attach() <-chan Param {
// TODO find better approach to prevent deadlocks
// this will buffer the receiver channel to prevent deadlocks when consumers use mutex-protected loadpoint api
out := make(chan Param, 128)
t.add(out)
return out
}
// add attaches a receiver channel to the tee
func (t *Tee) add(out chan<- Param) {
t.mu.Lock()
defer t.mu.Unlock()
t.recv = append(t.recv, out)
}
// Run starts parameter distribution
func (t *Tee) Run(in <-chan Param) {
for msg := range in {
// TODO MUST NOT PUBLISH POINTERS (WHO'S VALUES ARE LATER MODIFIED)
// if val := reflect.ValueOf(msg.Val); val.Kind() == reflect.Ptr {
// if ptr := reflect.Indirect(val); ptr.IsValid() {
// fmt.Println("DANGER pointer value:", msg.Key)
// }
// }
if val, ok := (msg.Val).(api.Redactor); ok {
msg.Val = val.Redacted()
}
t.mu.Lock()
for _, recv := range t.recv {
recv <- msg
}
t.mu.Unlock()
}
}