Commit 22d0b82a authored by wangwx's avatar wangwx

change comments words

parent 7993b30b
...@@ -32,13 +32,13 @@ import ( ...@@ -32,13 +32,13 @@ import (
) )
var ( var (
// ErrNilETCDV3Client raw Client nil // ErrNilETCDV3Client raw client 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 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
...@@ -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 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
} }
// Client represents etcd Client Configuration // Client represents etcd client 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, Endpoints etc. // NewClient create a client instance with name, endpoints 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 Client block connect to server") return nil, perrors.WithMessage(err, "new raw client 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, "Client keep session") return nil, perrors.WithMessage(err, "client 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 Client // close raw client
c.rawClient.Close() c.rawClient.Close()
// cancel ctx for raw Client // cancel ctx for raw client
c.cancel() c.cancel()
// clean raw Client // clean raw client
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 Client context // GetCtx return client context
func (c *Client) GetCtx() context.Context { func (c *Client) GetCtx() context.Context {
return c.ctx return c.ctx
} }
// Close close Client // Close close client
func (c *Client) Close() { func (c *Client) Close() {
if c == nil { if c == nil {
return return
} }
// stop the Client // stop the client
if ret := c.stop(); !ret { if ret := c.stop(); !ret {
return return
} }
// wait Client keep session stop // wait client 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 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 {
...@@ -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 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 {
select { select {
case <-c.Done(): case <-c.Done():
// Client be stopped, will clean the Client hold resources // client be stopped, will clean the client 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 Client, trigger reconnect to etcd // when connection lose, stop client, 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 Client //GetRawClient return etcd raw client
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 Endpoints //GetEndPoints return etcd endpoints
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 Client // Valid check client
func (c *Client) Valid() bool { func (c *Client) Valid() bool {
select { select {
case <-c.exit: case <-c.exit:
......
...@@ -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: "Name", v: "scott.wang"}}, }{k: "name", 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 = "Name" const prefix = "name"
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 Client for suite // set up a client 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("Client is not valid") t.Fatal("client is not valid")
} }
c.Close() c.Close()
if suite.client.Valid() != false { if suite.client.Valid() != false {
t.Fatal("Client is valid") t.Fatal("client 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("Client should be invalid then") suite.T().Fatal("client should be invalid then")
} }
} }
......
...@@ -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 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 etcd server Name //Name etcd server name
Name string Name string
//Endpoints etcd Endpoints //Endpoints etcd endpoints
Endpoints []string Endpoints []string
//Client etcd Client //Client etcd client
Client *Client Client *Client
//Timeout Timeout //Timeout timeout
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 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