2022-09-19 04:53:06 +00:00
|
|
|
package txn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
|
|
|
hookManagerKey key = iota + 1
|
|
|
|
)
|
|
|
|
|
|
|
|
type hookManager struct {
|
2023-03-16 23:52:49 +00:00
|
|
|
preCommitHooks []TxnFunc
|
|
|
|
postCommitHooks []MustFunc
|
|
|
|
postRollbackHooks []MustFunc
|
|
|
|
postCompleteHooks []MustFunc
|
2022-09-19 04:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *hookManager) register(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, hookManagerKey, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hookManagerCtx(ctx context.Context) *hookManager {
|
|
|
|
m, ok := ctx.Value(hookManagerKey).(*hookManager)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func executeHooks(ctx context.Context, hooks []TxnFunc) error {
|
|
|
|
// we need to return the first error
|
2022-09-28 06:08:00 +00:00
|
|
|
for _, h := range hooks {
|
2023-03-16 23:52:49 +00:00
|
|
|
if err := h(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-19 04:53:06 +00:00
|
|
|
}
|
2023-03-16 23:52:49 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-19 04:53:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func executeMustHooks(ctx context.Context, hooks []MustFunc) {
|
|
|
|
for _, h := range hooks {
|
|
|
|
h(ctx)
|
|
|
|
}
|
2022-09-28 06:08:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func (m *hookManager) executePostCommitHooks(ctx context.Context) {
|
|
|
|
executeMustHooks(ctx, m.postCommitHooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *hookManager) executePostRollbackHooks(ctx context.Context) {
|
|
|
|
executeMustHooks(ctx, m.postRollbackHooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *hookManager) executePreCommitHooks(ctx context.Context) error {
|
|
|
|
return executeHooks(ctx, m.preCommitHooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *hookManager) executePostCompleteHooks(ctx context.Context) {
|
|
|
|
executeMustHooks(ctx, m.postCompleteHooks)
|
2022-09-28 06:08:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func AddPreCommitHook(ctx context.Context, hook TxnFunc) {
|
2022-09-28 06:08:00 +00:00
|
|
|
m := hookManagerCtx(ctx)
|
2023-03-16 23:52:49 +00:00
|
|
|
m.preCommitHooks = append(m.preCommitHooks, hook)
|
2022-09-19 04:53:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func AddPostCommitHook(ctx context.Context, hook MustFunc) {
|
2022-09-19 04:53:06 +00:00
|
|
|
m := hookManagerCtx(ctx)
|
|
|
|
m.postCommitHooks = append(m.postCommitHooks, hook)
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func AddPostRollbackHook(ctx context.Context, hook MustFunc) {
|
2022-09-19 04:53:06 +00:00
|
|
|
m := hookManagerCtx(ctx)
|
|
|
|
m.postRollbackHooks = append(m.postRollbackHooks, hook)
|
|
|
|
}
|
2022-09-28 06:08:00 +00:00
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func AddPostCompleteHook(ctx context.Context, hook MustFunc) {
|
2022-09-28 06:08:00 +00:00
|
|
|
m := hookManagerCtx(ctx)
|
|
|
|
m.postCompleteHooks = append(m.postCompleteHooks, hook)
|
|
|
|
}
|