This commit is contained in:
crusader 2017-10-31 20:15:57 +09:00
parent 41d269f268
commit e1f57d373a

View File

@ -45,8 +45,7 @@ func (c *client) Start() {
c.ch.OnStart() c.ch.OnStart()
c.stopWg.Add(1) runClient(c)
go runClient(c)
} }
func (c *client) Stop() { func (c *client) Stop() {
@ -66,44 +65,43 @@ func runClient(c *client) {
var err error var err error
var stopping atomic.Value var stopping atomic.Value
for { dialChan := make(chan struct{})
dialChan := make(chan struct{}) go func() {
go func() { if conn, err = c.ch.Dial(); err != nil {
if conn, err = c.ch.Dial(); err != nil { if stopping.Load() == nil {
if stopping.Load() == nil { logging.Logger.Error(fmt.Sprintf("Client: [%s].Cannot establish rpc connection: [%s]", c.ch.GetAddr(), err))
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 { select {
c.Stats.incDialErrors() case <-c.stopChan:
select { stopping.Store(true)
case <-c.stopChan: <-dialChan
return return
case <-time.After(time.Second): case <-dialChan:
} c.Stats.incDialCalls()
continue
}
handleClientConnection(c, conn)
select {
case <-c.stopChan:
return
default:
}
} }
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) { func handleClientConnection(c *client, conn io.ReadWriteCloser) {