use persisted connections and handle died
Some checks failed
Build and Publish / BuildAndDeployAmd64 (push) Failing after 27s
Build and Publish / BuildAndDeploy (push) Successful in 2m16s

This commit is contained in:
matst80
2024-11-12 17:30:19 +01:00
parent 98d9b2451e
commit 170a51fad9
2 changed files with 54 additions and 26 deletions

View File

@@ -33,6 +33,8 @@ type TCPClient struct {
type PersistentConnection struct {
net.Conn
Died chan bool
Dead bool
address string
}
@@ -43,21 +45,29 @@ func NewPersistentConnection(address string) (*PersistentConnection, error) {
}
return &PersistentConnection{
Conn: connection,
Died: make(chan bool, 1),
Dead: false,
address: address,
}, nil
}
func (m *PersistentConnection) Connect() error {
connection, err := net.Dial("tcp", m.address)
if err != nil {
return err
if !m.Dead {
connection, err := net.Dial("tcp", m.address)
if err != nil {
m.Died <- true
m.Dead = true
return err
}
m.Conn = connection
}
m.Conn = connection
return nil
}
func (m *PersistentConnection) Close() {
m.Conn.Close()
m.Died <- true
m.Dead = true
}
func (m *PersistentConnection) HandleConnectionError(err error) error {