Sample software circuit breaker. |
- Input: An interface module to receive data from one component.
- Output: An interface module to transmit data to the next component.
- Control: A central control unit which manages passing or dropping or disconnecting the flow and/or flow path.
Now let us see how we can have this CB to do what we want, suppose we want it to drop more than 1000 data packet per second and we want the CB in a simple single thread mode. The following shows a pseudo code for what we have in mind:
CB cb;
cb.init();
cb.control.counter.set(0);
long lastSecond = getUnixTimestamp();
while (true) {
DataItem dataItem = cb.input.pop();
if (lastSecond != getUnixTimestamp()) {
lastSecond = getUnixTimestamp();
cb.control.counter.set(0);
}
cb.control.counter.increase();
if (cb.control.counter.getCurrent() < 1000) {
cb.output.push(dataItem);
}
}
The above code is clear and is exactly what we wanted to do. We read data from the input, if the current period of packet counting is passed we just reset the counter then we increase the counter. Now it is time to forward the data, we just forward it if the counter shows less than the threshold.
No comments:
Post a Comment