Set timeout for client disconnection

This commit is contained in:
Torjus Håkestad 2021-10-18 22:36:10 +02:00
parent a69606e9aa
commit dabbe5c305
2 changed files with 17 additions and 2 deletions

View File

@ -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 {
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.",

View File

@ -2,7 +2,7 @@ package dogtamer
import "fmt"
const Version string = "v0.1.6"
const Version string = "v0.1.7"
var Build string