mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-07-19 22:52:52 +02:00
initial import
This commit is contained in:
5
microservices/http-client-circut-breaker/go.mod
Normal file
5
microservices/http-client-circut-breaker/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module golang.source-fellows.com/samples/httpcircuitbreaker
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/sony/gobreaker v0.4.1
|
6
microservices/http-client-circut-breaker/go.sum
Normal file
6
microservices/http-client-circut-breaker/go.sum
Normal file
@ -0,0 +1,6 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sony/gobreaker v0.4.1 h1:oMnRNZXX5j85zso6xCPRNPtmAycat+WcoKbklScLDgQ=
|
||||
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
43
microservices/http-client-circut-breaker/main.go
Normal file
43
microservices/http-client-circut-breaker/main.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
breaker "github.com/sony/gobreaker"
|
||||
)
|
||||
|
||||
var cb *breaker.CircuitBreaker
|
||||
|
||||
func main() {
|
||||
|
||||
settings := breaker.Settings{Interval: 1 * time.Second, Timeout: 5 * time.Second}
|
||||
cb = breaker.NewCircuitBreaker(settings)
|
||||
|
||||
for {
|
||||
bites, err := cb.Execute(func() (interface{}, error) {
|
||||
res, err := http.Get("http://localhost:8080/a")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode == http.StatusOK {
|
||||
|
||||
}
|
||||
defer res.Body.Close()
|
||||
bites, _ := ioutil.ReadAll(res.Body)
|
||||
return bites, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Printf("error %v\n", err)
|
||||
|
||||
} else {
|
||||
fmt.Println(string(bites.([]byte)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user