Skip to content

Commit

Permalink
refactor: readd hub sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 10, 2022
1 parent 7ec90e5 commit 29b688f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pkg/radio/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"
"sync"
"time"

"github.com/ItsNotGoodName/go-upnpsub"
"github.com/huin/goupnp"
Expand Down Expand Up @@ -111,9 +112,11 @@ func (h *Hub) mutatorStart(ctx context.Context) {
func (h *Hub) Start(ctx context.Context) {
go h.mutatorStart(ctx)

firstRun := true

discover := func(cancel context.CancelFunc) (context.CancelFunc, error) {
newCtx, newCancel := context.WithCancel(ctx)
newRadios, err := h.discover(newCtx)
radioCTX, newCancel := context.WithCancel(ctx)
newRadios, err := h.discover(radioCTX)
if err != nil {
newCancel()
return nil, err
Expand All @@ -128,6 +131,17 @@ func (h *Hub) Start(ctx context.Context) {
cancel()
}

if !firstRun {
// Sleep for a while to allow radios to get their initial state and to prevent spamming the discovery channel.
select {
case <-ctx.Done():
newCancel()
return nil, ctx.Err()
case <-time.After(3 * time.Second):
}
firstRun = false
}

h.radiosMu.Lock()
h.radios = radios
h.radiosMu.Unlock()
Expand Down

0 comments on commit 29b688f

Please sign in to comment.