Last active
April 24, 2024 15:47
-
-
Save jerryan999/bcfdd746f3f8c2c11c3d27f1b65dfcf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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