use persisted connections and handle died
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user