diff --git a/client.go b/client.go index 462ff3c..965ce07 100644 --- a/client.go +++ b/client.go @@ -45,13 +45,14 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) // Wait for closer to exit to mitigate race condiion // between closer routine and this code path 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 { return nil, err } m.wg.Add(1) go m.closer() } + defer m.t.Reset(m.keepAliveDuration) const requestLength = 12 m.transactionCounter++ @@ -66,7 +67,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) req[7] = 3 //FunctionCode binary.BigEndian.PutUint16(req[8:10], first-1) 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) if err != nil { return nil, err @@ -74,7 +75,7 @@ func (m *Mbclient) ReadRegisters(first uint16, numRegs uint16) ([]uint16, error) if byteswritten != requestLength { 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[:]) if err != nil { return nil, err