Comments (6)
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.
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.
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.
This sounds good to me. I like that the 'generation' concept is not leaked out this way. I am closing my PR.
from gobreaker.
Thank you for your PR. I have merged it.
from gobreaker.
thanks π
from gobreaker.
Related Issues (20)
- start a new release? HOT 1
- println -> printf HOT 1
- [question/proposal] Monitoring
- breaker for host+service HOT 6
- Why don't use a rolling window to maintain the counts? HOT 4
- Where's the option for fallback function? HOT 5
- Create a new release for module support HOT 2
- Does this really require go 1.12? HOT 4
- race conditions HOT 1
- Confusion about ErrTooManyRequests error HOT 4
- May I request a new release with tag HOT 2
- It will cause a deadlock when call breaker.Counts() method in the OnStateChange() method HOT 3
- breaker per url HOT 1
- example: HTTP circuit breaker doesn't consider HTTP status codes which are more indicative of retryable errors HOT 1
- Reproducing "too many requests"
- Circuitbreaker not performing MaxRequests operations in Half-open state HOT 3
- is circuit breaker threadsafe? HOT 1
- [question/proposal] Monitoring HOT 1
- Will using aync/atomic for incrementing and loading/storing the counts improve performance?
- Exported type for errors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gobreaker.