added etcd kv
This commit is contained in:
parent
ce7d138c60
commit
fd9f8fc1fd
8 changed files with 54 additions and 71 deletions
|
|
@ -73,16 +73,18 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
c.session = true
|
||||
|
||||
prefix := cfg.Prefix + "/" + cfg.ClusterName
|
||||
c.kvPrefix = prefix + "/kv"
|
||||
ePrefix := prefix + "/e"
|
||||
c.id = fmt.Sprintf("%x", s.Lease())
|
||||
c.keyID = fmt.Sprintf("%s/%s", prefix, c.id)
|
||||
c.keyID = ePrefix + "/" + c.id
|
||||
|
||||
e := concurrency.NewElection(s, prefix)
|
||||
e := concurrency.NewElection(s, ePrefix)
|
||||
ctxCampaign, cancelCampaign := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
errc := make(chan error, 1)
|
||||
for {
|
||||
go func() {
|
||||
errc <- e.Campaign(ctxCampaign, "leader")
|
||||
errc <- e.Campaign(ctxCampaign, "")
|
||||
}()
|
||||
select {
|
||||
case err := <-errc:
|
||||
|
|
@ -141,12 +143,16 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
e.Resign(ctxResign)
|
||||
case <-c.stop:
|
||||
t.Stop()
|
||||
cancel()
|
||||
cancelObserve()
|
||||
cancelCampaign()
|
||||
cancel()
|
||||
if leading {
|
||||
ctxResign, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||
e.Resign(ctxResign)
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||
e.Resign(ctx)
|
||||
}
|
||||
if c.callbacks.Cleanup != nil {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||
c.callbacks.Cleanup(ctx, c)
|
||||
}
|
||||
s.Close()
|
||||
close(c.done)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue