Cache preferred Piped instance to furde reduce start delay
Some checks failed
Run Integration Tests / test (push) Has been cancelled
Some checks failed
Run Integration Tests / test (push) Has been cancelled
This commit is contained in:
parent
8f31f0b2eb
commit
be973266c6
2 changed files with 120 additions and 16 deletions
|
@ -19,14 +19,13 @@ type MusicAPIResponse struct {
|
|||
|
||||
func SearchMusicViaPiped(query string, page int) ([]MusicResult, error) {
|
||||
var lastError error
|
||||
|
||||
// We will try to use preferred instance
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
for _, instance := range pipedInstances {
|
||||
if disabledInstances[instance] {
|
||||
continue
|
||||
}
|
||||
instance := preferredInstance
|
||||
mu.Unlock()
|
||||
|
||||
if instance != "" && !disabledInstances[instance] {
|
||||
url := fmt.Sprintf(
|
||||
"https://%s/search?q=%s&filter=music_songs&page=%d",
|
||||
instance,
|
||||
|
@ -34,22 +33,51 @@ func SearchMusicViaPiped(query string, page int) ([]MusicResult, error) {
|
|||
page,
|
||||
)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
defer resp.Body.Close()
|
||||
var apiResp MusicAPIResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err == nil {
|
||||
return convertPipedToMusicResults(instance, apiResp), nil
|
||||
}
|
||||
}
|
||||
|
||||
printWarn("Preferred instance %s failed for music, falling back", instance)
|
||||
disableInstance(instance)
|
||||
}
|
||||
|
||||
// 2. Fallback using others
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, inst := range pipedInstances {
|
||||
if disabledInstances[inst] {
|
||||
continue
|
||||
}
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"https://%s/search?q=%s&filter=music_songs&page=%d",
|
||||
inst,
|
||||
url.QueryEscape(query),
|
||||
page,
|
||||
)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
printInfo("Disabling instance %s due to error: %v", instance, err)
|
||||
disabledInstances[instance] = true
|
||||
lastError = fmt.Errorf("request to %s failed: %w", instance, err)
|
||||
printInfo("Disabling instance %s due to error: %v", inst, err)
|
||||
disabledInstances[inst] = true
|
||||
lastError = fmt.Errorf("request to %s failed: %w", inst, err)
|
||||
continue
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
var apiResp MusicAPIResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
|
||||
lastError = fmt.Errorf("failed to decode response from %s: %w", instance, err)
|
||||
lastError = fmt.Errorf("failed to decode response from %s: %w", inst, err)
|
||||
continue
|
||||
}
|
||||
|
||||
return convertPipedToMusicResults(instance, apiResp), nil
|
||||
preferredInstance = inst
|
||||
return convertPipedToMusicResults(inst, apiResp), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("all Piped instances failed, last error: %v", lastError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue