Giter VIP home page Giter VIP logo

Comments (6)

aryszka avatar aryszka commented on August 24, 2024 1

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.

from gobreaker.

YoshiyukiMineo avatar YoshiyukiMineo commented on August 24, 2024

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?

from gobreaker.

YoshiyukiMineo avatar YoshiyukiMineo commented on August 24, 2024

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.

from gobreaker.

aryszka avatar aryszka commented on August 24, 2024

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

from gobreaker.

YoshiyukiMineo avatar YoshiyukiMineo commented on August 24, 2024

Thank you for your PR. I have merged it.

from gobreaker.

aryszka avatar aryszka commented on August 24, 2024

thanks πŸ‘

from gobreaker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.