improve code quality

This commit is contained in:
Astone
2019-12-03 15:25:58 +08:00
parent b5d65305db
commit 29fb58db39
20 changed files with 67 additions and 89 deletions

View File

@@ -39,9 +39,7 @@ func (e *etcdLeader) Elect(id string, opts ...leader.ElectOption) (leader.Electe
l := cc.NewElection(s, path)
ctx, _ := context.WithCancel(context.Background())
if err := l.Campaign(ctx, id); err != nil {
if err := l.Campaign(context.TODO(), id); err != nil {
return nil, err
}
@@ -63,14 +61,8 @@ func (e *etcdLeader) Follow() chan string {
ech := l.Observe(context.Background())
go func() {
for {
select {
case r, ok := <-ech:
if !ok {
return
}
ch <- string(r.Kvs[0].Value)
}
for r := range ech {
ch <- string(r.Kvs[0].Value)
}
}()
@@ -82,8 +74,7 @@ func (e *etcdLeader) String() string {
}
func (e *etcdElected) Reelect() error {
ctx, _ := context.WithCancel(context.Background())
return e.e.Campaign(ctx, e.id)
return e.e.Campaign(context.TODO(), e.id)
}
func (e *etcdElected) Revoked() chan bool {

View File

@@ -49,9 +49,7 @@ func (e *etcdLock) Acquire(id string, opts ...lock.AcquireOption) error {
m := cc.NewMutex(s, path)
ctx, _ := context.WithCancel(context.Background())
if err := m.Lock(ctx); err != nil {
if err := m.Lock(context.TODO()); err != nil {
return err
}

View File

@@ -63,7 +63,9 @@ func (s Schedule) Run() <-chan time.Time {
}
// start ticker
for t := range time.Tick(s.Interval) {
ticker := time.NewTicker(s.Interval)
defer ticker.Stop()
for t := range ticker.C {
ch <- t
}
}()