DEV Community

Cover image for How to create custom Decorator for Circuit Breaker in Typescript/Javascript Applications
Ankit Kumar
Ankit Kumar

Posted on • Edited on

How to create custom Decorator for Circuit Breaker in Typescript/Javascript Applications

To understand “How to create Decorators in Typescript for Circuit Breakers”, we need to understand

  • What are decorators in typescript?
  • What are circuit breakers in software systems?

Let's first read a bit about decorators

Decorators are a way to decorate functions, members of a class, or a class itself, with extra functionality.

When you apply a decorator to a function, a class, or a class member, you are actually calling a function that is going to receive details of what is being decorated, and the decorator implementation will then be able to transform the code dynamically, adding extra functionality, and reducing boilerplate code.

They are a way to have metaprogramming in TypeScript, which is a programming technique that enables the programmer to create code that uses other code from the application itself as data.


Let’s now see what are circuit breakers

A circuit breaker is an electrical safety device designed to protect an electrical circuit from damage caused by an overcurrent or short circuit. Its basic function is to interrupt current flow to protect equipment and to prevent the risk of fire. Unlike a fuse, which operates once and then must be replaced, a circuit breaker can be reset (either manually or automatically) to resume normal operation.

The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all. Usually, you’ll also want some kind of monitor alert if the circuit breaker trips.

Read full article

Top comments (0)