new logger and etcd fixes
This commit is contained in:
parent
ba001b4c20
commit
cbd4f38cca
11 changed files with 248 additions and 161 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/cluster"
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/config"
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/util"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
|
|
@ -60,10 +61,12 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
ctxSsession, cancelSession := context.WithCancel(context.Background())
|
||||
c.cancelSession = cancelSession
|
||||
log := c.Logger()
|
||||
log.Info("waiting for etcd")
|
||||
sessionCtx, sessionCancel := context.WithCancel(context.Background())
|
||||
c.sessionCancel = sessionCancel
|
||||
s, err := concurrency.NewSession(c.cli,
|
||||
concurrency.WithContext(ctxSsession),
|
||||
concurrency.WithContext(sessionCtx),
|
||||
concurrency.WithTTL(10),
|
||||
)
|
||||
if err != nil {
|
||||
|
|
@ -78,32 +81,33 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
c.keyID = ePrefix + "/" + c.id
|
||||
|
||||
e := concurrency.NewElection(s, ePrefix)
|
||||
ctxCampaign, cancelCampaign := context.WithCancel(context.Background())
|
||||
campaignCtx, campaignCancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
errc := make(chan error, 1)
|
||||
for {
|
||||
go func() {
|
||||
errc <- e.Campaign(ctxCampaign, "")
|
||||
errc <- e.Campaign(campaignCtx, "")
|
||||
}()
|
||||
select {
|
||||
case err := <-errc:
|
||||
if err != nil {
|
||||
time.Sleep(time.Second)
|
||||
log.Error(err, "campaign error")
|
||||
c.Fatal(cluster.ErrRestart)
|
||||
}
|
||||
case <-ctxCampaign.Done():
|
||||
case <-campaignCtx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
t := time.NewTicker(time.Second)
|
||||
t := time.NewTicker(time.Second * 2)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctxObserve, cancelObserve := context.WithCancel(context.Background())
|
||||
observeCtx, observeCancel := context.WithCancel(context.Background())
|
||||
leading := false
|
||||
following := false
|
||||
leaderChan := c.observe(ctxObserve, e.Observe(ctxObserve))
|
||||
leaderChan := c.observe(observeCtx, e.Observe(observeCtx))
|
||||
for {
|
||||
if !leading && c.leaderID(e.Leader(ctxObserve)) == c.keyID {
|
||||
if !leading && c.leaderID(e.Leader(observeCtx)) == c.keyID {
|
||||
leading = true
|
||||
go c.leader(ctx)
|
||||
} else if !following && !leading {
|
||||
|
|
@ -138,20 +142,19 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
case <-c.stepdown:
|
||||
cancel()
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
ctxResign, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||
e.Resign(ctxResign)
|
||||
resignCtx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||
e.Resign(resignCtx)
|
||||
case <-c.stop:
|
||||
t.Stop()
|
||||
cancel()
|
||||
cancelObserve()
|
||||
cancelCampaign()
|
||||
observeCancel()
|
||||
campaignCancel()
|
||||
if leading {
|
||||
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)
|
||||
c.callbacks.Cleanup(context.Background(), c)
|
||||
}
|
||||
s.Close()
|
||||
close(c.done)
|
||||
|
|
@ -204,9 +207,10 @@ func (c *Cluster) Fatal(err error) {
|
|||
|
||||
func (c *Cluster) Stop() {
|
||||
if !c.session {
|
||||
c.cancelSession()
|
||||
c.sessionCancel()
|
||||
return
|
||||
}
|
||||
c.stop <- struct{}{}
|
||||
close(c.stop)
|
||||
<-c.done
|
||||
c.cli.Close()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue