Skip to content

Commit 872bcaa

Browse files
committed
First improvements forfixing crashes in internal test programs. Clearing the cache when Closing the connection as convenience, however seems that connection is nil also sometimes by itself (connection dropped / network disconnect)? --> added another sanity check when checking cache during connection attempt.
This seems to definitely improve reliability, let's see in normal program if this fixes the crashes every ~3 to 25 days completely
1 parent 03eacb9 commit 872bcaa

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

connection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ var connectionCache map[string]*Connection = make(map[string]*Connection)
2424
// Creates a new connection to a RCT device at the given address
2525
func NewConnection(host string, cache time.Duration) (*Connection, error) {
2626
if conn, ok := connectionCache[host]; ok {
27-
return conn, nil
27+
if conn.conn != nil { // there might be dead connection in the cache, e.g. when connection was disconnected
28+
return conn, nil
29+
}
2830
}
2931

3032
conn := &Connection{
@@ -53,6 +55,7 @@ func (c *Connection) connect() (err error) {
5355
func (c *Connection) Close() {
5456
c.conn.Close()
5557
c.conn = nil
58+
delete(connectionCache, c.host) // connection is dead, no need to cache any more
5659
}
5760

5861
// Sends the given RCT datagram via the connection

0 commit comments

Comments
 (0)