Add retry on IP failures

This commit is contained in:
Mark Beamer Jr 2020-07-30 12:14:06 -04:00 committed by Alex Grintsvayg
parent ad27425471
commit b1e10e7b09
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
2 changed files with 79 additions and 65 deletions

View file

@ -13,7 +13,7 @@ LDFLAGS = -ldflags "-X ${IMPORT_PATH}/meta.Version=${VERSION} -X ${IMPORT_PATH}/
build:
mkdir -p ${BIN_DIR} && CGO_ENABLED=0 go build ${LDFLAGS} -asmflags -trimpath=${DIR} -o ${BIN_DIR}/${BINARY} main.go
mkdir -p ${BIN_DIR} && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -asmflags -trimpath=${DIR} -o ${BIN_DIR}/${BINARY} main.go
clean:
if [ -f ${BIN_DIR}/${BINARY} ]; then rm ${BIN_DIR}/${BINARY}; fi

View file

@ -232,6 +232,9 @@ func getClient(ip *net.TCPAddr) *http.Client {
}
func run(use string, args []string, withStdErr, withStdOut bool, stopChan stop.Chan, v ytapi.VideoParams) ([]string, error) {
var maxtries = 10
var attemps int
for {
var sourceAddress string
var err error
for {
@ -290,6 +293,7 @@ func run(use string, args []string, withStdErr, withStdOut bool, stopChan stop.C
done := make(chan error, 1)
go func() {
attemps++
done <- cmd.Wait()
}()
select {
@ -300,12 +304,22 @@ func run(use string, args []string, withStdErr, withStdOut bool, stopChan stop.C
return nil, errors.Err("canceled by stopper")
case err := <-done:
if err != nil {
if strings.Contains(err.Error(), "exit status 1") {
if strings.Contains(string(errorLog), "HTTP Error 429") || strings.Contains(string(errorLog), "returned non-zero exit status 8") {
v.IPPool.SetThrottled(sourceAddress)
}
if attemps > maxtries {
break
}
continue
}
return nil, errors.Prefix("youtube-dl "+strings.Join(args, " "), err)
}
return strings.Split(strings.Replace(string(outLog), "\r\n", "\n", -1), "\n"), nil
}
if len(errorLog) > 0 {
return nil, errors.Err(string(errorLog))
}
return strings.Split(strings.Replace(string(outLog), "\r\n", "\n", -1), "\n"), nil
}
}