Skip to content

Commit

Permalink
Add support for pointer to retry.StopError
Browse files Browse the repository at this point in the history
This commit removed the deprecated WithTransaction family and retry
construct. If you rely on the retry functionality, use the
github.com/arsham/retry package instead.
Support for go < 1.15 is removed.
  • Loading branch information
arsham committed Mar 4, 2021
1 parent 73b50ca commit 4973b19
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 660 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ linters:
- dogsled
- depguard
- dupl
- errorlint
- gocognit
- goconst
- gocritic
Expand All @@ -90,10 +91,13 @@ linters:
- unparam
- unused
- whitespace
- wrapcheck
- tparallel


# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.30.x
golangci-lint-version: 1.37.x
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ env:
- GO111MODULE=on

go:
- 1.14.x
- 1.15.x
- 1.16.x
- tip

matrix:
allow_failures:
- go: tip

before_install:
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.37.1

script:
- make ci_tests
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@



<a name="v0.5.0"></a>
## [v0.5.0] - 2021-03-04

### Added
- Add support for pointer to retry.StopError


<a name="v0.4.1"></a>
## [v0.4.1] - 2020-08-16

Expand Down Expand Up @@ -58,7 +65,8 @@



[Unreleased]: https://github.com/arsham/dbtools/compare/v0.4.1...HEAD
[Unreleased]: https://github.com/arsham/dbtools/compare/v0.5.0...HEAD
[v0.5.0]: https://github.com/arsham/dbtools/compare/v0.4.1...v0.5.0
[v0.4.1]: https://github.com/arsham/dbtools/compare/v0.4.0...v0.4.1
[v0.4.0]: https://github.com/arsham/dbtools/compare/v0.3.2...v0.4.0
[v0.3.2]: https://github.com/arsham/dbtools/compare/v0.3.1...v0.3.2
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tests: ## Run unit tests in watch mode. You can set: [run, timeout, short, dir,
.PHONY: dependencies
dependencies: ## Install dependencies requried for development operations.
@go get -u github.com/cespare/reflex
@go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.30.0
@go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.37.1
@go get -u github.com/git-chglog/git-chglog/cmd/git-chglog
@go get github.com/stretchr/testify/mock
@go get github.com/vektra/mockery/.../
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
[![Build Status](https://travis-ci.org/arsham/dbtools.svg?branch=master)](https://travis-ci.org/arsham/dbtools)
[![Coverage Status](https://codecov.io/gh/arsham/dbtools/branch/master/graph/badge.svg)](https://codecov.io/gh/arsham/dbtools)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Go Report Card](https://goreportcard.com/badge/github.com/arsham/dbtools)](https://goreportcard.com/report/github.com/arsham/dbtools)

This library contains goroutine safe helpers for retrying transactions until
they succeed and handles errors in a developer friendly way. There are helpers
for using with [go-sqlmock][go-sqlmock] in tests. There is also a `Mocha`
inspired reporter for [spec BDD library][spec].

This library supports `Go >= 1.14`.
This library supports `Go >= 1.15`.

1. [Transaction](#transaction)
* [PGX Pool](#pgx-pool)
Expand Down
91 changes: 14 additions & 77 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dbtools
import (
"context"
"database/sql"
"io"
"time"

"github.com/arsham/retry"
Expand Down Expand Up @@ -87,7 +86,7 @@ func (t *Transaction) PGX(ctx context.Context, transactions ...func(pgx.Tx) erro
select {
case <-ctx.Done():
e := errors.Wrap(tx.Rollback(ctx), "rolling back transaction")
return retry.StopError{
return &retry.StopError{
Err: multierror.Append(ctx.Err(), e).ErrorOrNil(),
}
default:
Expand All @@ -104,8 +103,12 @@ func (t *Transaction) PGX(ctx context.Context, transactions ...func(pgx.Tx) erro
if err != nil {
e := errors.Wrap(tx.Rollback(ctx), "rolling back transaction")
e = multierror.Append(err, e).ErrorOrNil()
if _, ok := err.(retry.StopError); ok {
e = retry.StopError{Err: e}
var (
v1 retry.StopError
v2 *retry.StopError
)
if errors.As(err, &v1) || errors.As(err, &v2) {
e = &retry.StopError{Err: e}
}
return e
}
Expand All @@ -128,7 +131,7 @@ func (t *Transaction) DB(ctx context.Context, transactions ...func(Tx) error) er
select {
case <-ctx.Done():
e := errors.Wrap(tx.Rollback(), "rolling back transaction")
return retry.StopError{
return &retry.StopError{
Err: multierror.Append(ctx.Err(), e).ErrorOrNil(),
}
default:
Expand All @@ -145,82 +148,16 @@ func (t *Transaction) DB(ctx context.Context, transactions ...func(Tx) error) er
if err != nil {
e := errors.Wrap(tx.Rollback(), "rolling back transaction")
e = multierror.Append(err, e).ErrorOrNil()
if _, ok := err.(retry.StopError); ok {
e = retry.StopError{Err: e}
var (
v1 retry.StopError
v2 *retry.StopError
)
if errors.As(err, &v1) || errors.As(err, &v2) {
e = &retry.StopError{Err: e}
}
return e
}
}
return errors.Wrap(tx.Commit(), "committing transaction")
})
}

// WithTransaction creates a transaction on db and uses it to call fn functions
// one by one. The first function that returns an error or panics will cause the
// loop to stop and transaction to be rolled back.
// Deprecated: use Transaction instead.
func WithTransaction(db *sql.DB, fn ...func(*sql.Tx) error) error {
tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "starting transaction")
}
for _, f := range fn {
var err error
func() {
defer func() {
if r := recover(); r != nil {
err = errors.Wrapf(errPanic, "%v", r)
}
}()
err = f(tx)
}()
if err != nil {
e := errors.Wrap(tx.Rollback(), "rolling back transaction")
return multierror.Append(err, e).ErrorOrNil()
}
}
return errors.Wrap(tx.Commit(), "committing transaction")
}

// Retry calls fn for retries times until it returns nil. If retries is zero fn
// would not be called. It delays and retries if the function returns any errors
// or panics. The fn function receives the current iteration as its argument.
// Deprecated: use http://github.com/arsham/retry library instead.
func Retry(retries int, delay time.Duration, fn func(int) error) error {
var err error
for i := 0; i < retries; i++ {
func() {
defer func() {
if r := recover(); r != nil {
err = errors.Wrapf(errPanic, "%v", r)
}
}()
err = fn(i)
}()
switch err {
case io.EOF, nil:
return nil
}
time.Sleep(delay * time.Duration(i+1))
}
return err
}

// RetryTransaction combines WithTransaction and Retry calls. It stops the call
// if context is times out or cancelled.
// Deprecated: use Transaction instead.
func RetryTransaction(ctx context.Context, db *sql.DB, retries int, delay time.Duration, fn ...func(*sql.Tx) error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return Retry(retries, delay, func(int) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return WithTransaction(db, fn...)
})
}
2 changes: 1 addition & 1 deletion db_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func ExampleTransaction_PGX_stopTrying() {
return nil
}, func(pgx.Tx) error {
fmt.Println("Running second query.")
return retry.StopError{Err: assert.AnError}
return &retry.StopError{Err: assert.AnError}
})
fmt.Printf("Transaction returns my error: %t", strings.Contains(err.Error(), assert.AnError.Error()))

Expand Down
Loading

0 comments on commit 4973b19

Please sign in to comment.