2019-07-18 02:09:22 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2019-07-18 02:15:02 +00:00
|
|
|
"errors"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-07-18 02:09:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cancellation interface {
|
2019-07-18 02:15:02 +00:00
|
|
|
Finished() <-chan struct{}
|
|
|
|
Cancel(error) error
|
|
|
|
Error() error
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CancellationFinalizer(c Cancellation) {
|
2019-07-18 02:15:02 +00:00
|
|
|
c.Cancel(errors.New("finalizer called"))
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cancellation struct {
|
2019-07-18 02:15:02 +00:00
|
|
|
signal chan error
|
|
|
|
cancel chan struct{}
|
|
|
|
errMtx sync.RWMutex
|
|
|
|
err error
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cancellation) worker() {
|
2019-07-18 02:15:02 +00:00
|
|
|
// Launch this in a separate goroutine when creating a cancellation
|
|
|
|
err := <-c.signal
|
|
|
|
c.errMtx.Lock()
|
|
|
|
c.err = err
|
|
|
|
c.errMtx.Unlock()
|
|
|
|
close(c.cancel)
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCancellation() Cancellation {
|
2019-07-18 02:15:02 +00:00
|
|
|
c := cancellation{
|
|
|
|
signal: make(chan error),
|
|
|
|
cancel: make(chan struct{}),
|
|
|
|
}
|
|
|
|
runtime.SetFinalizer(&c, CancellationFinalizer)
|
|
|
|
go c.worker()
|
|
|
|
return &c
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cancellation) Finished() <-chan struct{} {
|
2019-07-18 02:15:02 +00:00
|
|
|
return c.cancel
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cancellation) Cancel(err error) error {
|
2019-07-18 02:15:02 +00:00
|
|
|
select {
|
|
|
|
case c.signal <- err:
|
|
|
|
return nil
|
|
|
|
case <-c.cancel:
|
|
|
|
return c.Error()
|
|
|
|
}
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cancellation) Error() error {
|
2019-07-18 02:15:02 +00:00
|
|
|
c.errMtx.RLock()
|
|
|
|
err := c.err
|
|
|
|
c.errMtx.RUnlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CancellationChild(parent Cancellation) Cancellation {
|
|
|
|
child := NewCancellation()
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-child.Finished():
|
|
|
|
case <-parent.Finished():
|
|
|
|
child.Cancel(parent.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return child
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CancellationWithTimeout(parent Cancellation, timeout time.Duration) Cancellation {
|
2019-07-18 02:15:02 +00:00
|
|
|
child := CancellationChild(parent)
|
|
|
|
go func() {
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
defer TimerStop(timer)
|
|
|
|
select {
|
|
|
|
case <-child.Finished():
|
|
|
|
case <-timer.C:
|
|
|
|
child.Cancel(errors.New("timeout"))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return child
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CancellationWithDeadline(parent Cancellation, deadline time.Time) Cancellation {
|
2019-07-18 02:15:02 +00:00
|
|
|
return CancellationWithTimeout(parent, deadline.Sub(time.Now()))
|
2019-07-18 02:09:22 +00:00
|
|
|
}
|