Skip to content

Commit

Permalink
outbound connection retries
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Sep 30, 2023
1 parent e3fbd84 commit e471edd
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/send/tcp.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
package send

import (
"fmt"
"io"
"net"
"time"

"github.com/superstes/calamary/cnf"
"github.com/superstes/calamary/log"
"github.com/superstes/calamary/proc/parse"
"github.com/superstes/calamary/u"
)

func forwardTcp(pkt parse.ParsedPacket, conn net.Conn, connIo io.ReadWriter) {
var connFwd net.Conn
var err error
connFwd, err = net.DialTimeout("tcp", parse.PktDest(pkt), u.Timeout(cnf.C.Service.Timeout.Connect))
retries := 1
for {
connFwd, err = net.DialTimeout("tcp", parse.PktDest(pkt), u.Timeout(cnf.C.Service.Timeout.Connect))

if err != nil {
log.ConnError("send", parse.PktSrc(pkt), parse.PktDest(pkt), err)
return
if err == nil {
defer connFwd.Close()
break

} else {
if retries >= int(cnf.C.Service.Output.Retries) {
parse.LogConnError(
"send", pkt, fmt.Sprintf("Connect retry exceeded (%v/%v): %v",
cnf.C.Service.Output.Retries, cnf.C.Service.Output.Retries, err),
)
return
}
parse.LogConnDebug(
"send", pkt, fmt.Sprintf("Connection failed (retry %v/%v): %v",
cnf.C.Service.Output.Retries, cnf.C.Service.Output.Retries, err),
)
retries++
time.Sleep(u.Timeout(cnf.DefaultConnectRetryWait))
}
}
defer connFwd.Close()

close := make(chan bool, 1)
link := Link{src: connIo, close: close}
log.ConnDebug("send", parse.PktSrc(pkt), parse.PktDest(pkt), "Forwarding")
parse.LogConnDebug("send", pkt, "Forwarding")
go link.pipe(pkt, conn, connIo, connFwd)
go link.pipe(pkt, connFwd, connFwd, connIo)
<-close
log.ConnDebug("send", parse.PktSrc(pkt), parse.PktDest(pkt), "Closed")
parse.LogConnDebug("send", pkt, "Closed")
}

0 comments on commit e471edd

Please sign in to comment.