Giter Club home page Giter Club logo

fastify-circuit-breaker's Introduction

@fastify/circuit-breaker

CI NPM version js-standard-style

A low overhead circuit breaker for your routes.

Install

npm i @fastify/circuit-breaker

Usage

Register the plugin and, if needed, pass it custom options.
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({ port: 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 the values will be used in all routes.

fastify.register(require('@fastify/circuit-breaker'), {
  threshold: 3, // default 5
  timeout: 5000, // default 10000
  resetTimeout: 5000, // default 10000
  onCircuitOpen: async (req, reply) => {
    reply.statusCode = 500
    throw new Error('a custom error')
  },
  onTimeout: async (req, reply) => {
    reply.statusCode = 504
    return 'timed out'
  }
})
  • threshold: is the maximum number of failures accepted 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
  • onCircuitOpen: async function that gets called when the circuit is open due to errors. It can modify the reply and return a string | Buffer | Stream payload. If an Error is thrown it will be routed to your error handler.
  • onTimeout: async function that gets called when the circuit is open due to timeouts. It can modify the reply and return a string | Buffer | Stream | Error payload. If an Error is thrown it will be routed to your error handler.

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 circuit 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 it will return an error, even if the route has produced a successful response.

If you need a classic circuit breaker to wrap around an API call consider using easy-breaker.

Acknowledgements

Image curtesy of Martin Fowler.

License

MIT

Copyright © 2018 Tomas Della Vedova

fastify-circuit-breaker's People

Contributors

climba03003 avatar dancastillo avatar delvedor avatar dependabot-preview[bot] avatar dependabot[bot] avatar eomm avatar fdawgs avatar frikille avatar github-actions[bot] avatar greenkeeper[bot] avatar gurgunday avatar jsumners avatar kibertoad avatar mcollina avatar mrsabs avatar ryhinchey avatar salmanm avatar uzlopak avatar zekth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastify-circuit-breaker's Issues

fastify-circuit-breaker caused fastify instance crash ?

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.4.0

Plugin version

3.1.0

Node.js version

16

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Manjaro 22

Description

I try to hook this plugin into a normal fastify project, but I got a crash issue.

Steps to Reproduce

Here is a sample to reproduce:

const setTimeout = require('timers/promises').setTimeout;
const fastify = require('fastify')();
const cb = require('@fastify/circuit-breaker');

fastify.register(cb)
  .after(err => {
    if (err) {
      console.trace(`register plugins failed: ${err.message}`);
      throw err;
    }
  })
  .register(router);

async function router(fastify) {
  fastify.register(controller, {prefix: '/'});
  fastify.addHook('preHandler', fastify.circuitBreaker());
}

async function controller(fastify) {
  fastify.get('/', async function (req, reply) {
    // make a long time request manually, and expect a timeout response
    await setTimeout(60000);
    reply.code(200).send({health: 'OK'});
  });

  // ... Omit other URL handling
}


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

I have a normal action fastify.get('/'), and I wish this plugin could work when timeout.

node index.js

# open another terminal
curl -v localhost:3000

And curl seems working (but why I have to wait until 1 min, not default 10 s?):

$ curl -v localhost:3000
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.86.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 503 Service Unavailable
< content-type: application/json; charset=utf-8
< content-length: 109
< Date: Wed, 21 Dec 2022 15:09:53 GMT
< Connection: keep-alive
< Keep-Alive: timeout=72
< 
* Connection #0 to host localhost left intact
{"statusCode":503,"code":"FST_ERR_CIRCUIT_BREAKER_TIMEOUT","error":"Service Unavailable","message":"Timeout"}

And node index.js crashed:

$ node index.js
Server listening at http://localhost:3000
node:events:491
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:393:5)
    at ServerResponse.end (node:_http_outgoing:968:15)
    at /tmp/tmp/node_modules/fastify/lib/error-handler.js:43:17
    at fallbackErrorHandler (/tmp/tmp/node_modules/fastify/lib/error-handler.js:130:3)
    at handleError (/tmp/tmp/node_modules/fastify/lib/error-handler.js:33:5)
    at onErrorHook (/tmp/tmp/node_modules/fastify/lib/reply.js:742:5)
    at wrapOnSendEnd (/tmp/tmp/node_modules/fastify/lib/reply.js:535:5)
    at handleReject (/tmp/tmp/node_modules/fastify/lib/hooks.js:238:5)
Emitted 'error' event on ServerResponse instance at:
    at emitErrorNt (node:_http_outgoing:827:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}

Expected Behavior

I hope I can get timeout response when circuit-breaker trigger timeout event, and fastify instance should not crashed.

Optionally breaking the circuit without having to wait for the route handler to finish

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

So I saw that on the caveats section, the handler will just wait for the response to be generated anyway and just send an error if the timeout has been reached.

I wonder, with something like p-timeout and p-cancelable, would it not be possible to cancel the Promise without having to wait for it?

For example, you could simply check that a route handler promise has a .cancel method (as is the case when you use p-cancelable), and cancel those promises when the timeout is hit; and when it’s a “vanilla” promise, just fall back to the default behaviour as-is.

That way, we can support “not having to wait for route handler to finish” and actually use the timeout as a timeout while not breaking anything (minor semver) and still supporting the existing behaviour.

Motivation

No response

Example

No response

Feature Request - Percentage based error thresholds

🚀 Feature Proposal

Add support for percentage based error thresholds. Happy to work on this if it's something that'd be a good addition to the library.

Motivation

Often a threshold percentage makes more sense and is what developers might be used to if they've used a library like Oppossum before.

Example

I think we could reuse the existing threshold option. Pass an integer to keep the existing behavior or pass a float to enable a percentage based calculation.

fastify.register(require('fastify-circuit-breaker'), {
  threshold: 0.5 // enables percentage based calculation
})

how i make Custom Reply :(

💬 how i make Custom Reply

I'm checked this method [(fastify.setErrorHandler) ] but not helpful its work after threshold limit but in (TimeoutError) i can't make custom reply.

i need help for this problem.

And Thanks for this beautiful library ^_^

index.d.ts lost onCircuitOpen and onTimeout in FastifyCircuitBreakerOptions

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.4.0

Plugin version

3.1.0

Node.js version

16

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Manjaro 22

Description

It seems index.d.ts lost onCircuitOpen, onTimeout, timeoutErrorMessage and circuitOpenErrorMessage in FastifyCircuitBreakerOptions

export type FastifyCircuitBreakerOptions = {
/**
* The maximum numbers of failures you accept to have before opening the circuit.
* @default 5
*/
threshold?: number;
/**
* The maximum number of milliseconds you can wait before return a `TimeoutError`.
* @default 10000
*/
timeout?: number;
/**
* The number of milliseconds before the circuit will move from `open` to `half-open`.
* @default 10000
*/
resetTimeout?: number;
};

When I try to use fastify-circuit-breaker in typescript, this will not working:

import fastifyCircuitBreaker from '@fastify/circuit-breaker';

// ...
fastify.register(fastifyCircuitBreaker, {
  threshold: 3, // default 5
  timeout: 5000, // default 10000
  resetTimeout: 5000, // default 10000
  onCircuitOpen: async (req, reply) => {
    reply.statusCode = 500
    throw new Error('a custom error')
  },
  onTimeout: async (req, reply) => {
    reply.statusCode = 504
    return 'timed out'
  })

Will show error in building:

[tsl] ERROR in xxx.ts
      TS2769: No overload matches this call.
  Overload 1 of 3, '(plugin: FastifyPluginCallback<FastifyCircuitBreakerOptions, Server, FastifyTypeProviderDefault>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '{ threshold: number; timeout: number; resetTimeout: number; onCircuitOpen: (req: any, reply: any) => Promise<never>; onTimeout: (req: any, reply: any) => Promise<string>; }' is not assignable to parameter of type 'FastifyRegisterOptions<FastifyCircuitBreakerOptions> | undefined'.
      Object literal may only specify known properties, and 'onCircuitOpen' does not exist in type 'FastifyRegisterOptions<FastifyCircuitBreakerOptions>'.
  Overload 2 of 3, '(plugin: FastifyPluginAsync<FastifyCircuitBreakerOptions, Server, FastifyTypeProviderDefault>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'FastifyCircuitBreaker' is not assignable to parameter of type 'FastifyPluginAsync<FastifyCircuitBreakerOptions, Server, FastifyTypeProviderDefault>'.
  Overload 3 of 3, '(plugin: FastifyPluginCallback<FastifyCircuitBreakerOptions, Server, FastifyTypeProviderDefault> | FastifyPluginAsync<...> | Promise<...> | Promise<...>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '{ threshold: number; timeout: number; resetTimeout: number; onCircuitOpen: (req: any, reply: any) => Promise<never>; onTimeout: (req: any, reply: any) => Promise<string>; }' is not assignable to parameter of type 'FastifyRegisterOptions<FastifyCircuitBreakerOptions> | undefined'.
      Object literal may only specify known properties, and 'onCircuitOpen' does not exist in type 'FastifyRegisterOptions<FastifyCircuitBreakerOptions>'.
[tsl] ERROR in xxx.ts
      TS7006: Parameter 'req' implicitly has an 'any' type.
[tsl] ERROR xxx.ts
      TS7006: Parameter 'reply' implicitly has an 'any' type.
[tsl] ERROR xxx.ts
      TS7006: Parameter 'req' implicitly has an 'any' type.
[tsl] ERROR xxx.ts
      TS7006: Parameter 'reply' implicitly has an 'any' type.

Steps to Reproduce

As above

Expected Behavior

No response

circuitBreaker decorator is not available when used outside of a plugin

🐛 Bug Report

When adding a circuit breaker to a route that is not inside of a plugin, circuitBreaker is not available on the fastify instance.

To Reproduce

Steps to reproduce the behavior:

const fastify = require('fastify')()

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

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

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

Expected behavior

fastify.circuitBreaker is available and acts as expected. Currently, an error is thrown: TypeError: fastify.circuitBreaker is not a function

Your Environment

  • node version: 12.17.0
  • fastify version: 3.12.0
  • os: Mac

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.