mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-29 03:45:27 +01:00
36 lines
636 B
Go
36 lines
636 B
Go
package dosnapshot
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
type contextKey int
|
|
|
|
const contextKeyDosnapshot contextKey = iota
|
|
|
|
func Wait(ctx context.Context) <-chan struct{} {
|
|
wc, ok := ctx.Value(contextKeyDosnapshot).(chan struct{})
|
|
if !ok {
|
|
wc = make(chan struct{})
|
|
}
|
|
return wc
|
|
}
|
|
|
|
type Func func() error
|
|
|
|
var AlreadyDosnapshot = errors.New("already snapshotting")
|
|
|
|
func Context(ctx context.Context) (context.Context, Func) {
|
|
wc := make(chan struct{})
|
|
wuf := func() error {
|
|
select {
|
|
case wc <- struct{}{}:
|
|
return nil
|
|
default:
|
|
return AlreadyDosnapshot
|
|
}
|
|
}
|
|
return context.WithValue(ctx, contextKeyDosnapshot, wc), wuf
|
|
}
|