GithubHelp home page GithubHelp logo

dias-zhanabayev / httpbreaker Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 53 KB

Дополнение к http клиенту. Реализует паттерн circuit breaker для http

License: MIT License

Go 100.00%

httpbreaker's Introduction

httpbreaker

Дополнение к http клиенту. Реализует паттерн circuit breaker для http. Подвел реализацию github.com/sony/gobreaker к RoundTripper интерфейсу, чтобы можно было использовать вместе с любым http клиентом.

Installation

go get github.com/dias-zhanabayev/httpbreaker

Example

package main

import (
	"fmt"
	"github.com/dias-zhanabayev/httpbreaker"
	"io"
	"net/http"
)

var cb http.RoundTripper

func initialize() {
	var st httpbreaker.Settings
	st.Name = "HTTP GET"
	st.ReadyToTrip = func(counts httpbreaker.Counts) bool {
		failureRatio := float64(counts.TotalFailures()) / float64(counts.Requests())
		return counts.Requests() >= 3 && failureRatio >= 0.6
	}
	st.TracerTransport = http.DefaultTransport

	cb = httpbreaker.NewCircuitBreaker(st)
}

func main() {
	initialize()
	client := &http.Client{
		Transport: cb,
	}

	// nolint
	resp, err := client.Get("https://www.google.com/robots.txt")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			fmt.Println("Error:", err)
		}
	}(resp.Body)
	fmt.Println("Response:", resp.Status)
}

httpbreaker's People

Contributors

dias-zhanabayev avatar dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.