🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More
Socket
Sign inDemoInstall
Socket

redis-sampling-breaker

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

redis-sampling-breaker

Redis Sampling Breaker

0.0.14
unpublished
latest
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Redis Sampling Breaker

Redis Sampling Breaker is built on [Cockatiel](https://github.com/connor4312/cockatiel) with redis.

Table of Contents
  • About The Project
  • Getting Started
  • Usage
  • Contributing

About The Project

An enhancement to a circuit breaker with a rate limiter to protect your application from overloading due to excessive requests. The breaker can be used to stop sending requests to a service that is failing or is overloaded together with the rate limiter that limits the number of requests that can be made within a certain time period. Together, circuit breaker and rate limiter can help prevent your application from crashing or becoming unresponsive due to too many requests.

(back to top)

Built With

This project is built on the top of cockatiel.

(back to top)

Getting Started

You should have a basic setup of nodejs project using typescript

Prerequisites

Should have a sound knowledge of Circuit Breaker, Rate Limiter and typescript. Redis should be installed on your machine

Installation

  • Install redis sampling breaker package
    npm i redis-sampling-breaker
    

(back to top)

Usage

Then go forth with sampling breaker:

import {
  ExponentialBackoff,
  retry,
  handleAll,
  circuitBreaker,
  wrap,
} from 'cockatiel';
import axios from "axios";
import { SamplingBreaker } from "redis-sampling-breaker";
// Create a retry policy that'll try whatever function we execute 3
// times with a randomized exponential backoff.
const retry = retry(handleAll, { maxAttempts: 3, backoff: new ExponentialBackoff() });

// Create a circuit breaker that'll stop calling the executed function for 10
// seconds if it fails 5 times in a row. This can give time for e.g. a database
// to recover without getting tons of traffic.
const circuitBreakerPolicy = circuitBreaker(handleAll, {
  halfOpenAfter: 10 * 1000,
  breaker: new SamplingBreaker({ threshold: 0.2, duration: 30 * 1000 }),
});

// Combine these! Create a policy that retries 3 times, calling through the circuit breaker
const retryWithBreaker = wrap(retry, circuitBreakerPolicy);

exports.handleRequest = async (req, res) => {
  const data = await retryWithBreaker.execute(() => axios.get("http://127.0.0.1:8080"));
  return res.json(data);
};

With rate limiting policy and sliding window counter driver:

import * as crypto from "crypto"
import {
  handleAll,
} from 'cockatiel';
import axios from "axios";
import { rateLimiter,SlidingWindowCounterDriver } from "redis-sampling-breaker";

exports.handleRequest = async (req, res) => {
  const hash = crypto.createHash("md5").update(req.ip).digest("hex");
  const rateLimiterPolicy = rateLimiter(handleAll, {
    driver: new SlidingWindowCounterDriver({
      hash: hash,
      maxWindowRequestCount: 5,
      intervalInSeconds: 1 * 60,
    }),
  });

  const data = await rateLimiterPolicy.execute(() => axios.get("http://127.0.0.1:8080"));
  return res.json(data);
};

With rate limiting policy and leaky bucket driver:

import * as crypto from "crypto"
import {
  handleAll,
} from 'cockatiel';
import axios from "axios";
import { rateLimiter,LeakyBucketDriver } from "redis-sampling-breaker";

exports.handleRequest = async (req, res) => {
  const hash = crypto.createHash("md5").update(req.ip).digest("hex");
  const rateLimiterPolicy = rateLimiter(handleAll, {
    driver: new LeakyBucketDriver({
      hash: hash,
      bucketSize: 5,
      fillRate: 10,
    }),
  });

  const data = await rateLimiterPolicy.execute(() => axios.get("http://127.0.0.1:8080"));
  return res.json(data);
};

Wrap both rate limiting and sampling breaker:

import * as crypto from "crypto"
import {
  handleAll,
  retry,
  circuitBreaker
} from 'cockatiel';
import axios from "axios";
import { rateLimiter,SamplingBreaker,SlidingWindowCounterDriver } from "redis-sampling-breaker";

const circuitBreakerPolicy = circuitBreaker(handleAll, {
  halfOpenAfter: 10 * 1000,
  breaker: new SamplingBreaker({ threshold: 0.2, duration: 30 * 1000 }),
});

const retryPolicy = retry(handleAll, {
  maxAttempts: 3,
  backoff: new ExponentialBackoff(),
});
exports.handleRequest = async (req, res) => {
    const hash = crypto.createHash("md5").update(req.ip).digest("hex");
    const rateLimiterPolicy = rateLimiter(handleAll, {
      driver: new SlidingWindowCounterDriver({
        hash: hash,
        maxWindowRequestCount: 5,
        intervalInSeconds: 1 * 60,
      }),
    });

  
  const retryWithBreaker = wrap(redisRateLimiterPolicy,retryPolicy,circuitBreakerPolicy);
  const data = await retryWithBreaker.execute(() => axios.get("http://127.0.0.1:8080"));
  return res.json(data);
};

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  • Fork the Project
  • Create your Feature Branch (git checkout -b feature/AmazingFeature)
  • Commit your Changes (git commit -m 'Add some AmazingFeature')
  • Push to the Branch (git push origin feature/AmazingFeature)
  • Open a Pull Request

(back to top)

FAQs

Package last updated on 09 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts