Skip to content

Commit

Permalink
Merge pull request #59 from olachat/yinloo/fix-lock
Browse files Browse the repository at this point in the history
ensure conn.Close is closed
  • Loading branch information
yinloo-ola authored Mar 13, 2024
2 parents 05ee64c + 9b0bf9c commit e44a3a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion coredb/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ func (t *TxProvider) Tx(ctx context.Context, fn func(TxContext) error) error {
}

func (t *TxProvider) TxWithLock(ctx context.Context, lock string, durationInSec int, fn func(txContext TxContext) error) error {
dbConn, err := t.conn.Conn(ctx)
connCtx, cancel := context.WithCancel(context.Background())
dbConn, err := t.conn.Conn(connCtx)
defer func() {
cancel()
if dbConn != nil {
dbConn.Close()
}
}()
if err != nil {
return fmt.Errorf("fail to get db connection: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/mitchellh/cli v1.1.4
github.com/pkg/errors v0.9.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
golang.org/x/text v0.7.0
)

Expand Down Expand Up @@ -60,7 +61,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/src-d/go-oniguruma v1.1.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/mod v0.7.0 // indirect
Expand Down

0 comments on commit e44a3a0

Please sign in to comment.