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

Timeout not working with Execute method (working with Go method)

jarpit96 opened this issue · comments

Hi, I am running following sample code to test out execution timeout

h := circuit.Manager{}
cb := h.MustCreateCircuit("redis-cb", circuit.Config{
		Execution: circuit.ExecutionConfig{
			// Time out the context after a few ms
			Timeout: time.Millisecond * 30,
		},})


	start := time.Now()
	errCB := cb.Execute(c, func(ctx context.Context) error {
		time.Sleep(2*time.Second)
		return nil
	}, nil)

	fmt.Println(time.Now().Sub(start).Seconds())
	if errCB != nil {
		fmt.Println(errCB)
	}

Output (when using Execute) -

2.000189195

If I use .Go instead of .Execute, then the execution times out after 30 ms properly.
Output (when using .Go)

0.03288965
context deadline exceeded

Hi,

This is expected behavior. The README is documenting that the Execute method does not run inside a goroutine, but the Go function does. Let me know if there is a way I can clarify that in the README

Thanks!

Closing this out as expected behavior. Let me know if you find anything else strange.

@cep21 in that case what is the use case of Timeout inside ExecutionConfig.

If it is not timing out the operation Execute method. How can we make sure Execute method timed out after a certain duration for ex: 30 millisecond

It times out the context. Go has no way to stop execution of a goroutine. You can either use .Go to run this in a goroutine that will timeout, or you can use .Execute which will more efficiently run it inside the same goroutine, but you will have to program around using Context to time out your execution.