Fix lockup on io timeout

When the client got a IO timeout the timer were never reset
Adjusting timeouts
This commit is contained in:
2023-02-24 22:59:39 +01:00
parent a5f1936632
commit 9a92e47837

View File

@@ -45,13 +45,14 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
// Wait for closer to exit to mitigate race condiion // Wait for closer to exit to mitigate race condiion
// between closer routine and this code path // between closer routine and this code path
m.wg.Wait() m.wg.Wait()
m.conn, err = net.Dial("tcp", m.address) m.conn, err = net.DialTimeout("tcp", m.address, 5*time.Second)
if err != nil { if err != nil {
return nil, err return nil, err
} }
m.wg.Add(1) m.wg.Add(1)
go m.closer() go m.closer()
} }
defer m.t.Reset(m.keepAliveDuration)
const requestLength = 12 const requestLength = 12
m.transactionCounter++ m.transactionCounter++
@@ -66,7 +67,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
req[7] = 3 //FunctionCode req[7] = 3 //FunctionCode
binary.BigEndian.PutUint16(req[8:10], first-1) binary.BigEndian.PutUint16(req[8:10], first-1)
binary.BigEndian.PutUint16(req[10:12], numRegs) binary.BigEndian.PutUint16(req[10:12], numRegs)
m.conn.SetDeadline(time.Now().Add(10 * time.Second)) m.conn.SetDeadline(time.Now().Add(5 * time.Second))
byteswritten, err := m.conn.Write(req) byteswritten, err := m.conn.Write(req)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -74,7 +75,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error)
if byteswritten != requestLength { if byteswritten != requestLength {
return nil, fmt.Errorf("failed to send request") return nil, fmt.Errorf("failed to send request")
} }
m.conn.SetDeadline(time.Now().Add(10 * time.Second)) m.conn.SetDeadline(time.Now().Add(5 * time.Second))
_, err = io.ReadFull(m.conn, m.header[:]) _, err = io.ReadFull(m.conn, m.header[:])
if err != nil { if err != nil {
return nil, err return nil, err