From 8a77d88fab2c1868cae844c0a434d5df8c5ab2c4 Mon Sep 17 00:00:00 2001 From: Brad Choate Date: Tue, 29 Mar 2016 13:34:52 -0700 Subject: [PATCH 1/2] Prevent infinite retry loop. --- README.md | 1 + main.go | 9 ++++++--- writer.go | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9a501ad..4e4dde4 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Usage of influxdb-relay: -listen-addr="127.0.0.1:4444": Local address for the UDP listener -max-line-length=256: Maximum line length for line protocol, in bytes -target-url="http://127.0.0.1:8086/write?db=example": URL where recieved data should be written + -attempt-limit=10: Maximum number of times to retry failed influxdb requests (0=infinite) ``` License diff --git a/main.go b/main.go index 7bc94a1..64afa04 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,9 @@ var targetURL = flag.String( var listenAddrStr = flag.String( "listen-addr", "127.0.0.1:4444", "Local address for the UDP listener", ) +var attemptLimit = flag.Int( + "attempt-limit", 10, "Maximum number of times to retry failed influxdb requests (0=infinite)", +) func main() { flag.Parse() @@ -62,9 +65,9 @@ func main() { SendBufferQueue: sendBufferQueue, } writer := &Writer{ - SendBufferQueue: sendBufferQueue, - TargetURL: *targetURL, - RecvBufferQueue: recvBufferQueue, + SendBufferQueue: sendBufferQueue, + TargetURL: *targetURL, + RecvBufferQueue: recvBufferQueue, } go reader.Run() diff --git a/writer.go b/writer.go index ce1d83c..24d3f99 100644 --- a/writer.go +++ b/writer.go @@ -29,7 +29,17 @@ func (w *Writer) Run() { msg := <-w.SendBufferQueue // We'll keep trying to send this message until we succeed + // or get tired of trying + tries := 0 for { + tries += 1 + + // we tried, really hard, but let's be serious... + if tries == *attemptLimit { + log.Println("gave up writing to backend after", tries, "attempts") + break + } + contentReader := bytes.NewReader(msg.Content) resp, err := client.Post( @@ -40,10 +50,17 @@ func (w *Writer) Run() { time.Sleep(2 * time.Second) continue } + // InfluxDB docs say that only 204 exactly is truly successful, // and in fact 200 OK is not successful. Strange, but okay... if resp.StatusCode != 204 { log.Println("backend write returned", resp.Status) + if resp.StatusCode == 400 { + // invalid request; no amount of retries will + // reform malformed data + break + } + time.Sleep(2 * time.Second) continue } From e84ddc24a8ea00ba9f56574a485f8ea1e1b5f7bc Mon Sep 17 00:00:00 2001 From: Brad Choate Date: Tue, 29 Mar 2016 13:35:49 -0700 Subject: [PATCH 2/2] Ignore the compiled binary. --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01c8b5b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/influxdb-relay