mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
48 lines
835 B
Go
48 lines
835 B
Go
|
package generic
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
)
|
||
|
|
||
|
type hTransactionKey struct{}
|
||
|
|
||
|
type HandlerTransaction struct {
|
||
|
context.Context
|
||
|
parent context.Context
|
||
|
done chan struct{}
|
||
|
result bool
|
||
|
}
|
||
|
|
||
|
func (h *HandlerTransaction) shouldContinue() bool {
|
||
|
select {
|
||
|
case <-h.parent.Done():
|
||
|
return false
|
||
|
case <-h.done:
|
||
|
return h.result
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (h *HandlerTransaction) Commit() {
|
||
|
h.result = true
|
||
|
close(h.done)
|
||
|
}
|
||
|
|
||
|
func (h *HandlerTransaction) Rollback() {
|
||
|
close(h.done)
|
||
|
}
|
||
|
|
||
|
func NewHandlerTransaction(ctx context.Context) *HandlerTransaction {
|
||
|
ht := &HandlerTransaction{
|
||
|
parent: ctx,
|
||
|
done: make(chan struct{}),
|
||
|
}
|
||
|
ctx = context.WithValue(ctx, hTransactionKey{}, ht)
|
||
|
ht.Context = ctx
|
||
|
return ht
|
||
|
}
|
||
|
|
||
|
func getHandlerTransaction(ctx context.Context) *HandlerTransaction {
|
||
|
v, _ := ctx.Value(hTransactionKey{}).(*HandlerTransaction)
|
||
|
return v
|
||
|
}
|