From e1f57d373a96252ef58e52c60c25248926f7ef0f Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 31 Oct 2017 20:15:57 +0900 Subject: [PATCH] ing --- client.go | 70 +++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/client.go b/client.go index 64a1fba..8b948cd 100644 --- a/client.go +++ b/client.go @@ -45,8 +45,7 @@ func (c *client) Start() { c.ch.OnStart() - c.stopWg.Add(1) - go runClient(c) + runClient(c) } func (c *client) Stop() { @@ -66,44 +65,43 @@ func runClient(c *client) { var err error var stopping atomic.Value - for { - dialChan := make(chan struct{}) - go func() { - if conn, err = c.ch.Dial(); err != nil { - if stopping.Load() == nil { - logging.Logger.Error(fmt.Sprintf("Client: [%s].Cannot establish rpc connection: [%s]", c.ch.GetAddr(), err)) - } + dialChan := make(chan struct{}) + go func() { + if conn, err = c.ch.Dial(); err != nil { + if stopping.Load() == nil { + logging.Logger.Error(fmt.Sprintf("Client: [%s].Cannot establish rpc connection: [%s]", c.ch.GetAddr(), err)) } - close(dialChan) - }() - - select { - case <-c.stopChan: - stopping.Store(true) - <-dialChan - return - case <-dialChan: - c.Stats.incDialCalls() } + close(dialChan) + }() - if err != nil { - c.Stats.incDialErrors() - select { - case <-c.stopChan: - return - case <-time.After(time.Second): - } - continue - } - - handleClientConnection(c, conn) - - select { - case <-c.stopChan: - return - default: - } + select { + case <-c.stopChan: + stopping.Store(true) + <-dialChan + return + case <-dialChan: + c.Stats.incDialCalls() } + + if err != nil { + c.Stats.incDialErrors() + select { + case <-c.stopChan: + return + case <-time.After(time.Second): + } + return + } + + go handleClientConnection(c, conn) + + select { + case <-c.stopChan: + return + default: + } + } func handleClientConnection(c *client, conn io.ReadWriteCloser) {