Skip to content
Snippets Groups Projects
Unverified Commit 141f1db4 authored by vito.he's avatar vito.he Committed by GitHub
Browse files

Merge pull request #186 from divebomb/develop

Imp: split protocol/dubbo/pool.go gettyRPCClient.close and gettyRPCClientPool.remove
parents a8cb98e8 aa5c1600
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"math/rand" "math/rand"
"net" "net"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
...@@ -78,11 +79,19 @@ func newGettyRPCClientConn(pool *gettyRPCClientPool, protocol, addr string) (*ge ...@@ -78,11 +79,19 @@ func newGettyRPCClientConn(pool *gettyRPCClientPool, protocol, addr string) (*ge
time.Sleep(1e6) time.Sleep(1e6)
} }
logger.Infof("client init ok") logger.Infof("client init ok")
c.created = time.Now().Unix() c.updateActive(time.Now().Unix())
return c, nil return c, nil
} }
func (c *gettyRPCClient) updateActive(active int64) {
atomic.StoreInt64(&c.created, active)
}
func (c *gettyRPCClient) getActive() int64 {
return atomic.LoadInt64(&c.created)
}
func (c *gettyRPCClient) newSession(session getty.Session) error { func (c *gettyRPCClient) newSession(session getty.Session) error {
var ( var (
ok bool ok bool
...@@ -169,9 +178,8 @@ func (c *gettyRPCClient) removeSession(session getty.Session) { ...@@ -169,9 +178,8 @@ func (c *gettyRPCClient) removeSession(session getty.Session) {
} }
logger.Infof("after remove session{%s}, left session number:%d", session.Stat(), len(c.sessions)) logger.Infof("after remove session{%s}, left session number:%d", session.Stat(), len(c.sessions))
if len(c.sessions) == 0 { if len(c.sessions) == 0 {
c.pool.Lock() c.pool.safeRemove(c)
c.close() // -> pool.remove(c) c.close()
c.pool.Unlock()
} }
} }
...@@ -225,10 +233,8 @@ func (c *gettyRPCClient) isAvailable() bool { ...@@ -225,10 +233,8 @@ func (c *gettyRPCClient) isAvailable() bool {
} }
func (c *gettyRPCClient) close() error { func (c *gettyRPCClient) close() error {
err := perrors.Errorf("close gettyRPCClient{%#v} again", c) closeErr := perrors.Errorf("close gettyRPCClient{%#v} again", c)
c.once.Do(func() { c.once.Do(func() {
// delete @c from client pool
c.pool.remove(c)
c.gettyClient.Close() c.gettyClient.Close()
c.gettyClient = nil c.gettyClient = nil
for _, s := range c.sessions { for _, s := range c.sessions {
...@@ -238,10 +244,17 @@ func (c *gettyRPCClient) close() error { ...@@ -238,10 +244,17 @@ func (c *gettyRPCClient) close() error {
} }
c.sessions = c.sessions[:0] c.sessions = c.sessions[:0]
c.created = 0 c.updateActive(0)
err = nil closeErr = nil
}) })
return err return closeErr
}
func (c *gettyRPCClient) safeClose() error {
c.lock.Lock()
defer c.lock.Unlock()
return c.close()
} }
type gettyRPCClientPool struct { type gettyRPCClientPool struct {
...@@ -268,7 +281,7 @@ func (p *gettyRPCClientPool) close() { ...@@ -268,7 +281,7 @@ func (p *gettyRPCClientPool) close() {
p.conns = nil p.conns = nil
p.Unlock() p.Unlock()
for _, conn := range conns { for _, conn := range conns {
conn.close() conn.safeClose()
} }
} }
...@@ -286,11 +299,14 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC ...@@ -286,11 +299,14 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC
conn := p.conns[len(p.conns)-1] conn := p.conns[len(p.conns)-1]
p.conns = p.conns[:len(p.conns)-1] p.conns = p.conns[:len(p.conns)-1]
if d := now - conn.created; d > p.ttl { if d := now - conn.getActive(); d > p.ttl {
conn.close() // -> pool.remove(c) if closeErr := conn.safeClose(); closeErr == nil {
p.remove(conn)
}
continue continue
} }
conn.created = now //update created time conn.updateActive(now) //update created time
return conn, nil return conn, nil
} }
// create new conn // create new conn
...@@ -298,34 +314,37 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC ...@@ -298,34 +314,37 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC
} }
func (p *gettyRPCClientPool) release(conn *gettyRPCClient, err error) { func (p *gettyRPCClientPool) release(conn *gettyRPCClient, err error) {
if conn == nil || conn.created == 0 { if conn == nil || conn.getActive() == 0 {
return return
} }
if err != nil { if err != nil {
conn.close() conn.safeClose()
return return
} }
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
if p.conns == nil { if p.conns == nil {
return return
} }
if len(p.conns) >= p.size { if len(p.conns) >= p.size {
conn.close() if closeErr := conn.safeClose(); closeErr == nil {
// delete @conn from client pool
p.remove(conn)
}
return return
} }
p.conns = append(p.conns, conn) p.conns = append(p.conns, conn)
} }
func (p *gettyRPCClientPool) remove(conn *gettyRPCClient) { func (p *gettyRPCClientPool) remove(conn *gettyRPCClient) {
if conn == nil || conn.created == 0 { if conn == nil || conn.getActive() == 0 {
return return
} }
//p.Lock()
//defer p.Unlock()
if p.conns == nil { if p.conns == nil {
return return
} }
...@@ -339,3 +358,10 @@ func (p *gettyRPCClientPool) remove(conn *gettyRPCClient) { ...@@ -339,3 +358,10 @@ func (p *gettyRPCClientPool) remove(conn *gettyRPCClient) {
} }
} }
} }
func (p *gettyRPCClientPool) safeRemove(conn *gettyRPCClient) {
p.Lock()
defer p.Unlock()
p.remove(conn)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment