2020-05-11 09:31:25 +03:00
|
|
|
/*
|
|
|
|
* @Description: https://github.com/crazybber
|
|
|
|
* @Author: Edward
|
|
|
|
* @Date: 2020-05-11 10:55:28
|
|
|
|
* @Last Modified by: Edward
|
2020-05-22 12:57:41 +03:00
|
|
|
* @Last Modified time: 2020-05-22 17:21:06
|
2020-05-11 09:31:25 +03:00
|
|
|
*/
|
|
|
|
|
2020-04-28 17:48:09 +03:00
|
|
|
package circuit
|
|
|
|
|
|
|
|
import (
|
2020-05-21 11:02:28 +03:00
|
|
|
"context"
|
2020-05-10 17:03:24 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-05-22 11:50:41 +03:00
|
|
|
"log"
|
2020-05-10 17:03:24 +03:00
|
|
|
"net/http"
|
2020-05-08 10:51:33 +03:00
|
|
|
"testing"
|
2020-04-28 17:48:09 +03:00
|
|
|
)
|
|
|
|
|
2020-05-10 17:03:24 +03:00
|
|
|
var breaker *RequestBreaker
|
|
|
|
|
2020-05-22 12:57:41 +03:00
|
|
|
var onStateChangeEvent = func(name string, from, to State) {
|
|
|
|
fmt.Println("name:", name, "from:", from, "to", to)
|
|
|
|
}
|
|
|
|
|
|
|
|
var whenConditionOccurred = func(cnter counters) bool {
|
|
|
|
//失败率,可以由用户自己定义
|
|
|
|
failureRatio := float64(cnter.TotalFailures) / float64(cnter.Requests)
|
|
|
|
return cnter.Requests >= 3 && failureRatio >= 0.6
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:50:41 +03:00
|
|
|
func TestObjectBreaker(t *testing.T) {
|
2020-04-28 17:48:09 +03:00
|
|
|
|
2020-05-22 13:14:19 +03:00
|
|
|
jobToDo := func(ctx context.Context) (interface{}, error) {
|
2020-05-19 12:56:40 +03:00
|
|
|
resp, err := http.Get("https://bing.com/robots.txt")
|
2020-05-10 17:03:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return body, nil
|
2020-05-21 11:02:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 12:57:41 +03:00
|
|
|
breaker = NewRequestBreaker(Name("HTTP GET"), BreakIf(whenConditionOccurred), WithStateChanged(onStateChangeEvent))
|
2020-05-21 11:02:28 +03:00
|
|
|
|
|
|
|
body, err := breaker.Do(jobToDo)
|
2020-05-10 17:03:24 +03:00
|
|
|
if err != nil {
|
2020-05-19 12:56:40 +03:00
|
|
|
t.Fatal(err)
|
2020-05-10 17:03:24 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:56:40 +03:00
|
|
|
fmt.Println(string(body.([]byte)))
|
2020-04-28 17:48:09 +03:00
|
|
|
}
|
2020-05-21 11:02:28 +03:00
|
|
|
|
|
|
|
func TestFunctionalBreaker(t *testing.T) {
|
|
|
|
|
|
|
|
//something need to do
|
|
|
|
jobToDo := func(ctx context.Context) error {
|
|
|
|
resp, err := http.Get("https://bing.com/robots.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(body))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//wrapper and control job with a breaker
|
|
|
|
circuitWork := Breaker(jobToDo, 2 /* failureThreshold */)
|
|
|
|
|
|
|
|
params := context.TODO()
|
|
|
|
|
|
|
|
// do the job as usually
|
2020-05-22 11:50:41 +03:00
|
|
|
res := circuitWork(params)
|
|
|
|
|
|
|
|
log.Print("\nresult:", res)
|
2020-05-21 11:02:28 +03:00
|
|
|
|
|
|
|
}
|