Skip to content
Open
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
27 changes: 27 additions & 0 deletions example/http_breaker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -11,13 +12,34 @@ import (

var cb *gobreaker.CircuitBreaker

// StatusError is returned when the server responds with a non-2xx status code.
type StatusError struct {
Code int
Status string
}

func (e *StatusError) Error() string {
return "unexpected status: " + e.Status
}

func init() {
var st gobreaker.Settings
st.Name = "HTTP GET"
st.ReadyToTrip = func(counts gobreaker.Counts) bool {
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= 3 && failureRatio >= 0.6
}
st.IsSuccessful = func(err error) bool {
if err == nil {
return true
}
// Server errors indicate the service is unhealthy and should trip
// the circuit breaker. Client errors such as 404 Not Found are
// responses the server produced deliberately, so they are returned
// to the caller without counting as failures.
var se *StatusError
return errors.As(err, &se) && se.Code < http.StatusInternalServerError
}

cb = gobreaker.NewCircuitBreaker(st)
}
Expand All @@ -31,6 +53,11 @@ func Get(url string) ([]byte, error) {
}

defer resp.Body.Close()

if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return nil, &StatusError{Code: resp.StatusCode, Status: resp.Status}
}

return ioutil.ReadAll(resp.Body)
})
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions v2/example/http_breaker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -11,13 +12,34 @@ import (

var cb *gobreaker.CircuitBreaker[[]byte]

// StatusError is returned when the server responds with a non-2xx status code.
type StatusError struct {
Code int
Status string
}

func (e *StatusError) Error() string {
return "unexpected status: " + e.Status
}

func init() {
var st gobreaker.Settings
st.Name = "HTTP GET"
st.ReadyToTrip = func(counts gobreaker.Counts) bool {
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= 3 && failureRatio >= 0.6
}
st.IsSuccessful = func(err error) bool {
if err == nil {
return true
}
// Server errors indicate the service is unhealthy and should trip
// the circuit breaker. Client errors such as 404 Not Found are
// responses the server produced deliberately, so they are returned
// to the caller without counting as failures.
var se *StatusError
return errors.As(err, &se) && se.Code < http.StatusInternalServerError
}

cb = gobreaker.NewCircuitBreaker[[]byte](st)
}
Expand All @@ -31,6 +53,11 @@ func Get(url string) ([]byte, error) {
}

defer resp.Body.Close()

if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return nil, &StatusError{Code: resp.StatusCode, Status: resp.Status}
}

return io.ReadAll(resp.Body)
})
if err != nil {
Expand Down