chore: lock optimizer against piling goroutines
This commit is contained in:
parent
de9b2635e4
commit
43d514f224
1 changed files with 7 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
evopt "github.com/andig/evopt/client"
|
||||
|
|
@ -27,6 +28,7 @@ var (
|
|||
batteryPower = float32(6000) // default power of the battery in W
|
||||
|
||||
updated time.Time
|
||||
mu atomic.Uint32
|
||||
)
|
||||
|
||||
type batteryType string
|
||||
|
|
@ -52,8 +54,13 @@ type responseDetails struct {
|
|||
func (site *Site) optimizerUpdateAsync(battery []measurement) {
|
||||
var err error
|
||||
|
||||
if !mu.CompareAndSwap(0, 1) {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
updated = time.Now()
|
||||
mu.Store(0)
|
||||
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("panic %v", r)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue