-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnection.go
126 lines (101 loc) · 2.62 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ibapi
import (
"fmt"
"net"
"time"
)
const (
maxReconnectAttempts = 3
reconnectDelay = 500 * time.Millisecond
)
// Connection is a TCPConn wrapper.
type Connection struct {
*net.TCPConn
host string
port int
isConnected bool
numBytesSent int
numMsgSent int
numBytesRecv int
numMsgRecv int
}
func (c *Connection) Write(bs []byte) (int, error) {
n, err := c.TCPConn.Write(bs)
if err != nil {
log.Warn().Err(err).Msg("Write error detected, attempting to reconnect...")
if err := c.reconnect(); err != nil {
return 0, fmt.Errorf("write failed and reconnection failed: %w", err)
}
// Retry write after successful reconnection
return c.TCPConn.Write(bs)
}
c.numBytesSent += n
c.numMsgSent++
log.Trace().Int("nBytes", n).Msg("conn write")
return n, err
}
func (c *Connection) Read(bs []byte) (int, error) {
n, err := c.TCPConn.Read(bs)
c.numBytesRecv += n
c.numMsgRecv++
log.Trace().Int("nBytes", n).Msg("conn read")
return n, err
}
func (c *Connection) reset() {
c.numBytesSent = 0
c.numBytesRecv = 0
c.numMsgSent = 0
c.numMsgRecv = 0
}
func (c *Connection) connect(host string, port int) error {
c.host = host
c.port = port
c.reset()
address := fmt.Sprintf("%v:%v", c.host, c.port)
addr, err := net.ResolveTCPAddr("tcp4", address)
if err != nil {
log.Error().Err(err).Str("host", address).Msg("failed to resove tcp address")
return err
}
c.TCPConn, err = net.DialTCP("tcp4", nil, addr)
if err != nil {
log.Error().Err(err).Any("address", addr).Msg("failed to dial tcp")
return err
}
log.Debug().Any("address", c.TCPConn.RemoteAddr()).Msg("tcp socket connected")
c.isConnected = true
return nil
}
func (c *Connection) reconnect() error {
attempts := 0
backoff := reconnectDelay // Start with base delay
for attempts < maxReconnectAttempts {
log.Info().
Int("attempt", attempts+1).
Int("maxAttempts", maxReconnectAttempts).
Msg("Attempting to reconnect")
err := c.connect(c.host, c.port)
if err == nil {
log.Info().Msg("Reconnection successful")
return nil
}
attempts++
if attempts == maxReconnectAttempts {
return fmt.Errorf("failed to reconnect after %d attempts", attempts)
}
time.Sleep(backoff)
backoff *= 2 // Exponential backoff
}
return fmt.Errorf("failed to reconnect after %d attempts", attempts)
}
func (c *Connection) disconnect() error {
log.Trace().
Int("nMsgSent", c.numMsgSent).Int("nBytesSent", c.numBytesSent).
Int("nMsgRecv", c.numMsgRecv).Int("nBytesRecv", c.numBytesRecv).
Msg("conn disconnect")
c.isConnected = false
return c.Close()
}
func (c *Connection) IsConnected() bool {
return c.isConnected
}