npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hystrix-controller

v1.0.15

Published

Simplify your hystrixjs architecture, open and close circuits for testing, check circuit health, and more.

Downloads

22

Readme

Hystrix Controller

This library adds to the functionality of hystrixjs to help give developers a simpler solution and more functionality then the library provides by itself. Such as command managament (create, add, update, reset), command getters, circuit health checks, circuit metrics, and a controller allowing for opening and closing of circuits. All the developer needs to do is create a config.

This documentation does a good job covering everything you need to create an application using hystrix but I recommended reading the documentation for hystrixjs to gain further knowledge. Please find that information here https://www.npmjs.com/package/hystrixjs.

For hystrix monitoring I have added a method "hystrixjs" which will return the hystrix instance where the developer can access hystrixSSEStream to create monitoring.

To create Hystrix Commands

    const HystrixController = require('hystrix-controller');
    const config = require('../hystrix/hystrix-service-config')();

    HystrixController(config).createCommands();

Config

Config must be an array of configurations for each hystrix command example:

    const config = [{
        name: 'command',
        requestModel: requestFunc,
        errorThreshold: 10,
        timeout: 10000,
        requestVolumeThreshold: 10,
        sleepWindowInMilliseconds: 1000,
        statisticalWindowLength: 10000,
        statisticalWindowNumberOfBuckets: 10000,
        errorHandler: errorHandlerFunc,
        fallbackTo: hystrixFallbackFunc
    }]

hystrix command options:

• sleepWindowInMilliseconds - How long the circuit breaker should stay opened, before allowing a single request to test the health of the service

• errorHandler - Function to validate if the error response from the service is an actual error. If this function returns an error object (default implementation), this request call will be marked as failure, which will influence the error percentage. If it returns null or false, the call will not be marked as failure. An example could be a 404 error, if the customer is not found.

• requestModel - The function that is called when the developer runs the execution method on the hystrix command. Must return a promise.

• timeout: Timeout in milliseconds for request

• requestVolumeThreshold - minimum number of requests in a rolling window that needs to be exceeded, before the circuit breaker will bother at all to calculate the health

• forceOpened - force this circuit breaker to be always opened

• forceClosed - force this circuit breaker to be always closed

• errorThreshold - error percentage threshold to trip the circuit

• statisticalWindowLength - length of the window to keep track of execution counts metrics (success, failure)

• statisticalWindowNumberOfBuckets - number of buckets within the statistical window

• percentileWindowNumberOfBuckets - number of buckets within the percentile window

• percentileWindowLength - length of the window to keep track of execution times

• requestVolumeRejectionThreshold - maximum number of concurrent requests, which can be executed. Defaults to 0, i.e. no limitation

• fallbackTo - function, which will be executed if the request fails. The function will be called with the error as the 1st argument and an array of the original args as the 2nd argument'

Methods avaliable:

createCommands: Creates and saves all hystrix commands using the Config passed to HystrixController. This will also run the createCommands method.

   const HystrixController = require('hystrix-controller')
   const Config = require('./hystrix-config')

   HystrixController(Config).createCommands()

getConfig: returns the current config.

   const HystrixController = require('hystrix-controller')

   HystrixController().getConfig()
   <!-- returns [{
       name: 'command',
       requestModel: requestFunc,
       errorThreshold: 10,
       timeout: 10000,
       requestVolumeThreshold: 10,
       sleepWindowInMilliseconds: 1000,
       statisticalWindowLength: 10000,
       statisticalWindowNumberOfBuckets: 10000,
       errorHandler: errorHandlerFunc,
       fallbackTo: hystrixFallbackFunc
   }] -->

getServiceCommands: returns hystrix serviceCommands to be used in the application.

   const HystrixController = require('hystrix-controller')

   const { getUser } = HystrixController().getServiceCommands()

   getUser.execute(request)
       .then((user) => {
           return user
       })

updateHystrix: Let the developer update the config and serviceCommands with a new config Array. Will update state of the hystrix-controller.

   const HystrixController = require('hystrix-controller')
   const newConfig = require('./new-config)

   HystrixController().updateHystrix(newConfig)

addCommand: Lets the developer add a command to their hystix. The method takes a normal hystrix config object as an argument. Will add the new command to the hystrix-contoller state and serviceCommands.

   const HystrixController = require('hystrix-controller')

   const newCommand = {...}
   
   HystrixController().addCommand(newCommand)

circuitHealth: Will return an Array of all hystrix circuits, their metrics, and circuit-status ("opened or closed"). Can also pass an argument to the circuitHealth method ("name of command") to return the health of its specific circuit health.

   const HystrixController = require('hystrix-controller')

   const circuitHealth = HystrixController().circuitHealth()
   
   <!-- [ 
       { 
           name: 'getUser', 
           circuitStatus: 'opened',
           metrics: {
               errorCount: 2,
               errorPercentage: 16,
               successCount: 10,
               totalCount: 12
           }
       }, { 
           name: 'getFriends',
           circuitStatus: 'closed',
           metrics: {
               errorCount: 0,
               errorPercentage: 0,
               successCount: 12,
               totalCount: 12
           }
       } ] -->

getCircuitStatus: Will return status of passed in circuit "Name" status will be either open or closed.

   const HystrixController = require('hystrix-controller')

   const circuitHealth = HystrixController().getCircuitStatus('getUser')
   
   <!-- 'closed' -->

resetCache: hystrixjs holds state and this can make testing tricky depending on the test cases. The resetCache method resets the current state of hystrix. ResetCache also can take one argument a string if the developer wants to clear the state for a specific factory "metrics, circuit, command". If the method is called without an argument all three factories state will be reset.

   const HystrixController = require('hystrix-controller')

   HystrixController().resetCache()

   <!-- resets state for all factories -->

   HystrixController().resetCache('metrics')

   <!-- resets state for  metricsFactory: -->

controller: Allows developer to open, close, or reset circuit or circuits.

The controller method takes an Object as an argument with two nodes "services" which needs to be an array with the services the developer wishes to change the circuit status. This will be the name of service from the config the developer is using. "All" is also an option if the developer wishes to effect all circuits.

The second node "circuitStatus" is the status the developer want to set the service/services circuit to: open, close, reset. Open forces the circuit open, close force the circuit closed, and reset sets forceClose and forceOpen to config settings, closes circuit, and clears metric cache for the circuit.

   const HystrixController = require('hystrix-controller')

   HystrixController().controller({services: ['getUser'], circuitStatus: 'open'})
       
       <!-- 'getUser circuit has been opened' -->
   
   HystrixController().getCircuitStatus('getUser') 

       <!-- 'opened' -->

   HystrixController().controller({services: ['getUser'], circuitStatus: 'reset'})

       <!-- 'closed' -->

   HystrixController().controller({services: ['all'], circuitStatus: 'open'})
       
       <!-- 'getUser, getFriends circuit has been opened' -->

   HystrixController().circuitHealth()

       <!-- [ 
           { name: 'getUser', circuitStatus: 'opened', metrics: {...} }, 
           { name: 'getFriends', circuitStatus: 'opened', metrics: {...} } 
       ] -->

   HystrixController().controller({services: ['all'], circuitStatus: 'reset'})

       <!-- [ 
           { name: 'getUser', circuitStatus: 'closed', metrics: {...} }, 
           { name: 'getFriends', circuitStatus: 'closed', metrics: {...} } 
       ] -->