Skip to content

Commit ab5977f

Browse files
committed
feat: add CDN URL quarantine
See #188
1 parent c980afb commit ab5977f

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

player/player.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package player
33
import (
44
"context"
55
"fmt"
6+
"net/http"
7+
"net/url"
8+
"time"
9+
610
librespot "github.com/devgianlu/go-librespot"
711
"github.com/devgianlu/go-librespot/audio"
812
"github.com/devgianlu/go-librespot/output"
@@ -11,16 +15,19 @@ import (
1115
"github.com/devgianlu/go-librespot/spclient"
1216
"github.com/devgianlu/go-librespot/vorbis"
1317
"golang.org/x/exp/rand"
14-
"net/http"
15-
"net/url"
16-
"time"
1718
)
1819

19-
const SampleRate = 44100
20-
const Channels = 2
20+
const (
21+
SampleRate = 44100
22+
Channels = 2
23+
)
2124

2225
const MaxStateVolume = 65535
2326

27+
const DisableCheckMediaRestricted = true
28+
29+
const CdnUrlQuarantineDuration = 15 * time.Minute
30+
2431
type Player struct {
2532
log librespot.Logger
2633

@@ -33,6 +40,8 @@ type Player struct {
3340
audioKey *audio.KeyProvider
3441
events EventManager
3542

43+
cdnQuarantine map[string]time.Time
44+
3645
newOutput func(source librespot.Float32Reader, volume float32) (output.Output, error)
3746

3847
cmd chan playerCmd
@@ -141,6 +150,7 @@ func NewPlayer(opts *Options) (*Player, error) {
141150
sp: opts.Spclient,
142151
audioKey: opts.AudioKey,
143152
events: opts.Events,
153+
cdnQuarantine: make(map[string]time.Time),
144154
normalisationEnabled: opts.NormalisationEnabled,
145155
normalisationUseAlbumGain: opts.NormalisationUseAlbumGain,
146156
normalisationPregain: opts.NormalisationPregain,
@@ -414,8 +424,6 @@ func (p *Player) SetSecondaryStream(source librespot.AudioSource) {
414424
<-resp
415425
}
416426

417-
const DisableCheckMediaRestricted = true
418-
419427
func (p *Player) httpChunkedReaderFromStorageResolve(log librespot.Logger, client *http.Client, storageResolve *downloadpb.StorageResolveResponse) (*audio.HttpChunkedReader, error) {
420428
if storageResolve.Result == downloadpb.StorageResolveResponse_STORAGE {
421429
return nil, fmt.Errorf("old storage not supported")
@@ -437,13 +445,24 @@ func (p *Player) httpChunkedReaderFromStorageResolve(log librespot.Logger, clien
437445
continue
438446
}
439447

448+
if lastFailed, found := p.cdnQuarantine[cdnUrl.Host]; found {
449+
if i == len(storageResolve.Cdnurl)-1 {
450+
log.WithField("host", cdnUrl.Host).Warnf("cannot skip cdn url because it is the last one")
451+
} else if time.Since(lastFailed) < CdnUrlQuarantineDuration {
452+
log.WithField("host", cdnUrl.Host).Infof("skipping cdn url because it has failed recently")
453+
continue
454+
}
455+
}
456+
440457
var rawStream *audio.HttpChunkedReader
441458
rawStream, err = audio.NewHttpChunkedReader(log, client, cdnUrl.String())
442459
if err != nil {
443-
log.WithError(err).WithField("url", cdnUrl.String()).Warnf("failed creating chunked reader for %s, trying next url", cdnUrl.Host)
460+
log.WithError(err).WithField("host", cdnUrl.Host).Warnf("failed creating chunked reader, trying next url")
461+
p.cdnQuarantine[cdnUrl.Host] = time.Now()
444462
continue
445463
}
446464

465+
delete(p.cdnQuarantine, cdnUrl.Host)
447466
return rawStream, nil
448467
}
449468

0 commit comments

Comments
 (0)