added etcd kv

This commit is contained in:
ston1th 2020-11-15 22:02:23 +01:00
commit fd9f8fc1fd
8 changed files with 54 additions and 71 deletions

View file

@ -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)