Improve reconnection strategy

This commit is contained in:
Leonid Bugaev
2016-05-23 18:05:34 +05:00
parent a181b144d7
commit 0ccace8cad
2 changed files with 20 additions and 8 deletions
+4 -1
View File
@@ -62,7 +62,10 @@ run:
$(RUN) go run $(LDFLAGS) $(SOURCE) --input-dummy=0 --output-http="http://localhost:9000" --input-raw-track-response --input-raw 127.0.0.1:9000 --input-http 127.0.0.1:9000 --verbose --debug --middleware "./examples/middleware/echo.sh"
run-2:
sudo -E go run $(SOURCE) --input-raw :8000 --output-http "http://localhost:8001" --verbose --output-http-workers 1
sudo -E go run $(SOURCE) --input-dummy="" --output-tcp localhost:27001 --verbose --debug
run-3:
sudo -E go run $(SOURCE) --input-tcp :27001 --output-stdout
record:
$(RUN) go run $(SOURCE) --input-dummy=0 --output-file=requests.gor --verbose --debug
+16 -7
View File
@@ -38,9 +38,22 @@ func NewTCPOutput(address string) io.Writer {
}
func (o *TCPOutput) worker() {
retries := 1
conn, err := o.connect(o.address)
for ; err != nil; conn, err = o.connect(o.address) {
time.Sleep(2 * time.Second)
for {
if err == nil {
break
}
log.Println("Can't connect to aggregator instance, reconnecting in 1 second. Retries:", retries)
time.Sleep(1 * time.Second)
conn, err = o.connect(o.address)
retries++
}
if retries > 0 {
log.Println("Connected to aggregator instance after ", retries, " retries")
}
defer conn.Close()
@@ -50,7 +63,7 @@ func (o *TCPOutput) worker() {
_, err := conn.Write([]byte(payloadSeparator))
if err != nil {
log.Println("Worker failed on write, exitings and starting new worker:", err)
log.Println("Lost connection with aggregator instance, reconnecting")
go o.worker()
break
}
@@ -78,10 +91,6 @@ func (o *TCPOutput) Write(data []byte) (n int, err error) {
func (o *TCPOutput) connect(address string) (conn net.Conn, err error) {
conn, err = net.Dial("tcp", address)
if err != nil {
log.Println("Connection error ", err, o.address)
}
return
}