修复一次行监听为取消问题

This commit is contained in:
白茶清欢 2021-11-24 00:08:14 +08:00
parent 43cf6f27c9
commit c21e16138d

View File

@ -24,13 +24,18 @@ func LeaseOnce(ctx context.Context, key string, val string, ttl int64) error {
if ttl <= 0 { if ttl <= 0 {
return errors.New("lease time must be more than 0") return errors.New("lease time must be more than 0")
} }
if nil == ctx { if nil == ctx {
ctx = context.TODO() ctx = context.Background()
} }
var ( var (
resp *clientv3.LeaseGrantResponse resp *clientv3.LeaseGrantResponse
err error err error
cancelFunc context.CancelFunc
) )
ctx, cancelFunc = context.WithCancel(ctx)
defer cancelFunc()
// 创建一个5秒的租约 // 创建一个5秒的租约
if resp, err = Client.Grant(ctx, ttl); err != nil { if resp, err = Client.Grant(ctx, ttl); err != nil {
return errors.New("lease grant error : " + err.Error()) return errors.New("lease grant error : " + err.Error())