From 07426a4371e3ea8d564eb946f56aea92645e65f9 Mon Sep 17 00:00:00 2001 From: Alexander Gehres Date: Thu, 23 Feb 2023 00:11:40 +0100 Subject: [PATCH] add ci and badges --- .github/workflows/go.yml | 31 +++++++++++++++++++++++++++++++ LICENSE | 2 +- README.md | 9 +++++++-- go.mod | 10 ++++++++-- go.sum | 16 +++++++++++----- roundtripper.go | 7 +++---- 6 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..106598f --- /dev/null +++ b/.github/workflows/go.yml @@ -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 diff --git a/LICENSE b/LICENSE index 6192efa..f13a47a 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 1155acd..6c7a863 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ [![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. @@ -10,7 +13,6 @@ This means you can provide it to any library that accepts the go standard http.C 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 @@ -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{...} @@ -72,4 +77,4 @@ client := httpretry.NewDefaultClient( return time.Duration(attemptNum+1) * 1 * time.Second }), ) -``` \ No newline at end of file +``` diff --git a/go.mod b/go.mod index 179b472..f54e003 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 8fdee58..2ec90f7 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/roundtripper.go b/roundtripper.go index ecb76fb..2b769b6 100644 --- a/roundtripper.go +++ b/roundtripper.go @@ -3,7 +3,6 @@ package httpretry import ( "bytes" "io" - "io/ioutil" "net/http" "time" ) @@ -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 @@ -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() } }