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
2342ba7f
Commit
2342ba7f
authored
Apr 28, 2021
by
Randy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code block format and modify comment
parent
fabd3330
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
28 deletions
+14
-28
client.go
database/kv/etcd/v3/client.go
+4
-23
client_test.go
database/kv/etcd/v3/client_test.go
+10
-5
No files found.
database/kv/etcd/v3/client.go
View file @
2342ba7f
...
...
@@ -46,17 +46,10 @@ var (
// NewConfigClient create new Client
func
NewConfigClient
(
opts
...
Option
)
*
Client
{
options
:=
&
Options
{
Heartbeat
:
1
,
// default Heartbeat
}
for
_
,
opt
:=
range
opts
{
opt
(
options
)
}
newClient
,
err
:=
NewConfigClientWithErr
(
opts
...
)
newClient
,
err
:=
NewClient
(
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
options
.
Heartbeat
)
if
err
!=
nil
{
log
.
Printf
(
"new etcd client (Name{%s}, etcd addresses{%v}, Timeout{%d}) = error{%v}"
,
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
err
)
log
.
Printf
(
"new etcd client = error{%v}"
,
err
)
}
return
newClient
}
...
...
@@ -237,7 +230,6 @@ func (c *Client) GetEndPoints() []string {
// if k not exist will put k/v in etcd, otherwise return ErrCompareFail
func
(
c
*
Client
)
create
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
@@ -259,7 +251,6 @@ func (c *Client) create(k string, v string, opts ...clientv3.OpOption) error {
// if k in bulk insertion not exist all, then put all k/v in etcd, otherwise return error
func
(
c
*
Client
)
batchCreate
(
kList
[]
string
,
vList
[]
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
@@ -293,10 +284,9 @@ func (c *Client) batchCreate(kList []string, vList []string, opts ...clientv3.Op
return
nil
}
// put k
v into etcd
// put k
/v in etcd, if fail return error
func
(
c
*
Client
)
put
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
@@ -305,11 +295,9 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
return
err
}
// if k not exist will put k/v in etcd
// if k is already exist in etcd and revision = revision, replace it, otherwise return ErrCompareFail
// put k/v in etcd when ModRevision equal with rev, if not return ErrCompareFail or other err
func
(
c
*
Client
)
updateWithRev
(
k
string
,
v
string
,
rev
int64
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
@@ -330,7 +318,6 @@ func (c *Client) updateWithRev(k string, v string, rev int64, opts ...clientv3.O
func
(
c
*
Client
)
delete
(
k
string
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
@@ -341,7 +328,6 @@ func (c *Client) delete(k string) error {
func
(
c
*
Client
)
get
(
k
string
)
(
string
,
error
)
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
""
,
ErrNilETCDV3Client
}
...
...
@@ -361,7 +347,6 @@ func (c *Client) get(k string) (string, error) {
// getValAndRev get value and revision
func
(
c
*
Client
)
getValAndRev
(
k
string
)
(
string
,
int64
,
error
)
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
""
,
ErrRevision
,
ErrNilETCDV3Client
}
...
...
@@ -381,7 +366,6 @@ func (c *Client) getValAndRev(k string) (string, int64, error) {
// CleanKV delete all key and value
func
(
c
*
Client
)
CleanKV
()
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
@@ -393,7 +377,6 @@ func (c *Client) CleanKV() error {
// GetChildren return node children
func
(
c
*
Client
)
GetChildren
(
k
string
)
([]
string
,
[]
string
,
error
)
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
nil
,
nil
,
ErrNilETCDV3Client
}
...
...
@@ -419,7 +402,6 @@ func (c *Client) GetChildren(k string) ([]string, []string, error) {
// watchWithOption watch
func
(
c
*
Client
)
watchWithOption
(
k
string
,
opts
...
clientv3
.
OpOption
)
(
clientv3
.
WatchChan
,
error
)
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
nil
,
ErrNilETCDV3Client
}
...
...
@@ -429,7 +411,6 @@ func (c *Client) watchWithOption(k string, opts ...clientv3.OpOption) (clientv3.
func
(
c
*
Client
)
keepAliveKV
(
k
string
,
v
string
)
error
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
}
...
...
database/kv/etcd/v3/client_test.go
View file @
2342ba7f
...
...
@@ -118,10 +118,10 @@ func (suite *ClientTestSuite) TearDownSuite() {
}
func
(
suite
*
ClientTestSuite
)
setUpClient
()
*
Client
{
c
,
err
:=
NewC
lient
(
suite
.
etcdConfig
.
name
,
suite
.
etcdConfig
.
endpoints
,
suite
.
etcdConfig
.
timeout
,
suite
.
etcdConfig
.
heartbeat
)
c
,
err
:=
NewC
onfigClientWithErr
(
WithName
(
suite
.
etcdConfig
.
name
)
,
WithEndpoints
(
suite
.
etcdConfig
.
endpoints
...
)
,
WithTimeout
(
suite
.
etcdConfig
.
timeout
)
,
WithHeartbeat
(
suite
.
etcdConfig
.
heartbeat
)
)
if
err
!=
nil
{
suite
.
T
()
.
Fatal
(
err
)
}
...
...
@@ -269,6 +269,11 @@ func (suite *ClientTestSuite) TestBatchClientGetValAndRevKV() {
t
.
Fatal
(
err
)
}
err
=
c
.
Update
(
k
,
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
value
!=
expect
{
t
.
Fatalf
(
"expect %v but get %v"
,
expect
,
value
)
}
...
...
@@ -368,7 +373,7 @@ func (suite *ClientTestSuite) TestClientWatch() {
c
.
Close
()
}()
wc
,
err
:=
c
.
w
atchWithOption
(
prefix
)
wc
,
err
:=
c
.
W
atchWithOption
(
prefix
)
if
err
!=
nil
{
assert
.
Error
(
t
,
err
)
}
...
...
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