Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/bakerolls/retry

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/bakerolls/retry

  • v0.0.0-20190425193437-b68366ed72f5
  • Source
  • Go
  • Socket score

Version published
Created
Source

retry

GoDoc Go Report Card

Package retry is a small implementation of the http.RoundTripper interface that can be found in http.Client. It is responsible to make HTTP requests and can be used to cache or retry them.

By default a request will be tried again only if the response code is below 500 (Internal Server Error) and not 429 (Too Many Requests). This behaviour can be changed with the WithVerifier option.

Examples
New

Create a new HTTP client that will retry requests five times and sleeps one second between each one. Since the requested URL will always return a status code of 500 (Internal Server Error), this function will run for five seconds and the time it takes to call the endpoint five times.

package main

import (
	"fmt"
	"github.com/bake/retry"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	client := &http.Client{
		Transport: retry.New(5, time.Second),
	}
	res, err := client.Get("https://httpbin.org/status/500")
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	fmt.Printf("%s\n", res.Status)
	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
		log.Fatal(err)
	}
}

WithVerifier

WithVerifier can be used to modify the default behaviour that is used to determine if a request can be repeated. In this example, all responses that do not succeed (with a status code outside [200-300[) will be tried again.

package main

import (
	"fmt"
	"github.com/bake/retry"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	verifier := func(res *http.Response) bool {
		return 200 > res.StatusCode || res.StatusCode >= 300
	}
	client := &http.Client{
		Transport: retry.New(5, time.Second, retry.WithVerifier(verifier)),
	}
	res, err := client.Get("https://httpbin.org/status/418")
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	fmt.Printf("%s\n", res.Status)
	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
		log.Fatal(err)
	}
}

FAQs

Package last updated on 25 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc