goback icon indicating copy to clipboard operation
goback copied to clipboard

add more thourough examples

Open oryband opened this issue 10 years ago • 2 comments

The single example for this library is to short and undetailed. I would add further examples, using all public methods available.

In addition, maybe also add an example showing how to retry http.Get() requests using goback would be good:

// RetriableHTTPGet sends an HTTP GET request using exponential backoff,
// and returns response status code and body.
//
// It receives a "should retry?" function, which determines whether to retry the request or not.
// This function receives the (*http.Response, error) result of a http.Get() request,
// and returns a (bool, error). If the boolean is true, then the request will
// be retried using exponential backoff.
// If the returned error is not nil, the request is considered as errorneus and
// will not be retried, regardless of the boolean.
// If that error is not nil,
// the request is considered as "failed", and the request is retried using exponential backoff.
func RetriableHTTPGet(
    shouldRetry func(*http.Response, error) (bool, error), url string) (
    res *http.Response, err error) {

    // Initialize exponential backoff waiter.
    // Jitter helps to avoid race conditions between retrying goroutines.
    b := &goback.SimpleBackoff{
        MaxAttempts: 3,
        Min:         500 * time.Millisecond,
        Max:         500 * time.Millisecond,
        Factor:      2,
    }

    // Send HTTP request and execute "should retry?" function on result.
    for err == nil {
        res, err = http.Get(url)
        retry, err := shouldRetry(res, err)
        if err == nil && retry {
            err = goback.Wait(b)
        }
    }

    return
}

oryband avatar Mar 24 '15 10:03 oryband

i'm not sure that example above is 100% correct and working, but just a thought

oryband avatar Mar 24 '15 10:03 oryband

I'm sorry, I've just noticed you had an example directory. I totally missed it. You can add them to go docs if you apply a _test.go to each of the source files. Check godoc documentation.

oryband avatar Mar 24 '15 10:03 oryband