Unverified Commit 9f1418de authored by Xin.Zh's avatar Xin.Zh Committed by GitHub

Merge pull request #47 from wenxuwan/master

Exposure function of etcd
parents fd94fb87 bc1e1e28
...@@ -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 heartbeat Heartbeat: 1, // default Heartbeat
} }
for _, opt := range opts { for _, opt := range opts {
opt(options) opt(options)
} }
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 client (name{%s}, etcd addresses{%v}, timeout{%d}) = error{%v}", log.Printf("new etcd client (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
} }
...@@ -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, endpoints:%s} exit now.", c.name, c.endpoints) log.Printf("etcd client{Name:%s, Endpoints:%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, name:%s} keep goroutine game over.", c.endpoints, c.name) log.Printf("etcd client {Endpoints:%v, Name:%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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getRawClient() rawClient := c.GetRawClient()
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.getChildren(k) kList, vList, err := c.GetChildren(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)
} }
......
...@@ -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 name // RegistryETCDV3Client client Name
RegistryETCDV3Client = "etcd registry" RegistryETCDV3Client = "etcd registry"
// MetadataETCDV3Client client name // MetadataETCDV3Client client Name
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.endpoints = endpoints opt.Endpoints = 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.name = name opt.Name = 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.timeout = timeout opt.Timeout = 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.heartbeat = heartbeat opt.Heartbeat = heartbeat
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment