mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-12 16:26:37 +00:00
cmd/strelaysrv: Harmonize and improve log output (ref #6492)
This commit is contained in:
parent
d1db7e3dd2
commit
b7b9476e5a
@ -7,10 +7,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
httpStatusEnhanceYourCalm = 429
|
||||||
|
)
|
||||||
|
|
||||||
func poolHandler(pool string, uri *url.URL, mapping mapping) {
|
func poolHandler(pool string, uri *url.URL, mapping mapping) {
|
||||||
if debug {
|
if debug {
|
||||||
log.Println("Joining", pool)
|
log.Println("Joining", pool)
|
||||||
@ -28,38 +33,62 @@ func poolHandler(pool string, uri *url.URL, mapping mapping) {
|
|||||||
|
|
||||||
resp, err := httpClient.Post(pool, "application/json", &b)
|
resp, err := httpClient.Post(pool, "application/json", &b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error joining pool", pool, err)
|
log.Printf("Error joining pool %v: HTTP request: %v", pool, err)
|
||||||
} else if resp.StatusCode == 500 {
|
|
||||||
bs, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to join", pool, "due to an internal server error. Could not read response:", err)
|
|
||||||
} else {
|
|
||||||
log.Println("Failed to join", pool, "due to an internal server error:", string(bs))
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
|
||||||
} else if resp.StatusCode == 429 {
|
|
||||||
log.Println(pool, "under load, will retry in a minute")
|
|
||||||
time.Sleep(time.Minute)
|
time.Sleep(time.Minute)
|
||||||
continue
|
continue
|
||||||
} else if resp.StatusCode == 401 {
|
}
|
||||||
log.Println(pool, "failed to join due to IP address not matching external address. Aborting")
|
|
||||||
return
|
bs, err := ioutil.ReadAll(resp.Body)
|
||||||
} else if resp.StatusCode == 200 {
|
resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error joining pool %v: reading response: %v", pool, err)
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch resp.StatusCode {
|
||||||
|
case http.StatusOK:
|
||||||
var x struct {
|
var x struct {
|
||||||
EvictionIn time.Duration `json:"evictionIn"`
|
EvictionIn time.Duration `json:"evictionIn"`
|
||||||
}
|
}
|
||||||
err := json.NewDecoder(resp.Body).Decode(&x)
|
if err := json.NewDecoder(resp.Body).Decode(&x); err == nil {
|
||||||
if err == nil {
|
|
||||||
rejoin := x.EvictionIn - (x.EvictionIn / 5)
|
rejoin := x.EvictionIn - (x.EvictionIn / 5)
|
||||||
log.Println("Joined", pool, "rejoining in", rejoin)
|
log.Printf("Joined pool %s, rejoining in %v", pool, rejoin)
|
||||||
time.Sleep(rejoin)
|
time.Sleep(rejoin)
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
log.Println("Failed to deserialize response", err)
|
log.Printf("Joined pool %s, failed to deserialize response: %v", pool, err)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
log.Println(pool, "unknown response type from server", resp.StatusCode)
|
case http.StatusInternalServerError:
|
||||||
|
log.Printf("Failed to join %v: server error", pool)
|
||||||
|
log.Printf("Response data: %s", bs)
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
continue
|
||||||
|
|
||||||
|
case http.StatusBadRequest:
|
||||||
|
log.Printf("Failed to join %v: request or check error", pool)
|
||||||
|
log.Printf("Response data: %s", bs)
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
continue
|
||||||
|
|
||||||
|
case httpStatusEnhanceYourCalm:
|
||||||
|
log.Printf("Failed to join %v: under load (rate limiting)", pool)
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
continue
|
||||||
|
|
||||||
|
case http.StatusUnauthorized:
|
||||||
|
log.Printf("Failed to join %v: IP address not matching external address", pool)
|
||||||
|
log.Println("Aborting")
|
||||||
|
return
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Printf("Failed to join %v: unexpected status code from server: %d", pool, resp.StatusCode)
|
||||||
|
log.Printf("Response data: %s", bs)
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Hour)
|
time.Sleep(time.Hour)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user