@@ -21,7 +21,7 @@ func (r *Rediaron) StartEphemeral(ctx context.Context, path string, heartbeat ti
21
21
return nil , nil , ErrAlreadyExists
22
22
}
23
23
24
- ctx , cancel := context .WithCancel (context .Background ())
24
+ cctx , cancel := context .WithCancel (context .Background ())
25
25
expiry := make (chan struct {})
26
26
27
27
var wg sync.WaitGroup
@@ -33,23 +33,15 @@ func (r *Rediaron) StartEphemeral(ctx context.Context, path string, heartbeat ti
33
33
tick := time .NewTicker (heartbeat / 3 )
34
34
defer tick .Stop ()
35
35
36
- revoke := func () {
37
- if _ , err := r .cli .Del (context .Background (), path ).Result (); err != nil {
38
- log .Errorf ("[StartEphemeral] revoke with %s failed: %v" , path , err )
39
- }
40
- }
41
-
42
36
for {
43
37
select {
44
38
case <- tick .C :
45
- if _ , err := r .cli .Expire (context .Background (), path , heartbeat ).Result (); err != nil {
46
- log .Errorf ("[StartEphemeral] keepalive with %s failed: %v" , path , err )
47
- revoke ()
39
+ if err := r .refreshEphemeral (path , heartbeat ); err != nil {
40
+ r .revokeEphemeral (path )
48
41
return
49
42
}
50
-
51
- case <- ctx .Done ():
52
- revoke ()
43
+ case <- cctx .Done ():
44
+ r .revokeEphemeral (path )
53
45
return
54
46
}
55
47
}
@@ -60,3 +52,18 @@ func (r *Rediaron) StartEphemeral(ctx context.Context, path string, heartbeat ti
60
52
wg .Wait ()
61
53
}, nil
62
54
}
55
+
56
+ func (r * Rediaron ) revokeEphemeral (path string ) {
57
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
58
+ defer cancel ()
59
+ if _ , err := r .cli .Del (ctx , path ).Result (); err != nil {
60
+ log .Errorf ("[refreshEphemeral] revoke with %s failed: %v" , path , err )
61
+ }
62
+ }
63
+
64
+ func (r * Rediaron ) refreshEphemeral (path string , ttl time.Duration ) error {
65
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
66
+ defer cancel ()
67
+ _ , err := r .cli .Expire (ctx , path , ttl ).Result ()
68
+ return err
69
+ }
0 commit comments