chore: go fixes

This commit is contained in:
2026-05-02 18:58:13 +02:00
parent b7fee9d063
commit f0b5a4f9a8
8 changed files with 19 additions and 41 deletions

View File

@@ -304,13 +304,6 @@ func getMapKeys(m map[string]any) []string {
return keys return keys
} }
func min(a, b int) int {
if a < b {
return a
}
return b
}
func (c *allAnimeClient) extractSourceURLsFromData(ctx context.Context, data map[string]any) []StreamSource { func (c *allAnimeClient) extractSourceURLsFromData(ctx context.Context, data map[string]any) []StreamSource {
episodeData, ok := data["episode"].(map[string]any) episodeData, ok := data["episode"].(map[string]any)
if !ok { if !ok {

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"maps"
"net/http" "net/http"
"sort" "sort"
"strconv" "strconv"
@@ -214,9 +215,7 @@ func (h *Handler) HandleProxy(w http.ResponseWriter, r *http.Request) {
return return
} }
for k, v := range headers { maps.Copy(w.Header(), headers)
w.Header()[k] = v
}
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
if bodyReader != nil { if bodyReader != nil {

View File

@@ -31,7 +31,7 @@ func (e *providerExtractor) ExtractVideoLinks(ctx context.Context, providerPath
var resp *http.Response var resp *http.Response
var err error var err error
for attempt := 0; attempt < 3; attempt++ { for attempt := range 3 {
if attempt > 0 { if attempt > 0 {
select { select {
case <-ctx.Done(): case <-ctx.Done():

View File

@@ -48,12 +48,10 @@ func (s *Service) searchShowResultsByMode(ctx context.Context, query string, mod
var wg sync.WaitGroup var wg sync.WaitGroup
for _, mode := range modeCandidates { for _, mode := range modeCandidates {
modeValue := mode modeValue := mode
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
results, err := s.allAnimeClient.Search(ctx, query, modeValue) results, err := s.allAnimeClient.Search(ctx, query, modeValue)
searchCh <- searchModeResult{Mode: modeValue, Results: results, Err: err} searchCh <- searchModeResult{Mode: modeValue, Results: results, Err: err}
}() })
} }
wg.Wait() wg.Wait()

View File

@@ -316,10 +316,7 @@ func (c *Client) fetchWithRetry(ctx context.Context, urlStr string, out any) err
if retryable && attempt < maxRetries-1 { if retryable && attempt < maxRetries-1 {
resp.Body.Close() resp.Body.Close()
delay := retryDelay(attempt) delay := max(retryAfter, retryDelay(attempt))
if retryAfter > delay {
delay = retryAfter
}
if retryErr := waitForRetry(ctx, delay); retryErr != nil { if retryErr := waitForRetry(ctx, delay); retryErr != nil {
return retryErr return retryErr

View File

@@ -100,16 +100,16 @@ func (a Anime) ShortDuration() string {
return "" return ""
} }
// Duration format: "23 min per ep" or "1 hr 30 min" // Duration format: "23 min per ep" or "1 hr 30 min"
var num string var num strings.Builder
for _, c := range a.Duration { for _, c := range a.Duration {
if c >= '0' && c <= '9' { if c >= '0' && c <= '9' {
num += string(c) num.WriteString(string(c))
} else if c == ' ' && num != "" { } else if c == ' ' && num.String() != "" {
break break
} }
} }
if num != "" { if num.String() != "" {
return num + "m" return num.String() + "m"
} }
return a.Duration return a.Duration
} }

View File

@@ -65,10 +65,7 @@ func retryBackoff(attempts int64) string {
delay := time.Minute delay := time.Minute
if attempts > 1 { if attempts > 1 {
shift := attempts - 1 shift := min(attempts-1, 6)
if shift > 6 {
shift = 6
}
delay = time.Minute * time.Duration(1<<shift) delay = time.Minute * time.Duration(1<<shift)
} }
@@ -76,10 +73,7 @@ func retryBackoff(attempts int64) string {
delay = 30 * time.Minute delay = 30 * time.Minute
} }
minutes := int(delay / time.Minute) minutes := max(int(delay/time.Minute), 1)
if minutes < 1 {
minutes = 1
}
return fmt.Sprintf("+%d minutes", minutes) return fmt.Sprintf("+%d minutes", minutes)
} }

View File

@@ -7,6 +7,8 @@ import (
"io" "io"
"log" "log"
"path/filepath" "path/filepath"
"slices"
"strings"
"sync" "sync"
) )
@@ -45,19 +47,14 @@ func GetRenderer() *Renderer {
if len(genres) == 0 { if len(genres) == 0 {
return "" return ""
} }
s := "" var s strings.Builder
for _, g := range genres { for _, g := range genres {
s += "genres=" + fmt.Sprintf("%d", g) + "&" s.WriteString("genres=" + fmt.Sprintf("%d", g) + "&")
} }
return s[:len(s)-1] return s.String()[:len(s.String())-1]
}, },
"hasGenre": func(id int, genres []int) bool { "hasGenre": func(id int, genres []int) bool {
for _, g := range genres { return slices.Contains(genres, id)
if g == id {
return true
}
}
return false
}, },
"add": func(a, b int) int { "add": func(a, b int) int {
return a + b return a + b