Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
9785c0d0
Commit
9785c0d0
authored
Aug 23, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mod: gettyRPCClientConn -> gettyRPCClient, gettyRPCClientConnPool -> gettyRPCClientPool
parent
fc8c917d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
29 deletions
+29
-29
client.go
rpc/client.go
+3
-3
listener.go
rpc/listener.go
+2
-2
pool.go
rpc/pool.go
+24
-24
No files found.
rpc/client.go
View file @
9785c0d0
...
...
@@ -26,7 +26,7 @@ func init() {
type
Client
struct
{
conf
ClientConfig
pool
*
gettyRPCClient
Conn
Pool
pool
*
gettyRPCClientPool
sequence
gxatomic
.
Uint64
pendingLock
sync
.
RWMutex
...
...
@@ -67,7 +67,7 @@ func (c *Client) Call(typ CodecType, addr, service, method string, args interfac
var
(
err
error
session
getty
.
Session
conn
*
gettyRPCClient
Conn
conn
*
gettyRPCClient
)
conn
,
session
,
err
=
c
.
selectSession
(
typ
,
addr
)
if
err
!=
nil
||
session
==
nil
{
...
...
@@ -97,7 +97,7 @@ func (c *Client) Close() {
c
.
pool
=
nil
}
func
(
c
*
Client
)
selectSession
(
typ
CodecType
,
addr
string
)
(
*
gettyRPCClient
Conn
,
getty
.
Session
,
error
)
{
func
(
c
*
Client
)
selectSession
(
typ
CodecType
,
addr
string
)
(
*
gettyRPCClient
,
getty
.
Session
,
error
)
{
rpcConn
,
err
:=
c
.
pool
.
getConn
(
typ
.
String
(),
addr
)
if
err
!=
nil
{
return
nil
,
nil
,
jerrors
.
Trace
(
err
)
...
...
rpc/listener.go
View file @
9785c0d0
...
...
@@ -169,10 +169,10 @@ func (h *RpcServerHandler) callService(session getty.Session, req GettyRPCReques
////////////////////////////////////////////
type
RpcClientHandler
struct
{
conn
*
gettyRPCClient
Conn
conn
*
gettyRPCClient
}
func
NewRpcClientHandler
(
client
*
gettyRPCClient
Conn
)
*
RpcClientHandler
{
func
NewRpcClientHandler
(
client
*
gettyRPCClient
)
*
RpcClientHandler
{
return
&
RpcClientHandler
{
conn
:
client
}
}
...
...
rpc/pool.go
View file @
9785c0d0
...
...
@@ -15,13 +15,13 @@ import (
jerrors
"github.com/juju/errors"
)
type
gettyRPCClient
Conn
struct
{
type
gettyRPCClient
struct
{
once
sync
.
Once
protocol
string
addr
string
created
int64
// 为0,则说明没有被创建或者被销毁了
pool
*
gettyRPCClient
Conn
Pool
pool
*
gettyRPCClientPool
lock
sync
.
RWMutex
gettyClient
getty
.
Client
...
...
@@ -32,8 +32,8 @@ var (
errClientPoolClosed
=
jerrors
.
New
(
"client pool closed"
)
)
func
newGettyRPCClientConn
(
pool
*
gettyRPCClient
ConnPool
,
protocol
,
addr
string
)
(
*
gettyRPCClientConn
,
error
)
{
c
:=
&
gettyRPCClient
Conn
{
func
newGettyRPCClientConn
(
pool
*
gettyRPCClient
Pool
,
protocol
,
addr
string
)
(
*
gettyRPCClient
,
error
)
{
c
:=
&
gettyRPCClient
{
protocol
:
protocol
,
addr
:
addr
,
pool
:
pool
,
...
...
@@ -61,7 +61,7 @@ func newGettyRPCClientConn(pool *gettyRPCClientConnPool, protocol, addr string)
return
c
,
nil
}
func
(
c
*
gettyRPCClient
Conn
)
newSession
(
session
getty
.
Session
)
error
{
func
(
c
*
gettyRPCClient
)
newSession
(
session
getty
.
Session
)
error
{
var
(
ok
bool
tcpConn
*
net
.
TCPConn
...
...
@@ -100,7 +100,7 @@ func (c *gettyRPCClientConn) newSession(session getty.Session) error {
return
nil
}
func
(
c
*
gettyRPCClient
Conn
)
selectSession
()
getty
.
Session
{
func
(
c
*
gettyRPCClient
)
selectSession
()
getty
.
Session
{
c
.
lock
.
RLock
()
defer
c
.
lock
.
RUnlock
()
...
...
@@ -115,7 +115,7 @@ func (c *gettyRPCClientConn) selectSession() getty.Session {
return
c
.
sessions
[
rand
.
Int31n
(
int32
(
count
))]
.
session
}
func
(
c
*
gettyRPCClient
Conn
)
addSession
(
session
getty
.
Session
)
{
func
(
c
*
gettyRPCClient
)
addSession
(
session
getty
.
Session
)
{
log
.
Debug
(
"add session{%s}"
,
session
.
Stat
())
if
session
==
nil
{
return
...
...
@@ -126,7 +126,7 @@ func (c *gettyRPCClientConn) addSession(session getty.Session) {
c
.
lock
.
Unlock
()
}
func
(
c
*
gettyRPCClient
Conn
)
removeSession
(
session
getty
.
Session
)
{
func
(
c
*
gettyRPCClient
)
removeSession
(
session
getty
.
Session
)
{
if
session
==
nil
{
return
}
...
...
@@ -150,7 +150,7 @@ func (c *gettyRPCClientConn) removeSession(session getty.Session) {
}
}
func
(
c
*
gettyRPCClient
Conn
)
updateSession
(
session
getty
.
Session
)
{
func
(
c
*
gettyRPCClient
)
updateSession
(
session
getty
.
Session
)
{
if
session
==
nil
{
return
}
...
...
@@ -168,7 +168,7 @@ func (c *gettyRPCClientConn) updateSession(session getty.Session) {
}
}
func
(
c
*
gettyRPCClient
Conn
)
getClientRpcSession
(
session
getty
.
Session
)
(
rpcSession
,
error
)
{
func
(
c
*
gettyRPCClient
)
getClientRpcSession
(
session
getty
.
Session
)
(
rpcSession
,
error
)
{
var
(
err
error
rpcSession
rpcSession
...
...
@@ -191,7 +191,7 @@ func (c *gettyRPCClientConn) getClientRpcSession(session getty.Session) (rpcSess
return
rpcSession
,
jerrors
.
Trace
(
err
)
}
func
(
c
*
gettyRPCClient
Conn
)
isAvailable
()
bool
{
func
(
c
*
gettyRPCClient
)
isAvailable
()
bool
{
if
c
.
selectSession
()
==
nil
{
return
false
}
...
...
@@ -199,8 +199,8 @@ func (c *gettyRPCClientConn) isAvailable() bool {
return
true
}
func
(
c
*
gettyRPCClient
Conn
)
close
()
error
{
err
:=
jerrors
.
Errorf
(
"close gettyRPCClient
Conn
{%#v} again"
,
c
)
func
(
c
*
gettyRPCClient
)
close
()
error
{
err
:=
jerrors
.
Errorf
(
"close gettyRPCClient{%#v} again"
,
c
)
c
.
once
.
Do
(
func
()
{
// delete @c from client pool
c
.
pool
.
remove
(
c
)
...
...
@@ -219,25 +219,25 @@ func (c *gettyRPCClientConn) close() error {
return
err
}
type
gettyRPCClient
Conn
Pool
struct
{
type
gettyRPCClientPool
struct
{
rpcClient
*
Client
size
int
// []*gettyRPCClient
Conn
数组的size
ttl
int64
// 每个gettyRPCClient
Conn
的有效期时间. pool对象会在getConn时执行ttl检查
size
int
// []*gettyRPCClient数组的size
ttl
int64
// 每个gettyRPCClient的有效期时间. pool对象会在getConn时执行ttl检查
sync
.
Mutex
connMap
map
[
string
][]
*
gettyRPCClient
Conn
// 从[]*gettyRPCClientConn
可见key是连接地址,而value是对应这个地址的连接数组
connMap
map
[
string
][]
*
gettyRPCClient
// 从[]*gettyRPCClient
可见key是连接地址,而value是对应这个地址的连接数组
}
func
newGettyRPCClientConnPool
(
rpcClient
*
Client
,
size
int
,
ttl
time
.
Duration
)
*
gettyRPCClient
Conn
Pool
{
return
&
gettyRPCClient
Conn
Pool
{
func
newGettyRPCClientConnPool
(
rpcClient
*
Client
,
size
int
,
ttl
time
.
Duration
)
*
gettyRPCClientPool
{
return
&
gettyRPCClientPool
{
rpcClient
:
rpcClient
,
size
:
size
,
ttl
:
int64
(
ttl
.
Seconds
()),
connMap
:
make
(
map
[
string
][]
*
gettyRPCClient
Conn
),
connMap
:
make
(
map
[
string
][]
*
gettyRPCClient
),
}
}
func
(
p
*
gettyRPCClient
Conn
Pool
)
close
()
{
func
(
p
*
gettyRPCClientPool
)
close
()
{
p
.
Lock
()
connMap
:=
p
.
connMap
p
.
connMap
=
nil
...
...
@@ -249,7 +249,7 @@ func (p *gettyRPCClientConnPool) close() {
}
}
func
(
p
*
gettyRPCClient
ConnPool
)
getConn
(
protocol
,
addr
string
)
(
*
gettyRPCClientConn
,
error
)
{
func
(
p
*
gettyRPCClient
Pool
)
getConn
(
protocol
,
addr
string
)
(
*
gettyRPCClient
,
error
)
{
var
builder
strings
.
Builder
builder
.
WriteString
(
addr
)
...
...
@@ -284,7 +284,7 @@ func (p *gettyRPCClientConnPool) getConn(protocol, addr string) (*gettyRPCClient
return
newGettyRPCClientConn
(
p
,
protocol
,
addr
)
}
func
(
p
*
gettyRPCClient
ConnPool
)
release
(
conn
*
gettyRPCClientConn
,
err
error
)
{
func
(
p
*
gettyRPCClient
Pool
)
release
(
conn
*
gettyRPCClient
,
err
error
)
{
if
conn
==
nil
||
conn
.
created
==
0
{
return
}
...
...
@@ -316,7 +316,7 @@ func (p *gettyRPCClientConnPool) release(conn *gettyRPCClientConn, err error) {
p
.
connMap
[
key
]
=
append
(
connArray
,
conn
)
}
func
(
p
*
gettyRPCClient
ConnPool
)
remove
(
conn
*
gettyRPCClientConn
)
{
func
(
p
*
gettyRPCClient
Pool
)
remove
(
conn
*
gettyRPCClient
)
{
if
conn
==
nil
||
conn
.
created
==
0
{
return
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment