Skip to content

Commit

Permalink
Reuse backoff waiting code for initial proxy MCU connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Feb 27, 2024
1 parent bde0b08 commit 1a8444c
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions proxy/proxy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,15 @@ func (s *ProxyServer) Start(config *goconf.ConfigFile) error {
mcuType = signaling.McuTypeDefault
}

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
defer signal.Stop(interrupt)
backoff, err := signaling.NewExponentialBackoff(initialMcuRetry, maxMcuRetry)
if err != nil {
return err
}

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

var err error
var mcu signaling.Mcu
mcuRetry := initialMcuRetry
mcuRetryTimer := time.NewTimer(mcuRetry)
for {
switch mcuType {
case signaling.McuTypeJanus:
Expand All @@ -263,17 +264,10 @@ func (s *ProxyServer) Start(config *goconf.ConfigFile) error {
break
}

log.Printf("Could not initialize %s MCU at %s (%s) will retry in %s", mcuType, s.url, err, mcuRetry)
mcuRetryTimer.Reset(mcuRetry)
select {
case <-interrupt:
log.Printf("Could not initialize %s MCU at %s (%s) will retry in %s", mcuType, s.url, err, backoff.NextWait())
backoff.Wait(ctx)
if ctx.Err() != nil {
return fmt.Errorf("Cancelled")
case <-mcuRetryTimer.C:
// Retry connection
mcuRetry = mcuRetry * 2
if mcuRetry > maxMcuRetry {
mcuRetry = maxMcuRetry
}
}
}

Expand Down

0 comments on commit 1a8444c

Please sign in to comment.