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
9f1418de
Unverified
Commit
9f1418de
authored
Mar 10, 2021
by
Xin.Zh
Committed by
GitHub
Mar 10, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #47 from wenxuwan/master
Exposure function of etcd
parents
fd94fb87
bc1e1e28
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
29 deletions
+46
-29
client.go
database/kv/etcd/v3/client.go
+30
-18
options.go
database/kv/etcd/v3/options.go
+16
-11
No files found.
database/kv/etcd/v3/client.go
View file @
9f1418de
...
@@ -41,16 +41,16 @@ var (
...
@@ -41,16 +41,16 @@ var (
// NewConfigClient create new Client
// NewConfigClient create new Client
func
NewConfigClient
(
opts
...
Option
)
*
Client
{
func
NewConfigClient
(
opts
...
Option
)
*
Client
{
options
:=
&
Options
{
options
:=
&
Options
{
heartbeat
:
1
,
// default h
eartbeat
Heartbeat
:
1
,
// default H
eartbeat
}
}
for
_
,
opt
:=
range
opts
{
for
_
,
opt
:=
range
opts
{
opt
(
options
)
opt
(
options
)
}
}
newClient
,
err
:=
NewClient
(
options
.
name
,
options
.
endpoints
,
options
.
timeout
,
options
.
h
eartbeat
)
newClient
,
err
:=
NewClient
(
options
.
Name
,
options
.
Endpoints
,
options
.
Timeout
,
options
.
H
eartbeat
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"new etcd client (
name{%s}, etcd addresses{%v}, t
imeout{%d}) = error{%v}"
,
log
.
Printf
(
"new etcd client (
Name{%s}, etcd addresses{%v}, T
imeout{%d}) = error{%v}"
,
options
.
name
,
options
.
endpoints
,
options
.
t
imeout
,
err
)
options
.
Name
,
options
.
Endpoints
,
options
.
T
imeout
,
err
)
}
}
return
newClient
return
newClient
}
}
...
@@ -136,6 +136,11 @@ func (c *Client) stop() bool {
...
@@ -136,6 +136,11 @@ func (c *Client) stop() bool {
}
}
}
}
// GetCtx return client context
func
(
c
*
Client
)
GetCtx
()
context
.
Context
{
return
c
.
ctx
}
// Close close client
// Close close client
func
(
c
*
Client
)
Close
()
{
func
(
c
*
Client
)
Close
()
{
if
c
==
nil
{
if
c
==
nil
{
...
@@ -155,7 +160,7 @@ func (c *Client) Close() {
...
@@ -155,7 +160,7 @@ func (c *Client) Close() {
if
c
.
rawClient
!=
nil
{
if
c
.
rawClient
!=
nil
{
c
.
clean
()
c
.
clean
()
}
}
log
.
Printf
(
"etcd client{
name:%s, e
ndpoints:%s} exit now."
,
c
.
name
,
c
.
endpoints
)
log
.
Printf
(
"etcd client{
Name:%s, E
ndpoints:%s} exit now."
,
c
.
name
,
c
.
endpoints
)
}
}
func
(
c
*
Client
)
keepSession
()
error
{
func
(
c
*
Client
)
keepSession
()
error
{
...
@@ -173,7 +178,7 @@ func (c *Client) keepSession() error {
...
@@ -173,7 +178,7 @@ 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 client {
endpoints:%v, n
ame:%s} keep goroutine game over."
,
c
.
endpoints
,
c
.
name
)
log
.
Printf
(
"etcd client {
Endpoints:%v, N
ame:%s} keep goroutine game over."
,
c
.
endpoints
,
c
.
name
)
}()
}()
for
{
for
{
...
@@ -194,16 +199,22 @@ func (c *Client) keepSessionLoop(s *concurrency.Session) {
...
@@ -194,16 +199,22 @@ func (c *Client) keepSessionLoop(s *concurrency.Session) {
}
}
}
}
func
(
c
*
Client
)
getRawClient
()
*
clientv3
.
Client
{
//GetRawClient return etcd raw client
func
(
c
*
Client
)
GetRawClient
()
*
clientv3
.
Client
{
c
.
lock
.
RLock
()
c
.
lock
.
RLock
()
defer
c
.
lock
.
RUnlock
()
defer
c
.
lock
.
RUnlock
()
return
c
.
rawClient
return
c
.
rawClient
}
}
//GetEndPoints return etcd endpoints
func
(
c
*
Client
)
GetEndPoints
()
[]
string
{
return
c
.
endpoints
}
// if k not exist will put k/v in etcd, otherwise return nil
// if k not exist will put k/v in etcd, otherwise return nil
func
(
c
*
Client
)
put
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
func
(
c
*
Client
)
put
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
return
ErrNilETCDV3Client
...
@@ -219,7 +230,7 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
...
@@ -219,7 +230,7 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
// if k not exist will put k/v in etcd
// if k not exist will put k/v in etcd
// if k is already exist in etcd, replace it
// if k is already exist in etcd, replace it
func
(
c
*
Client
)
update
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
func
(
c
*
Client
)
update
(
k
string
,
v
string
,
opts
...
clientv3
.
OpOption
)
error
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
return
ErrNilETCDV3Client
...
@@ -233,7 +244,7 @@ func (c *Client) update(k string, v string, opts ...clientv3.OpOption) error {
...
@@ -233,7 +244,7 @@ func (c *Client) update(k string, v string, opts ...clientv3.OpOption) error {
}
}
func
(
c
*
Client
)
delete
(
k
string
)
error
{
func
(
c
*
Client
)
delete
(
k
string
)
error
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
return
ErrNilETCDV3Client
...
@@ -244,7 +255,7 @@ func (c *Client) delete(k string) error {
...
@@ -244,7 +255,7 @@ func (c *Client) delete(k string) error {
}
}
func
(
c
*
Client
)
get
(
k
string
)
(
string
,
error
)
{
func
(
c
*
Client
)
get
(
k
string
)
(
string
,
error
)
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
""
,
ErrNilETCDV3Client
return
""
,
ErrNilETCDV3Client
...
@@ -264,7 +275,7 @@ func (c *Client) get(k string) (string, error) {
...
@@ -264,7 +275,7 @@ func (c *Client) get(k string) (string, error) {
// CleanKV delete all key and value
// CleanKV delete all key and value
func
(
c
*
Client
)
CleanKV
()
error
{
func
(
c
*
Client
)
CleanKV
()
error
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
return
ErrNilETCDV3Client
...
@@ -274,8 +285,9 @@ func (c *Client) CleanKV() error {
...
@@ -274,8 +285,9 @@ func (c *Client) CleanKV() error {
return
err
return
err
}
}
func
(
c
*
Client
)
getChildren
(
k
string
)
([]
string
,
[]
string
,
error
)
{
//GetChildren return node children
rawClient
:=
c
.
getRawClient
()
func
(
c
*
Client
)
GetChildren
(
k
string
)
([]
string
,
[]
string
,
error
)
{
rawClient
:=
c
.
GetRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
nil
,
nil
,
ErrNilETCDV3Client
return
nil
,
nil
,
ErrNilETCDV3Client
...
@@ -300,7 +312,7 @@ func (c *Client) getChildren(k string) ([]string, []string, error) {
...
@@ -300,7 +312,7 @@ func (c *Client) getChildren(k string) ([]string, []string, error) {
}
}
func
(
c
*
Client
)
watchWithPrefix
(
prefix
string
)
(
clientv3
.
WatchChan
,
error
)
{
func
(
c
*
Client
)
watchWithPrefix
(
prefix
string
)
(
clientv3
.
WatchChan
,
error
)
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
nil
,
ErrNilETCDV3Client
return
nil
,
ErrNilETCDV3Client
...
@@ -310,7 +322,7 @@ func (c *Client) watchWithPrefix(prefix string) (clientv3.WatchChan, error) {
...
@@ -310,7 +322,7 @@ func (c *Client) watchWithPrefix(prefix string) (clientv3.WatchChan, error) {
}
}
func
(
c
*
Client
)
watch
(
k
string
)
(
clientv3
.
WatchChan
,
error
)
{
func
(
c
*
Client
)
watch
(
k
string
)
(
clientv3
.
WatchChan
,
error
)
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
nil
,
ErrNilETCDV3Client
return
nil
,
ErrNilETCDV3Client
...
@@ -320,7 +332,7 @@ func (c *Client) watch(k string) (clientv3.WatchChan, error) {
...
@@ -320,7 +332,7 @@ func (c *Client) watch(k string) (clientv3.WatchChan, error) {
}
}
func
(
c
*
Client
)
keepAliveKV
(
k
string
,
v
string
)
error
{
func
(
c
*
Client
)
keepAliveKV
(
k
string
,
v
string
)
error
{
rawClient
:=
c
.
g
etRawClient
()
rawClient
:=
c
.
G
etRawClient
()
if
rawClient
==
nil
{
if
rawClient
==
nil
{
return
ErrNilETCDV3Client
return
ErrNilETCDV3Client
...
@@ -389,7 +401,7 @@ func (c *Client) RegisterTemp(k, v string) error {
...
@@ -389,7 +401,7 @@ func (c *Client) RegisterTemp(k, v string) error {
// GetChildrenKVList gets children kv list by @k
// GetChildrenKVList gets children kv list by @k
func
(
c
*
Client
)
GetChildrenKVList
(
k
string
)
([]
string
,
[]
string
,
error
)
{
func
(
c
*
Client
)
GetChildrenKVList
(
k
string
)
([]
string
,
[]
string
,
error
)
{
kList
,
vList
,
err
:=
c
.
g
etChildren
(
k
)
kList
,
vList
,
err
:=
c
.
G
etChildren
(
k
)
return
kList
,
vList
,
perrors
.
WithMessagef
(
err
,
"get key children (key %s)"
,
k
)
return
kList
,
vList
,
perrors
.
WithMessagef
(
err
,
"get key children (key %s)"
,
k
)
}
}
...
...
database/kv/etcd/v3/options.go
View file @
9f1418de
...
@@ -26,19 +26,24 @@ const (
...
@@ -26,19 +26,24 @@ const (
ConnDelay
=
3
ConnDelay
=
3
// MaxFailTimes max failure times
// MaxFailTimes max failure times
MaxFailTimes
=
15
MaxFailTimes
=
15
// RegistryETCDV3Client client
n
ame
// RegistryETCDV3Client client
N
ame
RegistryETCDV3Client
=
"etcd registry"
RegistryETCDV3Client
=
"etcd registry"
// MetadataETCDV3Client client
n
ame
// MetadataETCDV3Client client
N
ame
MetadataETCDV3Client
=
"etcd metadata"
MetadataETCDV3Client
=
"etcd metadata"
)
)
// Options client configuration
// Options client configuration
type
Options
struct
{
type
Options
struct
{
name
string
//Name etcd server name
endpoints
[]
string
Name
string
client
*
Client
//Endpoints etcd endpoints
timeout
time
.
Duration
Endpoints
[]
string
heartbeat
int
// heartbeat second
//Client etcd client
Client
*
Client
//Timeout timeout
Timeout
time
.
Duration
//Heartbeat second
Heartbeat
int
}
}
// Option will define a function of handling Options
// Option will define a function of handling Options
...
@@ -47,27 +52,27 @@ type Option func(*Options)
...
@@ -47,27 +52,27 @@ type Option func(*Options)
// WithEndpoints sets etcd client endpoints
// WithEndpoints sets etcd client endpoints
func
WithEndpoints
(
endpoints
...
string
)
Option
{
func
WithEndpoints
(
endpoints
...
string
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
e
ndpoints
=
endpoints
opt
.
E
ndpoints
=
endpoints
}
}
}
}
// WithName sets etcd client name
// WithName sets etcd client name
func
WithName
(
name
string
)
Option
{
func
WithName
(
name
string
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
n
ame
=
name
opt
.
N
ame
=
name
}
}
}
}
// WithTimeout sets etcd client timeout
// WithTimeout sets etcd client timeout
func
WithTimeout
(
timeout
time
.
Duration
)
Option
{
func
WithTimeout
(
timeout
time
.
Duration
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
t
imeout
=
timeout
opt
.
T
imeout
=
timeout
}
}
}
}
// WithHeartbeat sets etcd client heartbeat
// WithHeartbeat sets etcd client heartbeat
func
WithHeartbeat
(
heartbeat
int
)
Option
{
func
WithHeartbeat
(
heartbeat
int
)
Option
{
return
func
(
opt
*
Options
)
{
return
func
(
opt
*
Options
)
{
opt
.
h
eartbeat
=
heartbeat
opt
.
H
eartbeat
=
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