Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
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
22d0b82a
Commit
22d0b82a
authored
Mar 09, 2021
by
wangwx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change comments words
parent
7993b30b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
40 deletions
+40
-40
client.go
database/kv/etcd/v3/client.go
+21
-21
client_test.go
database/kv/etcd/v3/client_test.go
+6
-6
options.go
database/kv/etcd/v3/options.go
+13
-13
No files found.
database/kv/etcd/v3/client.go
View file @
22d0b82a
...
...
@@ -32,13 +32,13 @@ import (
)
var
(
// ErrNilETCDV3Client raw
C
lient nil
// ErrNilETCDV3Client raw
c
lient nil
ErrNilETCDV3Client
=
perrors
.
New
(
"etcd raw Client is nil"
)
// full describe the ERR
// ErrKVPairNotFound not found key
ErrKVPairNotFound
=
perrors
.
New
(
"k/v pair not found"
)
)
// NewConfigClient create new
C
lient
// NewConfigClient create new
c
lient
func
NewConfigClient
(
opts
...
Option
)
*
Client
{
options
:=
&
Options
{
Heartbeat
:
1
,
// default Heartbeat
...
...
@@ -49,13 +49,13 @@ func NewConfigClient(opts ...Option) *Client {
newClient
,
err
:=
NewClient
(
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
options
.
Heartbeat
)
if
err
!=
nil
{
log
.
Printf
(
"new etcd
C
lient (Name{%s}, etcd addresses{%v}, Timeout{%d}) = error{%v}"
,
log
.
Printf
(
"new etcd
c
lient (Name{%s}, etcd addresses{%v}, Timeout{%d}) = error{%v}"
,
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
err
)
}
return
newClient
}
// Client represents etcd
C
lient Configuration
// Client represents etcd
c
lient Configuration
type
Client
struct
{
lock
sync
.
RWMutex
quitOnce
sync
.
Once
...
...
@@ -74,7 +74,7 @@ type Client struct {
Wait
sync
.
WaitGroup
}
// NewClient create a
Client instance with Name, E
ndpoints etc.
// NewClient create a
client instance with name, e
ndpoints etc.
func
NewClient
(
name
string
,
endpoints
[]
string
,
timeout
time
.
Duration
,
heartbeat
int
)
(
*
Client
,
error
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
...
...
@@ -87,7 +87,7 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
if
err
!=
nil
{
cancel
()
return
nil
,
perrors
.
WithMessage
(
err
,
"new raw
C
lient block connect to server"
)
return
nil
,
perrors
.
WithMessage
(
err
,
"new raw
c
lient block connect to server"
)
}
c
:=
&
Client
{
...
...
@@ -105,20 +105,20 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
if
err
:=
c
.
keepSession
();
err
!=
nil
{
cancel
()
return
nil
,
perrors
.
WithMessage
(
err
,
"
C
lient keep session"
)
return
nil
,
perrors
.
WithMessage
(
err
,
"
c
lient keep session"
)
}
return
c
,
nil
}
// NOTICE: need to get the lock before calling this method
func
(
c
*
Client
)
clean
()
{
// close raw
C
lient
// close raw
c
lient
c
.
rawClient
.
Close
()
// cancel ctx for raw
C
lient
// cancel ctx for raw
c
lient
c
.
cancel
()
// clean raw
C
lient
// clean raw
c
lient
c
.
rawClient
=
nil
}
...
...
@@ -136,23 +136,23 @@ func (c *Client) stop() bool {
}
}
// GetCtx return
C
lient context
// GetCtx return
c
lient context
func
(
c
*
Client
)
GetCtx
()
context
.
Context
{
return
c
.
ctx
}
// Close close
C
lient
// Close close
c
lient
func
(
c
*
Client
)
Close
()
{
if
c
==
nil
{
return
}
// stop the
C
lient
// stop the
c
lient
if
ret
:=
c
.
stop
();
!
ret
{
return
}
// wait
C
lient keep session stop
// wait
c
lient keep session stop
c
.
Wait
.
Wait
()
c
.
lock
.
Lock
()
...
...
@@ -160,7 +160,7 @@ func (c *Client) Close() {
if
c
.
rawClient
!=
nil
{
c
.
clean
()
}
log
.
Printf
(
"etcd
C
lient{Name:%s, Endpoints:%s} exit now."
,
c
.
name
,
c
.
endpoints
)
log
.
Printf
(
"etcd
c
lient{Name:%s, Endpoints:%s} exit now."
,
c
.
name
,
c
.
endpoints
)
}
func
(
c
*
Client
)
keepSession
()
error
{
...
...
@@ -178,20 +178,20 @@ func (c *Client) keepSession() error {
func
(
c
*
Client
)
keepSessionLoop
(
s
*
concurrency
.
Session
)
{
defer
func
()
{
c
.
Wait
.
Done
()
log
.
Printf
(
"etcd
C
lient {Endpoints:%v, Name:%s} keep goroutine game over."
,
c
.
endpoints
,
c
.
name
)
log
.
Printf
(
"etcd
c
lient {Endpoints:%v, Name:%s} keep goroutine game over."
,
c
.
endpoints
,
c
.
name
)
}()
for
{
select
{
case
<-
c
.
Done
()
:
//
Client be stopped, will clean the C
lient hold resources
//
client be stopped, will clean the c
lient hold resources
return
case
<-
s
.
Done
()
:
log
.
Print
(
"etcd server stopped"
)
c
.
lock
.
Lock
()
// when etcd server stopped, cancel ctx, stop all watchers
c
.
clean
()
// when connection lose, stop
C
lient, trigger reconnect to etcd
// when connection lose, stop
c
lient, trigger reconnect to etcd
c
.
stop
()
c
.
lock
.
Unlock
()
return
...
...
@@ -199,7 +199,7 @@ func (c *Client) keepSessionLoop(s *concurrency.Session) {
}
}
//GetRawClient return etcd raw
C
lient
//GetRawClient return etcd raw
c
lient
func
(
c
*
Client
)
GetRawClient
()
*
clientv3
.
Client
{
c
.
lock
.
RLock
()
defer
c
.
lock
.
RUnlock
()
...
...
@@ -207,7 +207,7 @@ func (c *Client) GetRawClient() *clientv3.Client {
return
c
.
rawClient
}
//GetEndPoints return etcd
E
ndpoints
//GetEndPoints return etcd
e
ndpoints
func
(
c
*
Client
)
GetEndPoints
()
[]
string
{
return
c
.
endpoints
}
...
...
@@ -362,7 +362,7 @@ func (c *Client) Done() <-chan struct{} {
return
c
.
exit
}
// Valid check
C
lient
// Valid check
c
lient
func
(
c
*
Client
)
Valid
()
bool
{
select
{
case
<-
c
.
exit
:
...
...
database/kv/etcd/v3/client_test.go
View file @
22d0b82a
...
...
@@ -48,7 +48,7 @@ var tests = []struct {
{
input
:
struct
{
k
string
v
string
}{
k
:
"
N
ame"
,
v
:
"scott.wang"
}},
}{
k
:
"
n
ame"
,
v
:
"scott.wang"
}},
{
input
:
struct
{
k
string
v
string
...
...
@@ -64,7 +64,7 @@ var tests = []struct {
}
// test dataset prefix
const
prefix
=
"
N
ame"
const
prefix
=
"
n
ame"
type
ClientTestSuite
struct
{
suite
.
Suite
...
...
@@ -129,7 +129,7 @@ func (suite *ClientTestSuite) setUpClient() *Client {
return
c
}
// set up a
C
lient for suite
// set up a
c
lient for suite
func
(
suite
*
ClientTestSuite
)
SetupTest
()
{
c
:=
suite
.
setUpClient
()
c
.
CleanKV
()
...
...
@@ -154,11 +154,11 @@ func (suite *ClientTestSuite) TestClientValid() {
t
:=
suite
.
T
()
if
!
c
.
Valid
()
{
t
.
Fatal
(
"
C
lient is not valid"
)
t
.
Fatal
(
"
c
lient is not valid"
)
}
c
.
Close
()
if
suite
.
client
.
Valid
()
!=
false
{
t
.
Fatal
(
"
C
lient is valid"
)
t
.
Fatal
(
"
c
lient is valid"
)
}
}
...
...
@@ -174,7 +174,7 @@ func (suite *ClientTestSuite) TestClientDone() {
c
.
Wait
.
Wait
()
if
c
.
Valid
()
{
suite
.
T
()
.
Fatal
(
"
C
lient should be invalid then"
)
suite
.
T
()
.
Fatal
(
"
c
lient should be invalid then"
)
}
}
...
...
database/kv/etcd/v3/options.go
View file @
22d0b82a
...
...
@@ -26,51 +26,51 @@ const (
ConnDelay
=
3
// MaxFailTimes max failure times
MaxFailTimes
=
15
// RegistryETCDV3Client
C
lient Name
// RegistryETCDV3Client
c
lient Name
RegistryETCDV3Client
=
"etcd registry"
// MetadataETCDV3Client
C
lient Name
// MetadataETCDV3Client
c
lient Name
MetadataETCDV3Client
=
"etcd metadata"
)
// Options
C
lient configuration
// Options
c
lient configuration
type
Options
struct
{
//Name etcd server
N
ame
//Name etcd server
n
ame
Name
string
//Endpoints etcd
E
ndpoints
//Endpoints etcd
e
ndpoints
Endpoints
[]
string
//Client etcd
C
lient
//Client etcd
c
lient
Client
*
Client
//Timeout
T
imeout
//Timeout
t
imeout
Timeout
time
.
Duration
//Heartbeat
Heartbeat
int
// Heartbeat second
//Heartbeat
second
Heartbeat
int
}
// Option will define a function of handling Options
type
Option
func
(
*
Options
)
// WithEndpoints sets etcd
Client E
ndpoints
// WithEndpoints sets etcd
client e
ndpoints
func
WithEndpoints
(
endpoints
...
string
)
Option
{
return
func
(
opt
*
Options
)
{
opt
.
Endpoints
=
endpoints
}
}
// WithName sets etcd
Client N
ame
// WithName sets etcd
client n
ame
func
WithName
(
name
string
)
Option
{
return
func
(
opt
*
Options
)
{
opt
.
Name
=
name
}
}
// WithTimeout sets etcd
Client T
imeout
// WithTimeout sets etcd
client t
imeout
func
WithTimeout
(
timeout
time
.
Duration
)
Option
{
return
func
(
opt
*
Options
)
{
opt
.
Timeout
=
timeout
}
}
// WithHeartbeat sets etcd
Client H
eartbeat
// WithHeartbeat sets etcd
client h
eartbeat
func
WithHeartbeat
(
heartbeat
int
)
Option
{
return
func
(
opt
*
Options
)
{
opt
.
Heartbeat
=
heartbeat
...
...
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