Skip to content

Commit 656c942

Browse files
authored
remove background context (#377)
1 parent 4a39c3c commit 656c942

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

store/redis/ephemeral.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (r *Rediaron) StartEphemeral(ctx context.Context, path string, heartbeat ti
2121
return nil, nil, ErrAlreadyExists
2222
}
2323

24-
ctx, cancel := context.WithCancel(context.Background())
24+
cctx, cancel := context.WithCancel(context.Background())
2525
expiry := make(chan struct{})
2626

2727
var wg sync.WaitGroup
@@ -33,23 +33,15 @@ func (r *Rediaron) StartEphemeral(ctx context.Context, path string, heartbeat ti
3333
tick := time.NewTicker(heartbeat / 3)
3434
defer tick.Stop()
3535

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-
4236
for {
4337
select {
4438
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)
4841
return
4942
}
50-
51-
case <-ctx.Done():
52-
revoke()
43+
case <-cctx.Done():
44+
r.revokeEphemeral(path)
5345
return
5446
}
5547
}
@@ -60,3 +52,18 @@ func (r *Rediaron) StartEphemeral(ctx context.Context, path string, heartbeat ti
6052
wg.Wait()
6153
}, nil
6254
}
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

Comments
 (0)