From a153bf02d1cf05f020d263e9670a76ae99cfeb02 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 29 Jul 2023 19:14:17 +0800 Subject: Bugfix: heartbeat packets are sent irregularly when idle, causing EOF errors --- recording/watcher.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/recording/watcher.go b/recording/watcher.go index f06b405..515def1 100644 --- a/recording/watcher.go +++ b/recording/watcher.go @@ -70,7 +70,13 @@ func watch( // the danmaku server requires heartbeat messages every 30 seconds heartbeat := func() error { + logger.Debug("Sending heartbeat...") err := dm.Heartbeat() + if err == nil { + logger.Info("Heartbeat sent OK.") + } else { + logger.Error("Failed to send heartbeat: %v", err) + } return err } @@ -96,15 +102,27 @@ func watch( logger.Info("The live is not started yet. Waiting...") } + hbCtx, hbCancel := context.WithCancel(ctx) + defer hbCancel() + go func() { + for { + select { + case <-heartBeatTimer.C: + err = heartbeat() + if err != nil { + logger.Error("heartbeat failed: %v", err) + } + case <-hbCtx.Done(): + logger.Debug("Heartbeat loop is stopped.") + return + } + } + }() + for { select { case <-ctx.Done(): return ctx.Err() - case <-heartBeatTimer.C: - err = heartbeat() - if err != nil { - return errs.NewError(errs.Heartbeat, err) - } default: var msg dmpkg.DanmakuExchange msg, err = dm.ReadExchange() -- cgit v1.2.3