From dabbe5c30522055978f8cb50d7a885cb6dcd0566 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 18 Oct 2021 22:36:10 +0200 Subject: [PATCH] Set timeout for client disconnection --- server/rtmp.go | 17 ++++++++++++++++- version.go | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/server/rtmp.go b/server/rtmp.go index 07e70b2..03cde8f 100644 --- a/server/rtmp.go +++ b/server/rtmp.go @@ -98,9 +98,24 @@ func (s *Stream) handleBroadcaster(c *rtmp.Conn, nc net.Conn) { // Ensure cleanup defer func() { // Ensure all clients have been removed + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + for len(s.Clients) > 0 { - time.Sleep(200 * time.Millisecond) + select { + case <-ctx.Done(): + // Timed out while waiting for clients to disconnect, log and remove stream. + s.server.RemoveStream(s) + s.server.Logger.Errorw("Timeout while waiting for clients to disconnect from stream.", + "stream_name", s.Name, + "stream_duration", time.Since(s.startTime), + "bytes_received", humanize.Bytes(s.bytesReceived.Load())) + return + default: + time.Sleep(200 * time.Millisecond) + } } + // Once all clients have been removed, remove stream from server s.server.RemoveStream(s) s.server.Logger.Infow("Stream has been successfully stopped.", diff --git a/version.go b/version.go index 84d30ae..f29c063 100644 --- a/version.go +++ b/version.go @@ -2,7 +2,7 @@ package dogtamer import "fmt" -const Version string = "v0.1.6" +const Version string = "v0.1.7" var Build string