Skip to content

Commit 101dce9

Browse files
authored
Merge pull request #96 from HyperloopUPV-H8/backend/tcp-client-errors
[backend] TCP connections not reconnecting after link goes down
2 parents 9fdf7bd + 81e86fa commit 101dce9

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

backend/pkg/transport/network/tcp/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type ClientConfig struct {
5353
CurrentRetries int
5454
// Backoff specifies the backoff algorithm for this client
5555
Backoff backoffFunction
56+
// AbortOnError specifies whether the client should abort on errors or keep trying to connect
57+
AbortOnError bool
5658
}
5759

5860
// NewClient inits a ClientConfig with good defaults and the provided information
@@ -66,8 +68,9 @@ func NewClient(local net.Addr) ClientConfig {
6668

6769
Context: context.TODO(),
6870

69-
MaxRetries: -1,
70-
Backoff: NewExponBackoff(defaultBackoffMin, defaultBackoffExp, defaultBackoffMax),
71+
MaxRetries: -1,
72+
Backoff: NewExponBackoff(defaultBackoffMin, defaultBackoffExp, defaultBackoffMax),
73+
AbortOnError: false,
7174
}
7275
}
7376

backend/pkg/transport/transport.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net"
8+
"os"
89
"sync"
910
"time"
1011

@@ -44,7 +45,8 @@ func (transport *Transport) HandleClient(config tcp.ClientConfig, target abstrac
4445
for {
4546
conn, err := config.Dial(network, remote)
4647
if err != nil {
47-
if !errors.Is(err, error(tcp.ErrTooManyRetries{})) {
48+
if config.AbortOnError {
49+
fmt.Fprintf(os.Stderr, "Error connecting to %s: %v\n", target, err)
4850
return err
4951
}
5052

@@ -55,6 +57,14 @@ func (transport *Transport) HandleClient(config tcp.ClientConfig, target abstrac
5557
if errors.Is(err, error(ErrTargetAlreadyConnected{})) {
5658
return err
5759
}
60+
if err != nil {
61+
if config.AbortOnError {
62+
fmt.Fprintf(os.Stderr, "Error handling connection to %s: %v\n", target, err)
63+
return err
64+
}
65+
66+
continue
67+
}
5868

5969
// Wait before trying to reconnect
6070
config.CurrentRetries = 5
@@ -83,6 +93,12 @@ func (transport *Transport) handleTCPConn(target abstraction.TransportTarget, co
8393
return err
8494
}
8595

96+
if tcpConn, ok := conn.(*net.TCPConn); ok {
97+
err := tcpConn.SetLinger(0)
98+
if err != nil {
99+
fmt.Fprintf(os.Stderr, "Error setting %s linger to zero: %v\n", target, err)
100+
}
101+
}
86102
conn, errChan := tcp.WithErrChan(conn)
87103
defer conn.Close()
88104

0 commit comments

Comments
 (0)