cep21 / circuit

An efficient and feature complete Hystrix like Go implementation of the circuit breaker pattern.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

circuit can't open?

jiacai2050 opened this issue · comments

func main() {
	// f := rolling.StatFactory{}
	h := circuit.Manager{
		DefaultCircuitProperties: []circuit.CommandPropertiesConstructor{
			// f.CreateConfig,
			func(circuitName string) circuit.Config {
				return circuit.Config{
					Execution: circuit.ExecutionConfig{
						MaxConcurrentRequests: 1000,
					},
					General: circuit.GeneralConfig{
						OpenToClosedFactory: hystrix.CloserFactory(hystrix.ConfigureCloser{
							//		// This should allow a new request every 10 milliseconds
							SleepWindow: time.Millisecond * 10,
						}),
						ClosedToOpenFactory: hystrix.OpenerFactory(hystrix.ConfigureOpener{
							RequestVolumeThreshold:   10,
							ErrorThresholdPercentage: 50,
						}),
					},
				}
			},
		},
	}

	c := h.MustCreateCircuit("hello-world")
	fmt.Printf("var = %+v\n", h.Var())
	loop := 100
	for i := 0; i < loop; i++ {
		time.Sleep(500 * time.Millisecond)
		errResult := c.Execute(context.Background(), func(ctx context.Context) error {

			if i > 10 {
				return nil
			}
			return fmt.Errorf("err i %d", i)
		}, nil)

		fmt.Println("Result of execution:", errResult)
	}

}

AKAIK, circuit should open after 10*50%=5 failed response within rolling window(default: 10s), but it execute successfully
revision: 7687802

Output

Result of execution: err i 0
Result of execution: err i 1
Result of execution: err i 2
Result of execution: err i 3
Result of execution: err i 4
Result of execution: err i 5
Result of execution: err i 6
Result of execution: err i 7
Result of execution: err i 8
Result of execution: err i 9
Result of execution: err i 10
Result of execution: <nil>
Result of execution: <nil>
Result of execution: <nil>
Result of execution: <nil>
Result of execution: <nil>

Because:

SleepWindow: time.Millisecond * 10, // This allow a new request every 10 milliseconds when circuit is open

And your test code is run in 1req/500ms.

Thanks @yangxikun , I modify SleepWindow to 5s, circuit indeed open, but after 10 failed reqs, not 5.

@jiacai2050 Because RequestVolumeThreshold: 10. see

func (e *Opener) ShouldOpen(now time.Time) bool {