Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ci and badges #1

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Get dependencies
run: go get -v ./...

- name: Build
run: go build -v ./...

- name: Test
run: go test -v -covermode=atomic -coverprofile=coverage.out ./...

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Alexander Gehres
Copyright (c) 2023 Alexander Gehres

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/ybbus/httpretry)](https://goreportcard.com/report/github.com/ybbus/httpretry)
[![Go build](https://github.com/ybbus/httpretry/actions/workflows/go.yml/badge.svg)](https://github.com/ybbus/httpretry)
[![Codecov](https://codecov.io/github/ybbus/httpretry/branch/master/graph/badge.svg?token=ARYOQ8R1DT)](https://codecov.io/github/ybbus/httpretry)
[![GoDoc](https://godoc.org/github.com/ybbus/httpretry?status.svg)](https://godoc.org/github.com/ybbus/httpretry)
[![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)]()

# httpRetry

Enriches the standard go http client with retry functionality using a wrapper around the Roundtripper interface.

The advantage of this library is that it makes use of the default http.Client.
This means you can provide it to any library that accepts the go standard http.Client.
This in turn gives you the possibility to add resilience to a lot of http based go libraries with just a single line of code.
Of course it can also be used as standalone http client in your own projects.


## Installation

```sh
Expand All @@ -25,13 +27,16 @@ To get a standard http client with retry functionality:
client := httpretry.NewDefaultClient()
// use this as usual when working with http.Client
```

This single line of code returns a default http.Client that uses an exponential backoff and sends up to 5 retries if the request was not successful.
Requests will be retried if the error seems to be temporary or the requests returns a status code that may change over time (e.g. GetwayTimeout).

### Modify / customize the Roundtripper (http.Transport)

Since httpretry wraps the actual Roundtripper of the http.Client, you should not try to replace / modify the client.Transport field after creation.

You either configure the http.Client upfront and then "make" it retryable like in this code:

```golang
customHttpClient := &http.Client{}
customHttpClient.Transport = &http.Transport{...}
Expand Down Expand Up @@ -72,4 +77,4 @@ client := httpretry.NewDefaultClient(
return time.Duration(attemptNum+1) * 1 * time.Second
}),
)
```
```
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/ybbus/httpretry

go 1.13
go 1.19

require github.com/stretchr/testify v1.4.0
require github.com/stretchr/testify v1.8.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 11 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 3 additions & 4 deletions roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package httpretry
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"time"
)
Expand Down Expand Up @@ -47,14 +46,14 @@ func (r *RetryRoundtripper) RoundTrip(req *http.Request) (*http.Response, error)

// store it for the first time
if dataBuffer == nil {
data, err := ioutil.ReadAll(req.Body)
data, err := io.ReadAll(req.Body)
req.Body.Close()
if err != nil {
return nil, err
}
dataBuffer = bytes.NewReader(data)
req.ContentLength = int64(dataBuffer.Len())
req.Body = ioutil.NopCloser(dataBuffer)
req.Body = io.NopCloser(dataBuffer)
}

// reset the request body
Expand Down Expand Up @@ -96,7 +95,7 @@ func (r *RetryRoundtripper) RoundTrip(req *http.Request) (*http.Response, error)

func drainAndCloseBody(resp *http.Response, maxBytes int64) {
if resp != nil {
io.CopyN(ioutil.Discard, resp.Body, maxBytes)
io.CopyN(io.Discard, resp.Body, maxBytes)
resp.Body.Close()
}
}