Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two-step pass check and outcome reporting #4

Closed
aryszka opened this issue Apr 27, 2017 · 6 comments
Closed

two-step pass check and outcome reporting #4

aryszka opened this issue Apr 27, 2017 · 6 comments

Comments

@aryszka
Copy link
Contributor

aryszka commented Apr 27, 2017

Hi, I'd like to have the possibility to check if a request can pass through and setting the outcome in separate steps, outside the Execute() method. This would be possible if the breaker had the following additional methods:

  • Allow() bool: would tell if the breaker is closed, or, in half open state, can the request pass through.
  • Success(): report a successful outcome
  • Failure(): report a failed outcome

Allow() would be basically a visible wrapper around beforeRequest(), while Failure() and Success() around afterRequest().

This would enable using the breaker in an asynchronous way, where the check for passing and reporting the outcome are not in a single control flow.

I'd be happy to make a pull request.

@YoshiyukiMineo
Copy link
Member

Sorry for the late reply.

You can use goroutines in the req function parameter of Execute if you'd like to use the breaker in an asynchronous way.

func (cb *CircuitBreaker) Execute(req func() (interface{}, error)) (interface{}, error)

So I think it's not necessary to add new methods.

Could you show a concrete use case for the 2-step way?

@aryszka
Copy link
Contributor Author

aryszka commented May 11, 2017

my use case: i have "filters" in a router processing requests and responses before forwarding them, both on the request and response path. They behave a somewhat similar alternative way to chaining standard handlers, when a handler calls the next one, but the difference is that these filters don't know about each other.

These filters are small stateless (typically) middleware, not knowing about the outside world. I want to implement the circuit breaker as such a filter. A filter has a Request(Context) and Response(Context) method, and has no notion about the overall control flow. On the request path, I want to check the breaker if a request can proceed and signal to the controlling logic to block if the breaker is open. On the response path, I want to register the outcome of the forwarded request.

in very a simplified way, it would look like this:

func (f *filter) Request(ctx filters.FilterContext) {
  if !f.gerBreaker().Allow() {
    ctx.Serve(&http.Response{StatusCode: 503})
  }
}

func (f *filter) Response(ctx filters.FilterContext) {
  if ctx.Response().StatusCode < 500 {
    f.getBreaker().Success()
    return
  }

  f.getBreaker().Fail()
}

Of course, i could just put a breaker in the controlling logic, and then the current interface of the gobreaker would be enough. But I want to allow to set different breaker configuration for different routes, and the way in my project of configuring these routes is to apply filters with different settings. (https://github.com/zalando/skipper)

The problems with the additional go routines that you're suggesting would be:

  • would need to make sure that the go routine spawned in Request() completes, even if Response() is not called for some reason
  • it would come with an additional synchronization cost and one extra goroutine for each incoming request. This can have negative effects when under high load.

As you see in my pull request #5 , this can done without changing the internal logic of gobreaker, only by exporting functionality that is already available in it.

@YoshiyukiMineo
Copy link
Member

Thanks. I understand your use case.

I’d like to hide internal implementation stuff like generation.
So I think it’s better to change Allow as below:

func (tc *TwoStepCircuitBreaker) Allow() (done func(succeeded bool), err error)

Allow returns the closure done instead of generation.
After the request, done needs to be called with the succeeded flag that represents whether the request succeeded or not.

Allow and Execute have different usages. So I think Allow belongs to a new different struct like TwoStepCircuitBreaker that is just a wrapper of CircuitBreaker.

Do you have any other suggestion?
I'll push the above change in another branch unless you want to submit it.

@aryszka
Copy link
Contributor Author

aryszka commented May 15, 2017

This sounds good to me. I like that the 'generation' concept is not leaked out this way. I am closing my PR.

@YoshiyukiMineo
Copy link
Member

Thank you for your PR. I have merged it.

@aryszka
Copy link
Contributor Author

aryszka commented May 30, 2017

thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants