Skip to content

Commit

Permalink
move examples and experimentals to submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Feb 23, 2021
1 parent e372cc3 commit 0f199af
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 33 deletions.
7 changes: 7 additions & 0 deletions docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Package retry provides the most advanced interruptible mechanism
// to perform actions repetitively until successful.
//
// The retry based on https://github.com/Rican7/retry but fully reworked
// and focused on integration with the https://github.com/kamilsk/breaker
// and the built-in https://pkg.go.dev/context package.
package retry
20 changes: 10 additions & 10 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ func (err Error) Error() string { return string(err) }
// Unwrap always returns nil means that an error doesn't have other root cause.
func (err Error) Unwrap() error { return nil }

// equal to go.octolab.org/errors.Unwrap
func unwrap(err error) error {
// compatible with github.com/pkg/errors
type causer interface {
Cause() error
}
// compatible with built-in errors since 1.13
type wrapper interface {
Unwrap() error
}

for err != nil {
layer, is := err.(wrapper)
if is {
Expand All @@ -37,3 +27,13 @@ func unwrap(err error) error {
}
return err
}

// compatible with github.com/pkg/errors
type causer interface {
Cause() error
}

// compatible with built-in errors since 1.13
type wrapper interface {
Unwrap() error
}
10 changes: 5 additions & 5 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ func TestError(t *testing.T) {
}

func TestUnwrap(t *testing.T) {
cause := errors.New("root")
core := unwrap(causer{layer{cause}})
if !reflect.DeepEqual(core, cause) {
root := errors.New("root")
core := unwrap(cause{layer{root}})
if !reflect.DeepEqual(core, root) {
t.Error("unexpected behavior")
}
}

// helpers

type causer struct{ error }
type cause struct{ error }

func (causer causer) Cause() error { return causer.error }
func (cause cause) Cause() error { return cause.error }

type layer struct{ error }

Expand Down
2 changes: 1 addition & 1 deletion compare_test.go → examples/compare_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package retry_test
package examples_test

import (
"context"
Expand Down
6 changes: 6 additions & 0 deletions examples/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Package examples contains extended documentation
// for github.com/kamilsk/retry/v5 module.
//
// It contains examples of usage with additional
// dependencies that are not needed by the module.
package examples
7 changes: 7 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/kamilsk/retry/examples

go 1.13

require github.com/kamilsk/retry/v5 v5.0.0-rc8

replace github.com/kamilsk/retry/v5 => ../
1 change: 1 addition & 0 deletions examples/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package examples_test
5 changes: 0 additions & 5 deletions retry.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// Package retry provides the most advanced interruptible mechanism
// to perform actions repetitively until successful.
// The retry based on https://github.com/Rican7/retry but fully reworked
// and focused on integration with the https://github.com/kamilsk/breaker
// and the built-in https://pkg.go.dev/context package.
package retry

import (
Expand Down
3 changes: 3 additions & 0 deletions sandbox/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package exp contains experimental unstable
// features.
package sandbox
18 changes: 11 additions & 7 deletions example_test.go → sandbox/example_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package retry_test
// +build go1.13

package sandbox_test

import (
"context"
"database/sql"
"errors"
"fmt"
"math/rand"
"net"
"time"

"github.com/kamilsk/retry/sandbox"

"github.com/kamilsk/retry/v5"
"github.com/kamilsk/retry/v5/backoff"
"github.com/kamilsk/retry/v5/exp"
"github.com/kamilsk/retry/v5/jitter"
"github.com/kamilsk/retry/v5/strategy"
)
Expand All @@ -31,8 +35,8 @@ func Example() {
),

// experimental
exp.CheckError(
exp.NetworkError(exp.Skip),
sandbox.CheckError(
sandbox.NetworkError(sandbox.Skip),
DatabaseError(),
),
}
Expand Down Expand Up @@ -60,10 +64,10 @@ func SendRequest(ctx context.Context) error {
}

func DatabaseError() func(error) bool {
blacklist := []error{sql.ErrNoRows, sql.ErrConnDone, sql.ErrTxDone}
deprecated := []error{sql.ErrNoRows, sql.ErrConnDone, sql.ErrTxDone}
return func(err error) bool {
for _, preset := range blacklist {
if err == preset {
for _, deprecated := range deprecated {
if errors.Is(err, deprecated) {
return false
}
}
Expand Down
7 changes: 7 additions & 0 deletions sandbox/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/kamilsk/retry/sandbox

go 1.11

require github.com/kamilsk/retry/v5 v5.0.0-rc8

replace github.com/kamilsk/retry/v5 => ../
4 changes: 2 additions & 2 deletions exp/experimental.go → sandbox/strategy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exp
package sandbox

import "net"

Expand All @@ -9,7 +9,7 @@ const (

// A Breaker carries a cancellation signal to interrupt an action execution.
//
// It is a subset of the built-in Context and github.com/kamilsk/breaker interfaces.
// It is a subset of the built-in context and github.com/kamilsk/breaker interfaces.
type Breaker = interface {
// Done returns a channel that's closed when a cancellation signal occurred.
Done() <-chan struct{}
Expand Down
4 changes: 2 additions & 2 deletions exp/experimental_test.go → sandbox/strategy_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exp_test
package sandbox_test

import (
"context"
Expand All @@ -7,7 +7,7 @@ import (
"net"
"testing"

. "github.com/kamilsk/retry/v5/exp"
. "github.com/kamilsk/retry/sandbox"
)

func TestCheckError(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion strategy/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "time"

// A Breaker carries a cancellation signal to interrupt an action execution.
//
// It is a subset of the built-in Context and github.com/kamilsk/breaker interfaces.
// It is a subset of the built-in context and github.com/kamilsk/breaker interfaces.
type Breaker = interface {
// Done returns a channel that's closed when a cancellation signal occurred.
Done() <-chan struct{}
Expand Down

0 comments on commit 0f199af

Please sign in to comment.