Pull request: 2473 update backoff

Merge in DNS/adguard-home from 2473-update-backoff to master

Updates #2473.

* commit 'cbdf80727f3cfb7f7908393c194cf8d5f0fb90c5':
  home: imp naming
  home: improve error handling
  Use a simple backoff for retrying the update
This commit is contained in:
Ainar Garipov 2020-12-25 13:38:26 +03:00
commit 1191a9acb7
1 changed files with 31 additions and 8 deletions

View File

@ -2,13 +2,14 @@ package home
import ( import (
"encoding/json" "encoding/json"
"errors"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"syscall" "syscall"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/sysutil" "github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/update" "github.com/AdguardTeam/AdGuardHome/internal/update"
@ -19,6 +20,13 @@ type getVersionJSONRequest struct {
RecheckNow bool `json:"recheck_now"` RecheckNow bool `json:"recheck_now"`
} }
// temporaryError is the interface for temporary errors from the Go standard
// library.
type temporaryError interface {
error
Temporary() (ok bool)
}
// Get the latest available version from the Internet // Get the latest available version from the Internet
func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
if Context.disableUpdate { if Context.disableUpdate {
@ -41,14 +49,29 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
var info update.VersionInfo var info update.VersionInfo
for i := 0; i != 3; i++ { for i := 0; i != 3; i++ {
Context.controlLock.Lock() func() {
info, err = Context.updater.GetVersionResponse(req.RecheckNow) Context.controlLock.Lock()
Context.controlLock.Unlock() defer Context.controlLock.Unlock()
if err != nil && strings.HasSuffix(err.Error(), "i/o timeout") {
// This case may happen while we're restarting DNS server info, err = Context.updater.GetVersionResponse(req.RecheckNow)
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/934 }()
continue
if err != nil {
var terr temporaryError
if errors.As(err, &terr) && terr.Temporary() {
// Temporary network error. This case may happen while
// we're restarting our DNS server. Log and sleep for
// some time.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/934.
d := time.Duration(i) * time.Second
log.Info("temp net error: %q; sleeping for %s and retrying", err, d)
time.Sleep(d)
continue
}
} }
break break
} }
if err != nil { if err != nil {