From a45a21d5deac3f6f74530cad94aecae8c53f4f97 Mon Sep 17 00:00:00 2001 From: J7mbo Date: Wed, 10 Apr 2019 22:29:09 +0200 Subject: [PATCH] [#8] - WaitTime is now a time.Duration instead of an int64 --- MaxRetriesError.go | 11 +++++++---- MethodCallRetrier.go | 28 ++++++++++++++-------------- README.md | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/MaxRetriesError.go b/MaxRetriesError.go index fa7bfe9..b83d7c3 100644 --- a/MaxRetriesError.go +++ b/MaxRetriesError.go @@ -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, diff --git a/MethodCallRetrier.go b/MethodCallRetrier.go index e3809b0..2fa0c1f 100644 --- a/MethodCallRetrier.go +++ b/MethodCallRetrier.go @@ -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 } @@ -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) { @@ -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 } diff --git a/README.md b/README.md index 10f85de..611bccf 100644 --- a/README.md +++ b/README.md @@ -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: