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
}
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 {
episodeData, ok := data["episode"].(map[string]any)
if !ok {

View File

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

View File

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

View File

@@ -48,12 +48,10 @@ func (s *Service) searchShowResultsByMode(ctx context.Context, query string, mod
var wg sync.WaitGroup
for _, mode := range modeCandidates {
modeValue := mode
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
results, err := s.allAnimeClient.Search(ctx, query, modeValue)
searchCh <- searchModeResult{Mode: modeValue, Results: results, Err: err}
}()
})
}
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 {
resp.Body.Close()
delay := retryDelay(attempt)
if retryAfter > delay {
delay = retryAfter
}
delay := max(retryAfter, retryDelay(attempt))
if retryErr := waitForRetry(ctx, delay); retryErr != nil {
return retryErr

View File

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

View File

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

View File

@@ -7,6 +7,8 @@ import (
"io"
"log"
"path/filepath"
"slices"
"strings"
"sync"
)
@@ -45,19 +47,14 @@ func GetRenderer() *Renderer {
if len(genres) == 0 {
return ""
}
s := ""
var s strings.Builder
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 {
for _, g := range genres {
if g == id {
return true
}
}
return false
return slices.Contains(genres, id)
},
"add": func(a, b int) int {
return a + b