cmd/strelaypoolsrv: Return better error codes and messages (#4770)

The current 500 "test failed" looks and sounds like a problem in the
relay pool server, while it actually indicates a problem on the
announcing side. Instead use 400 "connection test failed" to indicate
that the request was bad and what was the test.
This commit is contained in:
Jakob Borg 2018-02-21 12:53:49 +01:00 committed by GitHub
parent c9ec6159e8
commit 5e041dca9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -90,6 +90,10 @@ var (
evictionTimers = make(map[string]*time.Timer) evictionTimers = make(map[string]*time.Timer)
) )
const (
httpStatusEnhanceYourCalm = 429
)
func main() { func main() {
flag.StringVar(&listen, "listen", listen, "Listen address") flag.StringVar(&listen, "listen", listen, "Listen address")
flag.StringVar(&dir, "keys", dir, "Directory where http-cert.pem and http-key.pem is stored for TLS listening") flag.StringVar(&dir, "keys", dir, "Directory where http-cert.pem and http-key.pem is stored for TLS listening")
@ -344,7 +348,7 @@ func handlePostRequest(w http.ResponseWriter, r *http.Request) {
if debug { if debug {
log.Println("Asked to add a relay", newRelay, "which exists in permanent list") log.Println("Asked to add a relay", newRelay, "which exists in permanent list")
} }
http.Error(w, "Invalid request", 500) http.Error(w, "Invalid request", http.StatusBadRequest)
return return
} }
} }
@ -355,7 +359,7 @@ func handlePostRequest(w http.ResponseWriter, r *http.Request) {
case requests <- request{newRelay, uri, reschan}: case requests <- request{newRelay, uri, reschan}:
result := <-reschan result := <-reschan
if result.err != nil { if result.err != nil {
http.Error(w, result.err.Error(), 500) http.Error(w, result.err.Error(), http.StatusBadRequest)
return return
} }
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
@ -367,7 +371,7 @@ func handlePostRequest(w http.ResponseWriter, r *http.Request) {
if debug { if debug {
log.Println("Dropping request") log.Println("Dropping request")
} }
w.WriteHeader(429) w.WriteHeader(httpStatusEnhanceYourCalm)
} }
} }
@ -380,7 +384,7 @@ func requestProcessor() {
if debug { if debug {
log.Println("Test for relay", request.relay, "failed") log.Println("Test for relay", request.relay, "failed")
} }
request.result <- result{fmt.Errorf("test failed"), 0} request.result <- result{fmt.Errorf("connection test failed"), 0}
continue continue
} }