Skip to content

Commit fd7dc02

Browse files
anrsanrs
and
anrs
authored
uses the upstream ctx (#464)
Co-authored-by: anrs <anders.hu@shopee.com>
1 parent 2e9e19f commit fd7dc02

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

store/etcdv3/meta/ephemeral.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (e *ETCD) StartEphemeral(ctx context.Context, path string, heartbeat time.D
2828
return nil, nil, errors.Wrap(types.ErrKeyExists, path)
2929
}
3030

31-
ctx, cancel := context.WithCancel(context.Background())
31+
ctx, cancel := context.WithCancel(ctx)
3232
expiry := make(chan struct{})
3333

3434
var wg sync.WaitGroup
@@ -40,28 +40,24 @@ func (e *ETCD) StartEphemeral(ctx context.Context, path string, heartbeat time.D
4040
tick := time.NewTicker(heartbeat / 3)
4141
defer tick.Stop()
4242

43-
revoke := func() {
44-
cctx, ccancel := context.WithTimeout(context.Background(), time.Minute) // todo minute sucks
45-
defer ccancel()
46-
if _, err := e.cliv3.Revoke(cctx, lease.ID); err != nil {
43+
// Revokes the lease.
44+
defer func() {
45+
// It shouldn't be inheriting from the ctx.
46+
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
47+
defer cancel()
48+
if _, err := e.cliv3.Revoke(ctx, lease.ID); err != nil {
4749
log.Errorf(ctx, "[StartEphemeral] revoke %d with %s failed: %v", lease.ID, path, err)
4850
}
49-
}
51+
}()
5052

5153
for {
5254
select {
5355
case <-tick.C:
54-
cctx, ccancel := context.WithTimeout(ctx, time.Minute) // todo minute sucks
55-
if _, err := e.cliv3.KeepAliveOnce(cctx, lease.ID); err != nil {
56+
if _, err := e.cliv3.KeepAliveOnce(ctx, lease.ID); err != nil {
5657
log.Errorf(ctx, "[StartEphemeral] keepalive %d with %s failed: %v", lease.ID, path, err)
57-
ccancel()
58-
revoke()
5958
return
6059
}
61-
ccancel()
62-
6360
case <-ctx.Done():
64-
revoke()
6561
return
6662
}
6763
}

0 commit comments

Comments
 (0)