Skip to content

Commit

Permalink
Merge pull request #163 from sonroyaalmerol/retry-rate
Browse files Browse the repository at this point in the history
Add RETRY_WAIT feature to set a wait time on the retry loop
  • Loading branch information
sonroyaalmerol authored Sep 22, 2024
2 parents a76d559 + 06ff06a commit f2ae129
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Access the generated M3U playlist at `http://<server ip>:8080/playlist.m3u`.
| M3U_URL_1, M3U_URL_2, M3U_URL_X | Set M3U URLs as environment variables. | N/A | Any valid M3U URLs |
| M3U_MAX_CONCURRENCY_1, M3U_MAX_CONCURRENCY_2, M3U_MAX_CONCURRENCY_X | Set max concurrency. | 1 | Any integer |
| MAX_RETRIES | Set max number of retries (loop) across all M3Us while streaming. 0 to never stop retrying (beware of throttling from provider). | 5 | Any integer greater than or equal 0 |
| RETRY_WAIT | Set a wait time before retrying (looping) across all M3Us on stream initialization error. | 0 | Any integer greater than or equal 0 |
| STREAM_TIMEOUT | Set timeout duration in seconds of retrying on error before a stream is considered down. | 3 | Any positive integer greater than 0 |
| REDIS_ADDR | Set Redis server address | N/A | e.g. localhost:6379 |
| REDIS_PASS | Set Redis server password | N/A | Any string |
Expand Down
18 changes: 16 additions & 2 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (instance *Parser) ParseURL(m3uURL string, m3uIndex int) error {
debug := os.Getenv("DEBUG") == "true"

maxRetries := 10
retryWait := 0

var err error
maxRetriesStr, maxRetriesExists := os.LookupEnv("MAX_RETRIES")
if maxRetriesExists {
Expand All @@ -118,19 +120,31 @@ func (instance *Parser) ParseURL(m3uURL string, m3uIndex int) error {
}
}

retryWaitStr, retryWaitExists := os.LookupEnv("RETRY_WAIT")
if retryWaitExists {
retryWait, err = strconv.Atoi(retryWaitStr)
if err != nil {
retryWait = 0
}
}

if debug {
utils.SafeLogf("[DEBUG] Max retries set to %d\n", maxRetries)
}

var buffer bytes.Buffer
for i := 0; i <= maxRetries; i++ {
if i > 0 && retryWait > 0 {
utils.SafeLogf("Retrying in %d secs...\n", retryWait)
time.Sleep(time.Duration(retryWait) * time.Second)
}

if debug {
utils.SafeLogf("[DEBUG] Attempt %d to download M3U\n", i+1)
}
err := downloadM3UToBuffer(m3uURL, &buffer)
if err != nil {
utils.SafeLogf("downloadM3UToBuffer error. Retrying in 5 secs... (error: %v)\n", err)
time.Sleep(5 * time.Second)
utils.SafeLogf("downloadM3UToBuffer error: %v\n", err)
continue
}

Expand Down

0 comments on commit f2ae129

Please sign in to comment.