From 6ed47a7b188fc497fe2194107f25101ec213a78a Mon Sep 17 00:00:00 2001 From: Michael Inthilith Date: Tue, 15 Nov 2016 18:14:00 +0100 Subject: [PATCH 1/4] Move ping response type to config Now you can decide what type of http status code the ping will return. --- relay/config.go | 3 +++ relay/http.go | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/relay/config.go b/relay/config.go index 18131d9..bae7106 100644 --- a/relay/config.go +++ b/relay/config.go @@ -24,6 +24,9 @@ type HTTPConfig struct { // Default retention policy to set for forwarded requests DefaultRetentionPolicy string `toml:"default-retention-policy"` + // Default ping response + DefaultPingResponse int `toml:"default-ping-response"` + // Outputs is a list of backed servers where writes will be forwarded Outputs []HTTPOutputConfig `toml:"output"` } diff --git a/relay/http.go b/relay/http.go index 73d4f23..d700c6d 100644 --- a/relay/http.go +++ b/relay/http.go @@ -21,9 +21,10 @@ import ( // HTTP is a relay for HTTP influxdb writes type HTTP struct { - addr string - name string - schema string + addr string + name string + schema string + response int cert string rp string @@ -35,6 +36,7 @@ type HTTP struct { } const ( + DefaultHttpPingResponse = http.StatusNoContent DefaultHTTPTimeout = 10 * time.Second DefaultMaxDelayInterval = 10 * time.Second DefaultBatchSizeKB = 512 @@ -48,6 +50,7 @@ func NewHTTP(cfg HTTPConfig) (Relay, error) { h.addr = cfg.Addr h.name = cfg.Name + h.response = cfg.DefaultPingResponse h.cert = cfg.SSLCombinedPem h.rp = cfg.DefaultRetentionPolicy @@ -113,9 +116,14 @@ func (h *HTTP) Stop() error { func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) { start := time.Now() + pingStatusResponse := DefaultHttpPingResponse + if (h.response != 0) { + pingStatusResponse = h.response + } + if r.URL.Path == "/ping" && (r.Method == "GET" || r.Method == "HEAD") { w.Header().Add("X-InfluxDB-Version", "relay") - w.WriteHeader(http.StatusNoContent) + w.WriteHeader(pingStatusResponse) return } From 6aa5bb0d6a9d943fd612ed3f09481dab6c2246fe Mon Sep 17 00:00:00 2001 From: Michael Inthilith Date: Tue, 15 Nov 2016 18:17:57 +0100 Subject: [PATCH 2/4] Fix indent --- relay/http.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/relay/http.go b/relay/http.go index d700c6d..cc3238a 100644 --- a/relay/http.go +++ b/relay/http.go @@ -122,9 +122,9 @@ func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if r.URL.Path == "/ping" && (r.Method == "GET" || r.Method == "HEAD") { - w.Header().Add("X-InfluxDB-Version", "relay") - w.WriteHeader(pingStatusResponse) - return + w.Header().Add("X-InfluxDB-Version", "relay") + w.WriteHeader(pingStatusResponse) + return } if r.URL.Path != "/write" { From 402ce3e1e4c6c9dac88b90c07ee312474969a7a1 Mon Sep 17 00:00:00 2001 From: Michael Inthilith Date: Tue, 15 Nov 2016 18:21:54 +0100 Subject: [PATCH 3/4] Add docs & example --- README.md | 3 +++ sample.toml | 1 + 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index c6cfb64..37c9704 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ name = "example-http" # TCP address to bind to, for HTTP server. bind-addr = "127.0.0.1:9096" +# Ping response code, default is 204 +default-ping-response = 200 + # Enable HTTPS requests. ssl-combined-pem = "/etc/ssl/influxdb-relay.pem" diff --git a/sample.toml b/sample.toml index 5152857..4518a33 100644 --- a/sample.toml +++ b/sample.toml @@ -3,6 +3,7 @@ [[http]] name = "example-http" bind-addr = "127.0.0.1:9096" +default-ping-response = 204 output = [ { name="local1", location = "http://127.0.0.1:8086/write" }, { name="local2", location = "http://127.0.0.1:7086/write" }, From 3409c36f0fc0dd78e93eec2fec3203934cb6d3fb Mon Sep 17 00:00:00 2001 From: Michael Inthilith Date: Wed, 16 Nov 2016 10:06:47 +0100 Subject: [PATCH 4/4] Added content-length header & reword a bit --- relay/http.go | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/relay/http.go b/relay/http.go index cc3238a..8ef9145 100644 --- a/relay/http.go +++ b/relay/http.go @@ -21,18 +21,20 @@ import ( // HTTP is a relay for HTTP influxdb writes type HTTP struct { - addr string - name string - schema string - response int + addr string + name string + schema string - cert string - rp string + pingResponseCode int + pingResponseHeaders map[string]string - closing int64 - l net.Listener + cert string + rp string - backends []*httpBackend + closing int64 + l net.Listener + + backends []*httpBackend } const ( @@ -50,7 +52,16 @@ func NewHTTP(cfg HTTPConfig) (Relay, error) { h.addr = cfg.Addr h.name = cfg.Name - h.response = cfg.DefaultPingResponse + + h.pingResponseCode = DefaultHttpPingResponse + if (cfg.DefaultPingResponse != 0) { + h.pingResponseCode = cfg.DefaultPingResponse + } + + h.pingResponseHeaders["X-InfluxDB-Version"] = "relay" + if (h.pingResponseCode != http.StatusNoContent) { + h.pingResponseHeaders["Content-Length"] = "0" + } h.cert = cfg.SSLCombinedPem h.rp = cfg.DefaultRetentionPolicy @@ -116,14 +127,11 @@ func (h *HTTP) Stop() error { func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) { start := time.Now() - pingStatusResponse := DefaultHttpPingResponse - if (h.response != 0) { - pingStatusResponse = h.response - } - if r.URL.Path == "/ping" && (r.Method == "GET" || r.Method == "HEAD") { - w.Header().Add("X-InfluxDB-Version", "relay") - w.WriteHeader(pingStatusResponse) + for key, value := range h.pingResponseHeaders { + w.Header().Add(key, value) + } + w.WriteHeader(h.pingResponseCode) return }