Giter VIP home page Giter VIP logo

fastify-circuit-breaker's Introduction

fastify-circuit-breaker

js-standard-style CI workflow

A low overhead circuit breaker for your routes.

Install

npm i fastify-circuit-breaker

Usage

Register the plugin and if needed pass to it some custom option.
This plugin will add an onSend hook and expose a circuitBreaker utility.
Call fastify.circuitBreaker() when declaring the preHandler option of a route, in this way you will put that very specific route under the circuit breaking check.

const fastify = require('fastify')()

fastify.register(require('fastify-circuit-breaker'))

fastify.register(function (instance, opts, next) {
  instance.route({
    method: 'GET',
    url: '/',
    schema: {
      querystring: {
        error: { type: 'boolean' },
        delay: { type: 'number' }
      }
    },
    preHandler: instance.circuitBreaker(),
    handler: function (req, reply) {
      setTimeout(() => {
        reply.send(
          req.query.error ? new Error('kaboom') : { hello: 'world' }
        )
      }, req.query.delay || 0)
    }
  })
  next()
})

fastify.listen(3000, err => {
  if (err) throw err
  console.log('Server listening at http://localhost:3000')
})

Options

You can pass the following options during the plugin registration, in this way that values will be used in all the routes.

fastify.register(require('fastify-circuit-breaker'), {
  threshold: 3, // default 5
  timeout: 5000, // default 10000
  resetTimeout: 5000 // default 10000
})
  • threshold: is the maximum numbers of failures you accept to have before opening the circuit.
  • timeout: is the maximum number of milliseconds you can wait before return a TimeoutError.
  • resetTimeout: number of milliseconds before the circuit will move from open to half-open

Otherwise you can customize every single route by passing the same options to the circuitBreaker utility:

fastify.circuitBreaker({
  threshold: 3, // default 5
  timeout: 5000, // default 10000
  resetTimeout: 5000 // default 10000
})

If you pass the options directly to the utility, it will take precedence over the global configuration.

Customize error messages

If needed you can change the default error message for the circui open error and the timeout error:

fastify.register(require('fastify-circuit-breaker'), {
  timeoutErrorMessage: 'Ronf...', // default 'Timeout'
  circuitOpenErrorMessage: 'Oh gosh!' // default 'Circuit open'
})

Caveats

Since it is not possible to apply the classic timeout feature of the pattern, in this case the timeout will measure the time that the route takes to execute and once the route has finished if the time taken is higher than the timeout we will return an error, even if the route has produced a successful response.

If you need a classic circuit breaker to wrap some API call inside your application consider using easy-breaker.

Acknowledgements

Image curtesy of Martin Fowler.

License

MIT

Copyright © 2018 Tomas Della Vedova

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.