From 872bcaab06cdedbd94a6b279878cbb85bc60291b Mon Sep 17 00:00:00 2001 From: Matthias D Date: Tue, 5 Sep 2023 20:43:30 +0200 Subject: [PATCH] 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 --- connection.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 7f8d375..f706aa6 100644 --- a/connection.go +++ b/connection.go @@ -24,7 +24,9 @@ var connectionCache map[string]*Connection = make(map[string]*Connection) // Creates a new connection to a RCT device at the given address func NewConnection(host string, cache time.Duration) (*Connection, error) { if conn, ok := connectionCache[host]; ok { - return conn, nil + if conn.conn != nil { // there might be dead connection in the cache, e.g. when connection was disconnected + return conn, nil + } } conn := &Connection{ @@ -53,6 +55,7 @@ func (c *Connection) connect() (err error) { func (c *Connection) Close() { c.conn.Close() c.conn = nil + delete(connectionCache, c.host) // connection is dead, no need to cache any more } // Sends the given RCT datagram via the connection