@@ -28,7 +28,7 @@ func (e *ETCD) StartEphemeral(ctx context.Context, path string, heartbeat time.D
28
28
return nil , nil , errors .Wrap (types .ErrKeyExists , path )
29
29
}
30
30
31
- ctx , cancel := context .WithCancel (context . Background () )
31
+ ctx , cancel := context .WithCancel (ctx )
32
32
expiry := make (chan struct {})
33
33
34
34
var wg sync.WaitGroup
@@ -40,28 +40,24 @@ func (e *ETCD) StartEphemeral(ctx context.Context, path string, heartbeat time.D
40
40
tick := time .NewTicker (heartbeat / 3 )
41
41
defer tick .Stop ()
42
42
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 {
47
49
log .Errorf (ctx , "[StartEphemeral] revoke %d with %s failed: %v" , lease .ID , path , err )
48
50
}
49
- }
51
+ }()
50
52
51
53
for {
52
54
select {
53
55
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 {
56
57
log .Errorf (ctx , "[StartEphemeral] keepalive %d with %s failed: %v" , lease .ID , path , err )
57
- ccancel ()
58
- revoke ()
59
58
return
60
59
}
61
- ccancel ()
62
-
63
60
case <- ctx .Done ():
64
- revoke ()
65
61
return
66
62
}
67
63
}
0 commit comments