From d1da7ba632c72ba6417681427dd1614b6b6d1ccf Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Wed, 18 Dec 2019 15:50:55 +0800 Subject: [PATCH 1/3] change the position of the lock --- protocol/dubbo/pool.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/protocol/dubbo/pool.go b/protocol/dubbo/pool.go index d619a2f8fe..0c4e74ae71 100644 --- a/protocol/dubbo/pool.go +++ b/protocol/dubbo/pool.go @@ -291,8 +291,8 @@ func (p *gettyRPCClientPool) close() { func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPCClient, error) { p.Lock() - defer p.Unlock() if p.conns == nil { + p.Unlock() return nil, errClientPoolClosed } @@ -308,9 +308,10 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC continue } conn.updateActive(now) //update active time - + p.Unlock() return conn, nil } + p.Unlock() // create new conn return newGettyRPCClientConn(p, protocol, addr) } From 16aff1fb0992a78b1e6ef38458b39754eae60c27 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Wed, 18 Dec 2019 16:40:39 +0800 Subject: [PATCH 2/3] fix bug for reverseRegistryProtocol fix travis-ci problem change lock position change lock position --- protocol/dubbo/pool.go | 18 +++++++++++------- registry/protocol/protocol.go | 6 +++--- registry/protocol/protocol_test.go | 5 ----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/protocol/dubbo/pool.go b/protocol/dubbo/pool.go index 0c4e74ae71..6a5a62bd96 100644 --- a/protocol/dubbo/pool.go +++ b/protocol/dubbo/pool.go @@ -289,10 +289,18 @@ func (p *gettyRPCClientPool) close() { } func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPCClient, error) { - + if conn, err := p.selectGettyRpcClient(protocol, addr); err != nil { + return nil, err + } else if conn != nil { + return conn, nil + } + // create new conn + return newGettyRPCClientConn(p, protocol, addr) +} +func (p *gettyRPCClientPool) selectGettyRpcClient(protocol, addr string) (*gettyRPCClient, error) { p.Lock() + defer p.Unlock() if p.conns == nil { - p.Unlock() return nil, errClientPoolClosed } @@ -308,14 +316,10 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC continue } conn.updateActive(now) //update active time - p.Unlock() return conn, nil } - p.Unlock() - // create new conn - return newGettyRPCClientConn(p, protocol, addr) + return nil, nil } - func (p *gettyRPCClientPool) release(conn *gettyRPCClient, err error) { if conn == nil || conn.getActive() == 0 { return diff --git a/registry/protocol/protocol.go b/registry/protocol/protocol.go index ffdb2753d6..534a4b9459 100644 --- a/registry/protocol/protocol.go +++ b/registry/protocol/protocol.go @@ -338,10 +338,10 @@ func setProviderUrl(regURL *common.URL, providerURL *common.URL) { } func GetProtocol() protocol.Protocol { - if regProtocol == nil { - regProtocol = newRegistryProtocol() + if regProtocol != nil { + return regProtocol } - return regProtocol + return newRegistryProtocol() } type wrappedInvoker struct { diff --git a/registry/protocol/protocol_test.go b/registry/protocol/protocol_test.go index 761d140066..0c19da59df 100644 --- a/registry/protocol/protocol_test.go +++ b/registry/protocol/protocol_test.go @@ -291,8 +291,3 @@ func TestExportWithApplicationConfig(t *testing.T) { v2, _ := regProtocol.bounds.Load(getCacheKey(newUrl)) assert.NotNil(t, v2) } - -func TestGetProtocol(t *testing.T) { - singleton := GetProtocol() - assert.True(t, singleton == GetProtocol()) -} From e6e0e14f13aeadec56861d177c678f831ca349fd Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 20 Dec 2019 11:14:15 +0800 Subject: [PATCH 3/3] Modify code according to code review --- protocol/dubbo/pool.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/protocol/dubbo/pool.go b/protocol/dubbo/pool.go index 6a5a62bd96..b5bf040c67 100644 --- a/protocol/dubbo/pool.go +++ b/protocol/dubbo/pool.go @@ -289,13 +289,15 @@ func (p *gettyRPCClientPool) close() { } func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPCClient, error) { - if conn, err := p.selectGettyRpcClient(protocol, addr); err != nil { - return nil, err - } else if conn != nil { - return conn, nil + var ( + conn *gettyRPCClient + err error + ) + if conn, err = p.selectGettyRpcClient(protocol, addr); err == nil && conn == nil { + // create new conn + return newGettyRPCClientConn(p, protocol, addr) } - // create new conn - return newGettyRPCClientConn(p, protocol, addr) + return conn, err } func (p *gettyRPCClientPool) selectGettyRpcClient(protocol, addr string) (*gettyRPCClient, error) { p.Lock()