Skip to content

Commit

Permalink
handle proper channel closures
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Dec 14, 2024
1 parent 663c018 commit 2bd916d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
1 change: 1 addition & 0 deletions handlers/stream_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func StreamHandler(w http.ResponseWriter, r *http.Request, cm *store.Concurrency
}

exitStatus := make(chan int)
defer close(exitStatus)

utils.SafeLogf("Proxying %s to %s\n", r.RemoteAddr, selectedUrl)
go stream.ProxyStream(ctx, selectedIndex, resp, r, w, exitStatus)
Expand Down
21 changes: 5 additions & 16 deletions proxy/proxy_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,15 @@ func (instance *StreamInstance) ProxyStream(ctx context.Context, m3uIndex int, r
maxBackoff := time.Duration(timeoutSecond-1) * time.Second
currentBackoff := initialBackoff

contextSleep := func() bool {
contextSleep := func(ctx context.Context, timer *time.Timer) {
select {
case <-time.After(currentBackoff):
currentBackoff *= 2
if currentBackoff > maxBackoff {
currentBackoff = maxBackoff
}
return true
case <-ctx.Done():
utils.SafeLogf("Context canceled for stream: %s\n", r.RemoteAddr)
statusChan <- 0
return false
case <-timer.C:
utils.SafeLogf("Timeout reached while trying to stream: %s\n", r.RemoteAddr)
statusChan <- returnStatus
return false
}
}

Expand Down Expand Up @@ -147,9 +140,7 @@ func (instance *StreamInstance) ProxyStream(ctx context.Context, m3uIndex int, r
returnStatus = 2

utils.SafeLogf("Retrying same stream until timeout (%d seconds) is reached...\n", timeoutSecond)
if !contextSleep() {
return
}
contextSleep(ctx, timer)

continue
case err != nil:
Expand All @@ -162,9 +153,7 @@ func (instance *StreamInstance) ProxyStream(ctx context.Context, m3uIndex int, r
}

utils.SafeLogf("Retrying same stream until timeout (%d seconds) is reached...\n", timeoutSecond)
if !contextSleep() {
return
}
contextSleep(ctx, timer)

continue
}
Expand All @@ -189,9 +178,9 @@ func (instance *StreamInstance) ProxyStream(ctx context.Context, m3uIndex int, r
}
}
timer.Reset(timeoutDuration)
}

currentBackoff = initialBackoff
currentBackoff = initialBackoff
}
}
}
}

0 comments on commit 2bd916d

Please sign in to comment.