fix: resolve allanime timeout issues

This commit is contained in:
2026-05-02 13:50:28 +02:00
committed by Mikkel Elvers
parent 2e78713803
commit 33b450a686
3 changed files with 29 additions and 13 deletions

View File

@@ -98,7 +98,7 @@ func (rt *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
var allAnimeUTLSClient = &http.Client{
Transport: &utlsRoundTripper{},
Timeout: 15 * time.Second,
Timeout: 30 * time.Second,
}
type searchResult struct {
@@ -115,7 +115,7 @@ type allAnimeClient struct {
func newAllAnimeClient() *allAnimeClient {
return &allAnimeClient{
httpClient: &http.Client{
Timeout: 12 * time.Second,
Timeout: 30 * time.Second,
},
extractor: newProviderExtractor(),
}
@@ -305,7 +305,7 @@ func min(a, b int) int {
return b
}
func (c *allAnimeClient) extractSourceURLsFromData(data map[string]any) []StreamSource {
func (c *allAnimeClient) extractSourceURLsFromData(ctx context.Context, data map[string]any) []StreamSource {
episodeData, ok := data["episode"].(map[string]any)
if !ok {
return nil
@@ -357,9 +357,6 @@ func (c *allAnimeClient) extractSourceURLsFromData(data map[string]any) []Stream
decoded = "/" + decoded
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
extracted, err := c.extractor.ExtractVideoLinks(ctx, decoded)
if err != nil {
log.Printf("source extraction failed for %s: %v", decoded, err)
@@ -510,7 +507,7 @@ func (c *allAnimeClient) GetEpisodeSources(ctx context.Context, showID string, e
result, err := c.graphqlRequestWithHash(ctx, showID, episode, mode)
if err == nil {
// Result is already in shape {"episode": {"sourceUrls": [...]}}
sources := c.extractSourceURLsFromData(result)
sources := c.extractSourceURLsFromData(ctx, result)
if len(sources) > 0 {
return sources, nil
}

View File

@@ -19,18 +19,37 @@ type providerExtractor struct {
func newProviderExtractor() *providerExtractor {
return &providerExtractor{
httpClient: &http.Client{Timeout: 12 * time.Second},
baseURL: "https://allanime.day",
httpClient: &http.Client{Timeout: 30 * time.Second},
baseURL: allAnimeBaseURL,
referer: allAnimeReferer,
}
}
func (e *providerExtractor) ExtractVideoLinks(ctx context.Context, providerPath string) ([]StreamSource, error) {
endpoint := e.baseURL + providerPath
resp, err := doProxiedRequest(ctx, e.httpClient, endpoint, e.referer)
if err != nil {
return nil, fmt.Errorf("fetch provider response: %w", err)
var resp *http.Response
var err error
for attempt := 0; attempt < 3; attempt++ {
if attempt > 0 {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(time.Duration(attempt) * 2 * time.Second):
}
}
resp, err = doProxiedRequest(ctx, e.httpClient, endpoint, e.referer)
if err == nil {
break
}
if attempt == 2 {
return nil, fmt.Errorf("fetch provider response: %w", err)
}
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))