func (p *EtcdRegisterPlugin) Start() error {
	if p.done == nil {
		p.done = make(chan struct{})
	}
	if p.dying == nil {
		p.dying = make(chan struct{})
	}
	if p.kv == nil {
		kv, err := libkv.NewStore(store.ETCD, p.EtcdServers, p.Options)
		if err != nil {
			log.Errorf("cannot create etcd registry: %v", err)
			return err
		}
		p.kv = kv
	}
	err := p.kv.Put(p.BasePath, []byte("rpcx_path"), &store.WriteOptions{IsDir: true})
	if err != nil && !strings.Contains(err.Error(), "Not a file") {
		log.Errorf("cannot create etcd path %s: %v", p.BasePath, err)
		return err
	}
	if p.UpdateInterval > 0 {
		ticker := time.NewTicker(p.UpdateInterval)
		go func() {
			defer p.kv.Close()
			for {
				select {
				case <-p.dying:
					close(p.done)
					return
				case <-ticker.C:
					extra := make(map[string]string)
					if p.Metrics != nil {
						extra["calls"] = fmt.Sprintf("%.2f", metrics.GetOrRegisterMeter("calls", p.Metrics).RateMean())
						extra["connections"] = fmt.Sprintf("%.2f", metrics.GetOrRegisterMeter("connections", p.Metrics).RateMean())
					}
					p.servicesLock.RLock()
					for _, name := range p.Services {
						nodePath := fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
						kvPair, err := p.kv.Get(nodePath)
						if err != nil {
							log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error())
							p.metasLock.RLock()
							meta := p.metas[name]
							p.metasLock.RUnlock()
							err = p.kv.Put(nodePath, []byte(meta), &store.WriteOptions{TTL: p.UpdateInterval * 3})
							if err != nil {
								log.Errorf("cannot re-create etcd path %s: %v", nodePath, err)
							}
						} else {
							v, _ := url.ParseQuery(string(kvPair.Value))
							for key, value := range extra {
								v.Set(key, value)
							}
							p.kv.Put(nodePath, []byte(v.Encode()), &store.WriteOptions{TTL: p.UpdateInterval * 3})
						}
					}
					p.servicesLock.RUnlock()
				}
			}
		}()
	}
	return nil
}
Start starts to connect etcd cluster
package serverplugin
import (
	"context"
	"errors"
	"fmt"
	"net"
	"net/url"
	"strings"
	"sync"
	"time"
	"github.com/docker/libkv"
	"github.com/docker/libkv/store"
	"github.com/docker/libkv/store/etcd"
	metrics "github.com/rcrowley/go-metrics"
	"github.com/kudoochui/kudos/rpcx/log"
)
func init() {
	etcd.Register()
}
type EtcdRegisterPlugin struct {
	ServiceAddress string
	EtcdServers []string
	BasePath string
	Metrics  metrics.Registry
	Services       []string
	servicesLock	sync.RWMutex
	metasLock      sync.RWMutex
	metas          map[string]string
	UpdateInterval time.Duration
	Options *store.Config
	kv      store.Store
	dying chan struct{}
	done  chan struct{}
}