Skip to content

ladydascalie/go-httpretry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpretry

Go

An http client with support for retry policies and functional options.

Install

go get github.com/ladydascalie/go-httpretry

Usage

Use the provided (sane) default

package main

import "github.com/ladydascalie/go-httpretry"

func main() {
        client := httpretry.NewClient()
        
        ctx, cancel := context.WithTimeout(context.Background(), time.Second)
        defer cancel()
        
        req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.foo.bar", nil)
        // handle your errors!
        
        res, err := client.Do(req)
        // handle your errors!
}

Customise your usage with functional options

package main

import "github.com/ladydascalie/go-httpretry"

func main() {
        client := httpretry.NewClient(
                        httpretry.WithRetryPolicy(&httpretry.RetryPolicy{
                        Retries:     5,
                        WaitTime:    time.Second,
                        BackoffMode: httpretry.BackoffModeExponential,
                        ResponseCheck: func(response *http.Response) (shouldRetry bool, retryAfter time.Duration) {
                                if response.StatusCode == http.StatusServiceUnavailable {
                                        return true, 30 * time.Second
                                }
                                return false, 0
                        },
                }),
        )

        ctx, cancel := context.WithTimeout(context.Background(), time.Second)
        defer cancel()
        
        req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.foo.bar", nil)
        // handle your errors!
        
        res, err := client.Do(req)
        // handle your errors!

        resp, err := client.Do(req)
}

About

An http client with support for retry policies and functional options.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages