Skip to content

Commit

Permalink
Merge pull request #1 from bradchoate/master
Browse files Browse the repository at this point in the history
An update to prevent an infinite loop
  • Loading branch information
bradchoate committed Mar 31, 2016
2 parents 46b4255 + e84ddc2 commit 413e67e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/influxdb-relay
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
Expand Down

0 comments on commit 413e67e

Please sign in to comment.