Skip to content

Commit 81e86fa

Browse files
committed
Add flag to conditionally return on connection errors
1 parent ceb0b88 commit 81e86fa

2 files changed

Lines changed: 15 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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func (transport *Transport) HandleClient(config tcp.ClientConfig, target abstrac
4545
for {
4646
conn, err := config.Dial(network, remote)
4747
if err != nil {
48-
if !errors.Is(err, error(tcp.ErrTooManyRetries{})) {
48+
if config.AbortOnError {
49+
fmt.Fprintf(os.Stderr, "Error connecting to %s: %v\n", target, err)
4950
return err
5051
}
5152

@@ -56,6 +57,14 @@ func (transport *Transport) HandleClient(config tcp.ClientConfig, target abstrac
5657
if errors.Is(err, error(ErrTargetAlreadyConnected{})) {
5758
return err
5859
}
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+
}
5968

6069
// Wait before trying to reconnect
6170
config.CurrentRetries = 5

0 commit comments

Comments
 (0)