mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-17 19:08:46 -07:00
95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
// Copyright (C) 2015 Audrius Butkevicius and Contributors.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
httpStatusEnhanceYourCalm = 429
|
|
)
|
|
|
|
func poolHandler(pool string, uri *url.URL, mapping mapping) {
|
|
if debug {
|
|
log.Println("Joining", pool)
|
|
}
|
|
for {
|
|
uriCopy := *uri
|
|
uriCopy.Host = mapping.Address().String()
|
|
|
|
var b bytes.Buffer
|
|
json.NewEncoder(&b).Encode(struct {
|
|
URL string `json:"url"`
|
|
}{
|
|
uriCopy.String(),
|
|
})
|
|
|
|
resp, err := httpClient.Post(pool, "application/json", &b)
|
|
if err != nil {
|
|
log.Printf("Error joining pool %v: HTTP request: %v", pool, err)
|
|
time.Sleep(time.Minute)
|
|
continue
|
|
}
|
|
|
|
bs, err := ioutil.ReadAll(resp.Body)
|
|
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 {
|
|
EvictionIn time.Duration `json:"evictionIn"`
|
|
}
|
|
if err := json.Unmarshal(bs, &x); err == nil {
|
|
rejoin := x.EvictionIn - (x.EvictionIn / 5)
|
|
log.Printf("Joined pool %s, rejoining in %v", pool, rejoin)
|
|
time.Sleep(rejoin)
|
|
continue
|
|
} else {
|
|
log.Printf("Joined pool %s, failed to deserialize response: %v", pool, err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|