2016-06-02 05:16:02 -07:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors.
|
2015-09-07 01:21:23 -07:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2020-04-04 00:39:13 -07:00
|
|
|
"net/http"
|
2015-09-07 01:21:23 -07:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-04 00:39:13 -07:00
|
|
|
const (
|
|
|
|
httpStatusEnhanceYourCalm = 429
|
|
|
|
)
|
|
|
|
|
2016-08-22 23:43:27 -07:00
|
|
|
func poolHandler(pool string, uri *url.URL, mapping mapping) {
|
2015-09-21 14:33:29 -07:00
|
|
|
if debug {
|
|
|
|
log.Println("Joining", pool)
|
|
|
|
}
|
2015-09-07 01:21:23 -07:00
|
|
|
for {
|
2016-08-22 23:43:27 -07:00
|
|
|
uriCopy := *uri
|
|
|
|
uriCopy.Host = mapping.Address().String()
|
|
|
|
|
2015-09-07 01:21:23 -07:00
|
|
|
var b bytes.Buffer
|
|
|
|
json.NewEncoder(&b).Encode(struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}{
|
2016-08-22 23:43:27 -07:00
|
|
|
uriCopy.String(),
|
2015-09-07 01:21:23 -07:00
|
|
|
})
|
|
|
|
|
2018-02-21 07:56:04 -07:00
|
|
|
resp, err := httpClient.Post(pool, "application/json", &b)
|
2015-09-07 01:21:23 -07:00
|
|
|
if err != nil {
|
2020-04-04 00:39:13 -07:00
|
|
|
log.Printf("Error joining pool %v: HTTP request: %v", pool, err)
|
2015-09-07 01:21:23 -07:00
|
|
|
time.Sleep(time.Minute)
|
|
|
|
continue
|
2020-04-04 00:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2015-09-07 01:21:23 -07:00
|
|
|
var x struct {
|
|
|
|
EvictionIn time.Duration `json:"evictionIn"`
|
|
|
|
}
|
2020-06-10 02:26:09 -07:00
|
|
|
if err := json.Unmarshal(bs, &x); err == nil {
|
2015-09-07 01:21:23 -07:00
|
|
|
rejoin := x.EvictionIn - (x.EvictionIn / 5)
|
2020-04-04 00:39:13 -07:00
|
|
|
log.Printf("Joined pool %s, rejoining in %v", pool, rejoin)
|
2015-09-07 01:21:23 -07:00
|
|
|
time.Sleep(rejoin)
|
|
|
|
continue
|
2015-11-06 05:58:44 -07:00
|
|
|
} else {
|
2020-04-04 00:39:13 -07:00
|
|
|
log.Printf("Joined pool %s, failed to deserialize response: %v", pool, err)
|
2015-09-07 01:21:23 -07:00
|
|
|
}
|
2020-04-04 00:39:13 -07:00
|
|
|
|
|
|
|
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
|
2015-09-07 01:21:23 -07:00
|
|
|
}
|
2020-04-04 00:39:13 -07:00
|
|
|
|
2015-09-07 01:21:23 -07:00
|
|
|
time.Sleep(time.Hour)
|
|
|
|
}
|
|
|
|
}
|