mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-24 17:35:01 +01:00
b2c6e51a43
This was merged to master prematurely as the job components are not decoupled well enough
for these signals to be useful yet.
This reverts commit 2c8c2cfa14
.
closes #452
36 lines
610 B
Go
36 lines
610 B
Go
package wakeup
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
type contextKey int
|
|
|
|
const contextKeyWakeup contextKey = iota
|
|
|
|
func Wait(ctx context.Context) <-chan struct{} {
|
|
wc, ok := ctx.Value(contextKeyWakeup).(chan struct{})
|
|
if !ok {
|
|
wc = make(chan struct{})
|
|
}
|
|
return wc
|
|
}
|
|
|
|
type Func func() error
|
|
|
|
var AlreadyWokenUp = errors.New("already woken up")
|
|
|
|
func Context(ctx context.Context) (context.Context, Func) {
|
|
wc := make(chan struct{})
|
|
wuf := func() error {
|
|
select {
|
|
case wc <- struct{}{}:
|
|
return nil
|
|
default:
|
|
return AlreadyWokenUp
|
|
}
|
|
}
|
|
return context.WithValue(ctx, contextKeyWakeup, wc), wuf
|
|
}
|