Skip to content

Commit

Permalink
comments and format
Browse files Browse the repository at this point in the history
  • Loading branch information
ivcosla committed Jun 23, 2019
1 parent 8b70b57 commit 684b5ff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
21 changes: 15 additions & 6 deletions internal/netutil/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@ import (
"github.com/prometheus/common/log"
)

var ErrMaximumRetriesReached = errors.New("maximum retries attempted without success")
// Package errors
var (
ErrMaximumRetriesReached = errors.New("maximum retries attempted without success")
)

// RetryFunc is a function used as argument of (*Retrier).Do(), which will retry on error unless it is whitelisted
type RetryFunc func() error

// Retrier holds a configuration for how retries should be performed
type Retrier struct {
exponentialBackoff time.Duration
exponentialFactor uint32 // multiplier for the backoff duration that is applied on every retry. If 0 the backoff is not increased during retries
times uint32 // number of times that the given function is going to be retried until success, if 0 it will be retried forever until success
exponentialBackoff time.Duration // multiplied on every retry by a exponentialFactor
exponentialFactor uint32 // multiplier for the backoff duration that is applied on every retry
times uint32 // number of times that the given function is going to be retried until success, if 0 it will be retried forever until success
errWhitelist map[error]struct{}
}

// NewRetrier returns a retrier that is ready to call Do() method
func NewRetrier(exponentialBackoff time.Duration, times, factor uint32) *Retrier {
return &Retrier{
exponentialBackoff: exponentialBackoff,
times: times,
times: times,
exponentialFactor: factor,
errWhitelist: make(map[error]struct{}),
}
}

// WithErrWhitelist sets a list of errors into the retrier, if the RetryFunc provided to Do() fails with one of them it will return inmediatelly with such error
func (r *Retrier) WithErrWhitelist(errors ...error) *Retrier {
m := make(map[error]struct{})
for _, err := range errors {
Expand All @@ -37,6 +44,8 @@ func (r *Retrier) WithErrWhitelist(errors ...error) *Retrier {
return r
}

// Do takes a RetryFunc and attempts to execute it, if it fails with an error it will be retried a maximum of given times with an exponentialBackoff, until it returns
// nil or an error that is whitelisted
func (r Retrier) Do(f RetryFunc) error {
if r.times == 0 {
return r.retryUntilSuccess(f)
Expand All @@ -48,7 +57,7 @@ func (r Retrier) Do(f RetryFunc) error {
func (r Retrier) retryNTimes(f RetryFunc) error {
currentBackoff := r.exponentialBackoff

for i:= uint32(0); i < r.times; i++ {
for i := uint32(0); i < r.times; i++ {
err := f()
if err != nil {
if r.isWhitelisted(err) {
Expand Down
2 changes: 1 addition & 1 deletion internal/netutil/retrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func TestRetrier_Do(t *testing.T) {
err := loopR.Do(f)
require.NoError(t, err)

require.Equal(t,threshold,c)
require.Equal(t, threshold, c)
})
}
3 changes: 2 additions & 1 deletion internal/therealproxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package therealproxy

import (
"fmt"
"github.com/skycoin/skywire/internal/netutil"
"io"
"log"
"net"
"time"

"github.com/skycoin/skywire/internal/netutil"

"github.com/hashicorp/yamux"
)

Expand Down
6 changes: 3 additions & 3 deletions internal/therealproxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package therealproxy

import (
"fmt"
"github.com/armon/go-socks5"
"github.com/hashicorp/yamux"
"log"
"net"
)

"github.com/armon/go-socks5"
"github.com/hashicorp/yamux"
)

// Server implements multiplexing proxy server using yamux.
type Server struct {
Expand Down

0 comments on commit 684b5ff

Please sign in to comment.