Skip to content

Commit

Permalink
pgctx: add IsInTx function
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Aug 20, 2019
1 parent 78bc581 commit 0f877f6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pgctx/pgctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ func RunInTx(ctx context.Context, f func(ctx context.Context) error) error {
return nil
}

// IsInTx checks is context inside RunInTx
func IsInTx(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyQueryer{}).(*sql.Tx)
return ok
}

// Committed calls f after committed or immediate if not in tx
func Committed(ctx context.Context, f func(ctx context.Context)) {
// check is in tx ?
if _, ok := ctx.Value(ctxKeyQueryer{}).(*sql.Tx); !ok {
f(ctx)
if f == nil {
return
}

if f == nil {
if !IsInTx(ctx) {
f(ctx)
return
}

Expand Down

0 comments on commit 0f877f6

Please sign in to comment.