Skip to content

Commit

Permalink
[#8] - WaitTime is now a time.Duration instead of an int64
Browse files Browse the repository at this point in the history
  • Loading branch information
J7mbo committed Apr 10, 2019
1 parent 6630c4b commit a45a21d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
11 changes: 7 additions & 4 deletions MaxRetriesError.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package MethodCallRetrier

import "fmt"
import (
"fmt"
"time"
)

/* Custom error for differentiation. */
/* MaxRetriesError is a custom error for differentiation. */
type MaxRetriesError struct {
methodName string
waitTime int64
waitTime time.Duration
maxRetries int64
}

/* Create a new instance of MaxRetriesError. */
/* Error is for implementing the error interface. */
func (e *MaxRetriesError) Error() string {
return fmt.Sprintf(
"Tried calling: '%s' %d times but reached max retries of: %d", e.methodName, e.waitTime, e.maxRetries,
Expand Down
28 changes: 14 additions & 14 deletions MethodCallRetrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import (
"time"
)

/* Handles the retrying of a function call when an error is given up to X times - useful for http requests. */
/* MethodCallRetrier handles the retrying of a function when an error is given up to X times - eg. for http requests. */
type MethodCallRetrier struct {
/* Wait time in seconds between each unsuccessful call. */
waitTime int64
/* waitTime is the wait time in seconds between each unsuccessful call. */
waitTime time.Duration

/* Maximum number of retries to attempt before returning an error. */
/* maxRetries is the maximum number of retries to attempt before returning an error. */
maxRetries int64

/* Useful for incremental increases in the sleep time. Defaults to no exponent. */
/* exponent is useful for incremental increases in the sleep time. Defaults to no exponent increase. */
exponent int64

/* Store the current number of retries; is always reset after ExecuteWithRetry() has finished. */
/* currentRetries stores the current number of retries; is always reset after ExecuteWithRetry() has finished. */
currentRetries int64

/* Store the errors retrieved as they may be different on each subsequent retry. */
/* errorList stores the errors retrieved as they may be different on each subsequent retry. */
errorList []error
}

/* New returns a new MethodCallRetrier. */
func New(waitTime int64, maxRetries int64, exponent int64) *MethodCallRetrier {
func New(waitTime time.Duration, maxRetries int64, exponent int64) *MethodCallRetrier {
if exponent < 1 {
exponent = 1
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (r *MethodCallRetrier) ExecuteFuncWithRetry(function func() error) []error
return r.errorList
}

/* Retries the call to object.methodName(...args) with a maximum number of retries and a wait time. */
/* ExecuteWithRetry retries the call to object.methodName(...args) with a maximum number of retries and a wait time. */
func (r *MethodCallRetrier) ExecuteWithRetry(
object interface{}, methodName string, args ...interface{},
) ([]reflect.Value, []error) {
Expand Down Expand Up @@ -169,26 +169,26 @@ func calculateJitter(waitTime time.Duration) time.Duration {
return waitTime + jitter / 2
}

/* If it's a pointer, we need to call the concrete instead */
/* objectIsAPointer decides whether or not an object is a pointer and so we would need to call the concrete instead. */
func objectIsAPointer(object interface{}) bool {
return reflect.ValueOf(object).Kind() == reflect.Ptr
}

/* Sleep for the given wait time and increment the retry count by 1. */
/* sleepAndIncrementRetries sleeps for the given wait time and increments the retry count by 1. */
func (r *MethodCallRetrier) sleepAndIncrementRetries() {
time.Sleep(calculateJitter(time.Duration(r.waitTime) * time.Second))

r.waitTime *= r.exponent
r.waitTime = time.Duration(int64(r.waitTime) * r.exponent)

r.currentRetries++
}

/* Reset the current retries back to zero so that we can re-use this object elsewhere. */
/* resetCurrentRetries resets the current retries back to zero so that we can re-use this object elsewhere. */
func (r *MethodCallRetrier) resetCurrentRetries() {
r.currentRetries = 0
}

/* Reset the error list back to zero so that we can re-use this object elsewhere. */
/* resetErrorList resets the error list back to zero so that we can re-use this object elsewhere. */
func (r *MethodCallRetrier) resetErrorList() {
r.errorList = nil
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Usage
Initialise the object with some options:

```
MethodCallRetrier.New(waitTime int64, maxRetries int64, exponent int64)
MethodCallRetrier.New(waitTime time.Duration, maxRetries int64, exponent int64)
```

Call `ExecuteWithRetry` with your object and method you want to retry:
Expand Down

0 comments on commit a45a21d

Please sign in to comment.