fix: synchronize logs, buffer events channel, exit on goroutine failure

- Add mutex to prevent interleaved log output from concurrent goroutines
- Make logMsg delegate to logPrintf for consistent synchronization
- Buffer events channel to size 1 with select/drop when full
- Close events channel on ListenForTrainer exit so process terminates
This commit is contained in:
Erik Brakkee 2026-05-26 21:41:10 +00:00
parent 091067593b
commit 4949beae3e
4 changed files with 14 additions and 3 deletions

BIN
bridge

Binary file not shown.

View File

@ -28,6 +28,8 @@ func resetAndWait(drv io.ReadWriter) error {
func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
events chan<- Event) {
defer close(events)
drv, err := usb.GetDevice(gousb.ID(vendorId), gousb.ID(productId))
if err != nil {
panic(err)
@ -66,9 +68,13 @@ func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
cur := ant.PowerMessage(msg)
logPrintf("ant: %v: power: %dW cadence: %d rpm\n",
msg.DeviceNumber(), cur.InstantaneousPower(), cur.InstantaneousCadence())
events <- Event{
select {
case events <- Event{
Power: cur.InstantaneousPower(),
Cadence: cur.InstantaneousCadence(),
}:
default:
logPrintf("ant: dropped event, buffer full\n")
}
}
}

View File

@ -73,7 +73,7 @@ This allows these trainers to be used with modern devices that don't support ANT
logPrintf("hciconfig up failed: %v\n", err)
}
events := make(chan Event)
events := make(chan Event, 1)
go ListenForTrainer(uint16(c.vendorID), uint16(c.productID), c.deviceNumber, events)
ExposeBluetooth(events)

View File

@ -2,15 +2,20 @@ package main
import (
"fmt"
"sync"
"time"
)
var logMu sync.Mutex
func logMsg(msg string) {
fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " " + msg)
logPrintf("%s\n", msg)
}
func logPrintf(format string, a ...any) {
logMu.Lock()
fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" "+format, a...)
logMu.Unlock()
}
func logMsgf(format string, a ...any) {