Skip to content

Commit

Permalink
Merge pull request #121 from sonroyaalmerol/fix-clear-concurrencies
Browse files Browse the repository at this point in the history
Fix clear concurrencies function
  • Loading branch information
sonroyaalmerol committed Aug 25, 2024
2 parents 1e506cb + f2627ad commit 7208da8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 22 additions & 2 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,28 @@ func (db *Instance) DecrementConcurrency(m3uIndex int) error {
}

func (db *Instance) ClearConcurrencies() error {
if err := db.Redis.Del(db.Ctx, "concurrency:*").Err(); err != nil {
return fmt.Errorf("error clear concurrencies from Redis: %v", err)
var cursor uint64
var err error
var keys []string

for {
var scanKeys []string
scanKeys, cursor, err = db.Redis.Scan(db.Ctx, cursor, "concurrency:*", 0).Result()
if err != nil {
return fmt.Errorf("error scanning keys from Redis: %v", err)
}
keys = append(keys, scanKeys...)
if cursor == 0 {
break
}
}

if len(keys) == 0 {
return nil
}

if err := db.Redis.Del(db.Ctx, keys...).Err(); err != nil {
return fmt.Errorf("error deleting keys from Redis: %v", err)
}

return nil
Expand Down
6 changes: 6 additions & 0 deletions stream_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ func proxyStream(ctx context.Context, m3uIndex int, resp *http.Response, r *http
func streamHandler(w http.ResponseWriter, r *http.Request, db *database.Instance) {
debug := os.Getenv("DEBUG") == "true"

if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte(fmt.Sprintf("HTTP method %q not allowed", r.Method)))
return
}

ctx, cancel := context.WithCancel(r.Context())
defer cancel()

Expand Down

0 comments on commit 7208da8

Please sign in to comment.