- [Instructor] We will build the circuit breaker
directly into our conference-app.
Depending on use case, and preferences,
it could also live inside the service registry
or even be a service on its own.
So, in my conference-app, I will now go to server
and there I create a new folder lib,
and in there, I create new file CircuitBreaker.js,
and in there let's start with the class declaration
and let's also not forget to module.exports that.
So first of all,
the circuit breaker will have to do up on http calls,
so we will bring in axios, const axios
equals require axios at the very top
of this file.
Now let's start with the constructor,
where we will initialize a few member variables
that we will need.
First I want to store individual state
for each end point that we are calling.
So I want to create some member
that can hold all these states.
So I add this.states equals
and it's an empty object.
I want the circuit to be open
after a certain threshold of failures,
so let's add this failureThreshold
and let it be five.
Then also, when we want to switch
into this half open stage,
there is this cool down period,
so add this.cooldownPeriod equals 10 seconds,
and I also want to define a request time out,
so that's the time we wait
for an http request to return,
or if it does not return after this time,
it's considered to have failed.
Next, let's add a function that creates such a state object
for an end point for us,
so add initState
and it gets an endpoint as argument
and there I now set this state
for this endpoint
so this will create a new entry
in states for this endpoint are overrided
failures, zero at the beginning,
cooldownperiod is the cool down period
that we defined before,
then the circuit of course in the beginning
is always closed
and the nextTry so that would be for half open state,
is zero, basically, uninitialized.
Next we will implement methods
that will handle the different events that we will see
so we will see a success event,
so let's say onSuccess, and this gets an endpoint,
and what we can do here
is we can simply reset the state to the default
because when we have a success
we can basically reset everything
that we had before in terms of failures, et cetera,
so I can simply add this.initState(endpoint).
We of course also want
to handle the failure states,
so I add onFailure
and there I also get an endpoint,
let's fetch the state
from the states object,
this states endpoint,
and we can already increase the failure counter.
Plus equals one.
Now let's see if we now reach the threshold,
so I add if (state).failures > this.failureThreshold,
then I want this circuit to be open
then we want to set nextTry
this is when we want to switch
into half open state
and this will be new Date divided by 1000,
plus this.cooldownPeriod,
we want to exponential back of here.
And this would be now
as a good time to create some kind of alert,
maybe even let the system be known
no because there is something wrong obviously.
In our case we will now simply to console.log.
We don't have a logging library
in this conference-app,
so we're using console.log here.
And let's say alert,
Circuit for $(endpoint)
is in state open.
Next we want to create a function
that now tests in which state we are
and if we can do a request to add
canRequest endpoint and there I add const state
equals this.states endpoint
and if state.circuit is closed,
then we can instantly return true,
so everything is fine.
Now we have to check
if we can go into half open
so it's not closed
so let's see const now,
there we store the current timestamp in seconds,
and if(state.nextTry) <= now) we can go
into half open state,
which means state.circuit= 'HALF',
we won't ever evaluate the half open state like that
so this variable on that,
but just for the sake of completeness,
we will still do that,
and we switch, we tell, we say return true.
In any other case we will return false.
So if we go into half open state
it will try to one time,
and then if it fails,
it runs into on failure again,
and there it will again reset nextTry
and set the circuit to open.
We are now missing a method
that actually performs this request
to the endpoint and we will create that
in the next video.