Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gostnops
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
gostnops
Commits
7cf17969
Commit
7cf17969
authored
Feb 16, 2021
by
ztelur
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
quitOnce & keepSession
parent
bedf9d59
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
13 deletions
+20
-13
client.go
database/kv/etcd/v3/client.go
+20
-13
No files found.
database/kv/etcd/v3/client.go
View file @
7cf17969
...
...
@@ -58,6 +58,7 @@ func NewConfigClient(opts ...Option) *Client {
// Client represents etcd client Configuration
type
Client
struct
{
lock
sync
.
RWMutex
quitOnce
sync
.
Once
// these properties are only set once when they are started.
name
string
...
...
@@ -102,9 +103,9 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
exit
:
make
(
chan
struct
{}),
}
if
err
:=
c
.
maintenanceStatus
();
err
!=
nil
{
if
err
:=
c
.
keepSession
();
err
!=
nil
{
cancel
()
return
nil
,
perrors
.
WithMessage
(
err
,
"client
maintenance status
"
)
return
nil
,
perrors
.
WithMessage
(
err
,
"client
keep session
"
)
}
return
c
,
nil
}
...
...
@@ -124,11 +125,15 @@ func (c *Client) clean() {
func
(
c
*
Client
)
stop
()
bool
{
select
{
case
<-
c
.
exit
:
return
tru
e
return
fals
e
default
:
ret
:=
false
c
.
quitOnce
.
Do
(
func
()
{
ret
=
true
close
(
c
.
exit
)
})
return
ret
}
return
false
}
// Close close client
...
...
@@ -138,9 +143,11 @@ func (c *Client) Close() {
}
// stop the client
c
.
stop
()
if
ret
:=
c
.
stop
();
!
ret
{
return
}
// wait client
maintenance status
stop
// wait client
keep session
stop
c
.
Wait
.
Wait
()
c
.
lock
.
Lock
()
...
...
@@ -151,22 +158,22 @@ func (c *Client) Close() {
log
.
Printf
(
"etcd client{name:%s, endpoints:%s} exit now."
,
c
.
name
,
c
.
endpoints
)
}
func
(
c
*
Client
)
maintenanceStatus
()
error
{
func
(
c
*
Client
)
keepSession
()
error
{
s
,
err
:=
concurrency
.
NewSession
(
c
.
rawClient
,
concurrency
.
WithTTL
(
c
.
heartbeat
))
if
err
!=
nil
{
return
perrors
.
WithMessage
(
err
,
"new session with server"
)
}
// must add wg before go
maintenance status
goroutine
// must add wg before go
keep session
goroutine
c
.
Wait
.
Add
(
1
)
go
c
.
maintenanceStatus
Loop
(
s
)
go
c
.
keepSession
Loop
(
s
)
return
nil
}
func
(
c
*
Client
)
maintenanceStatus
Loop
(
s
*
concurrency
.
Session
)
{
func
(
c
*
Client
)
keepSession
Loop
(
s
*
concurrency
.
Session
)
{
defer
func
()
{
c
.
Wait
.
Done
()
log
.
Printf
(
"etcd client {endpoints:%v, name:%s}
maintenance
goroutine game over."
,
c
.
endpoints
,
c
.
name
)
log
.
Printf
(
"etcd client {endpoints:%v, name:%s}
keep
goroutine game over."
,
c
.
endpoints
,
c
.
name
)
}()
for
{
...
...
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