Skip to content

Instantly share code, notes, and snippets.

@jerryan999
Last active April 24, 2024 15:47
Show Gist options
  • Save jerryan999/bcfdd746f3f8c2c11c3d27f1b65dfcf3 to your computer and use it in GitHub Desktop.
Save jerryan999/bcfdd746f3f8c2c11c3d27f1b65dfcf3 to your computer and use it in GitHub Desktop.
func DoReq() error {
resp, err := http.Get("http://localhost:8080/ping")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("bad response")
}
return nil
}
var startTime time.Time = time.Now()
func server() {
e := gin.Default()
e.GET("/ping", func(ctx *gin.Context) {
if time.Since(startTime) < 5*time.Second {
ctx.String(http.StatusInternalServerError, "pong")
return
}
ctx.String(http.StatusOK, "pong")
})
fmt.Printf("Starting server at port 8080\n")
e.Run(":8080")
}
package main
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/sony/gobreaker"
)
var startTime time.Time = time.Now()
func server() {
e := gin.Default()
e.GET("/ping", func(ctx *gin.Context) {
if time.Since(startTime) < 5*time.Second {
ctx.String(http.StatusInternalServerError, "pong")
return
}
ctx.String(http.StatusOK, "pong")
})
fmt.Printf("Starting server at port 8080\n")
e.Run(":8080")
}
// On client side, we defind a simple function to call the upstream service.
func DoReq() error {
resp, err := http.Get("http://localhost:8080/ping")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("bad response")
}
return nil
}
func main() {
go server()
// call with circuit breaker
cb := gobreaker.NewCircuitBreaker(
gobreaker.Settings{
Name: "my-circuit-breaker",
MaxRequests: 3,
Timeout: 3 * time.Second,
Interval: 1 * time.Second,
ReadyToTrip: func(counts gobreaker.Counts) bool {
return counts.ConsecutiveFailures > 3
},
OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
fmt.Printf("CircuitBreaker '%s' changed from '%s' to '%s'\n", name, from, to)
},
},
)
fmt.Println("Call with circuit breaker")
for i := 0; i < 100; i++ {
_, err := cb.Execute(func() (interface{}, error) {
err := DoReq()
return nil, err
})
if err != nil {
fmt.Println(err)
}
time.Sleep(100 * time.Millisecond)
}
}
func main() {
go server()
// call with circuit breaker
cb := gobreaker.NewCircuitBreaker(
gobreaker.Settings{
Name: "my-circuit-breaker",
MaxRequests: 3,
Timeout: 3 * time.Second,
Interval: 1 * time.Second,
ReadyToTrip: func(counts gobreaker.Counts) bool {
return counts.ConsecutiveFailures > 3
},
OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
fmt.Printf("CircuitBreaker '%s' changed from '%s' to '%s'\n", name, from, to)
},
},
)
fmt.Println("Call with circuit breaker")
for i := 0; i < 100; i++ {
_, err := cb.Execute(func() (interface{}, error) {
err := DoReq()
return nil, err
})
if err != nil {
fmt.Println(err)
}
time.Sleep(100 * time.Millisecond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment