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
Show 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 (
...
@@ -32,13 +32,13 @@ import (
)
)
var
(
var
(
// ErrNilETCDV3Client raw
C
lient nil
// ErrNilETCDV3Client raw
c
lient nil
ErrNilETCDV3Client
=
perrors
.
New
(
"etcd raw Client is nil"
)
// full describe the ERR
ErrNilETCDV3Client
=
perrors
.
New
(
"etcd raw Client is nil"
)
// full describe the ERR
// ErrKVPairNotFound not found key
// ErrKVPairNotFound not found key
ErrKVPairNotFound
=
perrors
.
New
(
"k/v pair not found"
)
ErrKVPairNotFound
=
perrors
.
New
(
"k/v pair not found"
)
)
)
// NewConfigClient create new
C
lient
// NewConfigClient create new
c
lient
func
NewConfigClient
(
opts
...
Option
)
*
Client
{
func
NewConfigClient
(
opts
...
Option
)
*
Client
{
options
:=
&
Options
{
options
:=
&
Options
{
Heartbeat
:
1
,
// default Heartbeat
Heartbeat
:
1
,
// default Heartbeat
...
@@ -49,13 +49,13 @@ func NewConfigClient(opts ...Option) *Client {
...
@@ -49,13 +49,13 @@ func NewConfigClient(opts ...Option) *Client {
newClient
,
err
:=
NewClient
(
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
options
.
Heartbeat
)
newClient
,
err
:=
NewClient
(
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
options
.
Heartbeat
)
if
err
!=
nil
{
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
)
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
err
)
}
}
return
newClient
return
newClient
}
}
// Client represents etcd
C
lient Configuration
// Client represents etcd
c
lient Configuration
type
Client
struct
{
type
Client
struct
{
lock
sync
.
RWMutex
lock
sync
.
RWMutex
quitOnce
sync
.
Once
quitOnce
sync
.
Once
...
@@ -74,7 +74,7 @@ type Client struct {
...
@@ -74,7 +74,7 @@ type Client struct {
Wait
sync
.
WaitGroup
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
)
{
func
NewClient
(
name
string
,
endpoints
[]
string
,
timeout
time
.
Duration
,
heartbeat
int
)
(
*
Client
,
error
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
...
@@ -87,7 +87,7 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
...
@@ -87,7 +87,7 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
if
err
!=
nil
{
if
err
!=
nil
{
cancel
()
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
{
c
:=
&
Client
{
...
@@ -105,20 +105,20 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
...
@@ -105,20 +105,20 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
if
err
:=
c
.
keepSession
();
err
!=
nil
{
if
err
:=
c
.
keepSession
();
err
!=
nil
{
cancel
()
cancel
()
return
nil
,
perrors
.
WithMessage
(
err
,
"
C
lient keep session"
)
return
nil
,
perrors
.
WithMessage
(
err
,
"
c
lient keep session"
)
}
}
return
c
,
nil
return
c
,
nil
}
}
// NOTICE: need to get the lock before calling this method
// NOTICE: need to get the lock before calling this method
func
(
c
*
Client
)
clean
()
{
func
(
c
*
Client
)
clean
()
{
// close raw
C
lient
// close raw
c
lient
c
.
rawClient
.
Close
()
c
.
rawClient
.
Close
()
// cancel ctx for raw
C
lient
// cancel ctx for raw
c
lient
c
.
cancel
()
c
.
cancel
()
// clean raw
C
lient
// clean raw
c
lient
c
.
rawClient
=
nil
c
.
rawClient
=
nil
}
}
...
@@ -136,23 +136,23 @@ func (c *Client) stop() bool {
...
@@ -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
{
func
(
c
*
Client
)
GetCtx
()
context
.
Context
{
return
c
.
ctx
return
c
.
ctx
}
}
// Close close
C
lient
// Close close
c
lient
func
(
c
*
Client
)
Close
()
{
func
(
c
*
Client
)
Close
()
{
if
c
==
nil
{
if
c
==
nil
{
return
return
}
}
// stop the
C
lient
// stop the
c
lient
if
ret
:=
c
.
stop
();
!
ret
{
if
ret
:=
c
.
stop
();
!
ret
{
return
return
}
}
// wait
C
lient keep session stop
// wait
c
lient keep session stop
c
.
Wait
.
Wait
()
c
.
Wait
.
Wait
()
c
.
lock
.
Lock
()
c
.
lock
.
Lock
()
...
@@ -160,7 +160,7 @@ func (c *Client) Close() {
...
@@ -160,7 +160,7 @@ func (c *Client) Close() {
if
c
.
rawClient
!=
nil
{
if
c
.
rawClient
!=
nil
{
c
.
clean
()
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
{
func
(
c
*
Client
)
keepSession
()
error
{
...
@@ -178,20 +178,20 @@ func (c *Client) keepSession() error {
...
@@ -178,20 +178,20 @@ func (c *Client) keepSession() error {
func
(
c
*
Client
)
keepSessionLoop
(
s
*
concurrency
.
Session
)
{
func
(
c
*
Client
)
keepSessionLoop
(
s
*
concurrency
.
Session
)
{
defer
func
()
{
defer
func
()
{
c
.
Wait
.
Done
()
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
{
for
{
select
{
select
{
case
<-
c
.
Done
()
:
case
<-
c
.
Done
()
:
//
Client be stopped, will clean the C
lient hold resources
//
client be stopped, will clean the c
lient hold resources
return
return
case
<-
s
.
Done
()
:
case
<-
s
.
Done
()
:
log
.
Print
(
"etcd server stopped"
)
log
.
Print
(
"etcd server stopped"
)
c
.
lock
.
Lock
()
c
.
lock
.
Lock
()
// when etcd server stopped, cancel ctx, stop all watchers
// when etcd server stopped, cancel ctx, stop all watchers
c
.
clean
()
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
.
stop
()
c
.
lock
.
Unlock
()
c
.
lock
.
Unlock
()
return
return
...
@@ -199,7 +199,7 @@ func (c *Client) keepSessionLoop(s *concurrency.Session) {
...
@@ -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
{
func
(
c
*
Client
)
GetRawClient
()
*
clientv3
.
Client
{
c
.
lock
.
RLock
()
c
.
lock
.
RLock
()
defer
c
.
lock
.
RUnlock
()
defer
c
.
lock
.
RUnlock
()
...
@@ -207,7 +207,7 @@ func (c *Client) GetRawClient() *clientv3.Client {
...
@@ -207,7 +207,7 @@ func (c *Client) GetRawClient() *clientv3.Client {
return
c
.
rawClient
return
c
.
rawClient
}
}
//GetEndPoints return etcd
E
ndpoints
//GetEndPoints return etcd
e
ndpoints
func
(
c
*
Client
)
GetEndPoints
()
[]
string
{
func
(
c
*
Client
)
GetEndPoints
()
[]
string
{
return
c
.
endpoints
return
c
.
endpoints
}
}
...
@@ -362,7 +362,7 @@ func (c *Client) Done() <-chan struct{} {
...
@@ -362,7 +362,7 @@ func (c *Client) Done() <-chan struct{} {
return
c
.
exit
return
c
.
exit
}
}
// Valid check
C
lient
// Valid check
c
lient
func
(
c
*
Client
)
Valid
()
bool
{
func
(
c
*
Client
)
Valid
()
bool
{
select
{
select
{
case
<-
c
.
exit
:
case
<-
c
.
exit
:
...
...
database/kv/etcd/v3/client_test.go
View file @
22d0b82a
...
@@ -48,7 +48,7 @@ var tests = []struct {
...
@@ -48,7 +48,7 @@ var tests = []struct {
{
input
:
struct
{
{
input
:
struct
{
k
string
k
string
v
string
v
string
}{
k
:
"
N
ame"
,
v
:
"scott.wang"
}},
}{
k
:
"
n
ame"
,
v
:
"scott.wang"
}},
{
input
:
struct
{
{
input
:
struct
{
k
string
k
string
v
string
v
string
...
@@ -64,7 +64,7 @@ var tests = []struct {
...
@@ -64,7 +64,7 @@ var tests = []struct {
}
}
// test dataset prefix
// test dataset prefix
const
prefix
=
"
N
ame"
const
prefix
=
"
n
ame"
type
ClientTestSuite
struct
{
type
ClientTestSuite
struct
{
suite
.
Suite
suite
.
Suite
...
@@ -129,7 +129,7 @@ func (suite *ClientTestSuite) setUpClient() *Client {
...
@@ -129,7 +129,7 @@ func (suite *ClientTestSuite) setUpClient() *Client {
return
c
return
c
}
}
// set up a
C
lient for suite
// set up a
c
lient for suite
func
(
suite
*
ClientTestSuite
)
SetupTest
()
{
func
(
suite
*
ClientTestSuite
)
SetupTest
()
{
c
:=
suite
.
setUpClient
()
c
:=
suite
.
setUpClient
()
c
.
CleanKV
()
c
.
CleanKV
()
...
@@ -154,11 +154,11 @@ func (suite *ClientTestSuite) TestClientValid() {
...
@@ -154,11 +154,11 @@ func (suite *ClientTestSuite) TestClientValid() {
t
:=
suite
.
T
()
t
:=
suite
.
T
()
if
!
c
.
Valid
()
{
if
!
c
.
Valid
()
{
t
.
Fatal
(
"
C
lient is not valid"
)
t
.
Fatal
(
"
c
lient is not valid"
)
}
}
c
.
Close
()
c
.
Close
()
if
suite
.
client
.
Valid
()
!=
false
{
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() {
...
@@ -174,7 +174,7 @@ func (suite *ClientTestSuite) TestClientDone() {
c
.
Wait
.
Wait
()
c
.
Wait
.
Wait
()
if
c
.
Valid
()
{
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 (
...
@@ -26,51 +26,51 @@ const (
ConnDelay
=
3
ConnDelay
=
3
// MaxFailTimes max failure times
// MaxFailTimes max failure times
MaxFailTimes
=
15
MaxFailTimes
=
15
// RegistryETCDV3Client
C
lient Name
// RegistryETCDV3Client
c
lient Name
RegistryETCDV3Client
=
"etcd registry"
RegistryETCDV3Client
=
"etcd registry"
// MetadataETCDV3Client
C
lient Name
// MetadataETCDV3Client
c
lient Name
MetadataETCDV3Client
=
"etcd metadata"
MetadataETCDV3Client
=
"etcd metadata"
)
)
// Options
C
lient configuration
// Options
c
lient configuration
type
Options
struct
{
type
Options
struct
{
//Name etcd server
N
ame
//Name etcd server
n
ame
Name
string
Name
string
//Endpoints etcd
E
ndpoints
//Endpoints etcd
e
ndpoints
Endpoints
[]
string
Endpoints
[]
string
//Client etcd
C
lient
//Client etcd
c
lient
Client
*
Client
Client
*
Client
//Timeout
T
imeout
//Timeout
t
imeout
Timeout
time
.
Duration
Timeout
time
.
Duration
//Heartbeat
//Heartbeat
second
Heartbeat
int
// Heartbeat second
Heartbeat
int
}
}
// Option will define a function of handling Options
// Option will define a function of handling Options
type
Option
func
(
*
Options
)
type
Option
func
(
*
Options
)
// WithEndpoints sets etcd
Client E
ndpoints
// WithEndpoints sets etcd
client e
ndpoints
func
WithEndpoints
(
endpoints
...
string
)
Option
{
func
WithEndpoints
(
endpoints
...
string
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
Endpoints
=
endpoints
opt
.
Endpoints
=
endpoints
}
}
}
}
// WithName sets etcd
Client N
ame
// WithName sets etcd
client n
ame
func
WithName
(
name
string
)
Option
{
func
WithName
(
name
string
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
Name
=
name
opt
.
Name
=
name
}
}
}
}
// WithTimeout sets etcd
Client T
imeout
// WithTimeout sets etcd
client t
imeout
func
WithTimeout
(
timeout
time
.
Duration
)
Option
{
func
WithTimeout
(
timeout
time
.
Duration
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
Timeout
=
timeout
opt
.
Timeout
=
timeout
}
}
}
}
// WithHeartbeat sets etcd
Client H
eartbeat
// WithHeartbeat sets etcd
client h
eartbeat
func
WithHeartbeat
(
heartbeat
int
)
Option
{
func
WithHeartbeat
(
heartbeat
int
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
Heartbeat
=
heartbeat
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