From 4603af9c2ca0561350c61dd4948c265d102cbcfe Mon Sep 17 00:00:00 2001 From: Wolfgang Welz <welzwo@gmail.com> Date: Thu, 18 Jun 2020 13:39:05 +0200 Subject: [PATCH] assure conn is not nil --- plugins/analysis/client/plugin.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/plugins/analysis/client/plugin.go b/plugins/analysis/client/plugin.go index 736e79ac..24b2e1eb 100644 --- a/plugins/analysis/client/plugin.go +++ b/plugins/analysis/client/plugin.go @@ -1,6 +1,7 @@ package client import ( + "errors" "net" "sync" "time" @@ -96,28 +97,43 @@ func (c *connector) Stop() { } func (c *connector) new() { + c.mu.Lock() + defer c.mu.Unlock() + select { case _ = <-c.closing: return default: - c.mu.Lock() - defer c.mu.Unlock() - - conn, err := net.Dial("tcp", config.Node.GetString(CfgServerAddress)) + c.conn = nil + tcpConn, err := net.Dial("tcp", config.Node.GetString(CfgServerAddress)) if err != nil { time.AfterFunc(1*time.Minute, c.new) log.Warn(err) return } - c.conn = network.NewManagedConnection(conn) + c.conn = network.NewManagedConnection(tcpConn) c.conn.Events.Close.Attach(events.NewClosure(c.new)) } } +func (c *connector) Close() (err error) { + c.mu.Lock() + defer c.mu.Unlock() + + if c.conn != nil { + err = c.conn.Close() + } + return +} + func (c *connector) Write(b []byte) (int, error) { // TODO: check that start was called // TODO: check that Stop was not called c.mu.Lock() defer c.mu.Unlock() + + if c.conn == nil { + return 0, errors.New("no connection established") + } return c.conn.Write(b) } -- GitLab