执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Run remains synchronous: cancellation is a request, not proof that the last
|
||||
// network write has stopped. The caller may release ownership only after run
|
||||
// and its cleanup have actually returned, and the renewal goroutine is joined.
|
||||
func runWithLibraryLease(parent context.Context, interval time.Duration, renew func(context.Context) error, run func(context.Context) error) error {
|
||||
ctx, cancel := context.WithCancelCause(parent)
|
||||
defer cancel(nil)
|
||||
stop := make(chan struct{})
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
done <- nil
|
||||
return
|
||||
case <-ctx.Done():
|
||||
done <- context.Cause(ctx)
|
||||
return
|
||||
case <-ticker.C:
|
||||
bounded, finish := context.WithTimeout(ctx, interval)
|
||||
err := renew(bounded)
|
||||
finish()
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
err := run(ctx)
|
||||
close(stop)
|
||||
leaseErr := <-done
|
||||
return errors.Join(err, leaseErr, context.Cause(ctx))
|
||||
}
|
||||
Reference in New Issue
Block a user