Skip to content

robert-kel-tg/httpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpclient

  • Http Client with Circuit Breaker and Retry mechanisms

Installation

go get -u github.com/robert-kel-tg/httpclient

Usage

package main

import (
	"bytes"
	"github.com/robert-kel-tg/httpclient"
	log "github.com/sirupsen/logrus"
	"io/ioutil"
	"time"
)

func main() {
  	// With default values applied
	client := http.NewClient("TestHttpClient")

  	// Or you can specify one or more values
	client := NewClient(
		"TestHttpClient",
		WithTimeout(3000),
		WithMaxConcurrentRequests(3),
		WithRequestVolumeThreshold(3),
		WithSleepWindow(1000),
		WithErrorPercentThreshold(1),
		WithRetryAttempt(3),
		WithRetrySleep(time.Millisecond*5),
	)

	body := "That's body!"

	resp, err := client.Post(&http.RequestSettings{
		Url:  "https://httpbin.org/post",
		Body: bytes.NewReader([]byte(body)),
	})

	if err != nil {
		log.Errorf("Error reading response %v", err)
	}

	defer func() {
		if err := resp.Body.Close(); err != nil {
			log.Errorf("Error closing body %v", err)
		}
	}()

	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Errorf("Error reading body %v", err)
	}
	log.Printf("Printing response body \n %s", string(bodyBytes))
}

About

Go Http Client with Circuit Breaker and Retry mechanisms

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages