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

@carbonteq/resilience

v0.0.1

Published

A resilience and transient-fault-handling library that allows developers to express policies such as Backoff, Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. Inspired by .NET Polly.

Downloads

2

Readme

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.

Built With

This project is built on the top of cockatiel.

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

  1. Install redis sampling breaker package
    npm i @carbonteq/resilience

Usage

Then go forth with sampling breaker:

import {
  ExponentialBackoff,
  retry,
  handleAll,
  circuitBreaker,
  wrap,
  RedisSamplingBreaker
} from '@carbonteq/resilience';
import axios from "axios";
import Redis from "ioredis";

// 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 RedisSamplingBreaker({ threshold: 0.2, duration: 30 * 1000, redisClient: new Redis() }),
});

// 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 axios from "axios";
import Redis from "ioredis";
import {handleAll, rateLimiter,SlidingWindowCounterDriver } from "@carbonteq/resilience";

exports.handleRequest = async (req, res) => {
  const hash = crypto.createHash("md5").update(req.ip).digest("hex");
  const rateLimiterPolicy = rateLimiter(handleAll, {
    redisClient: new Redis(),
    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 axios from "axios";
import Redis from "ioredis";
import { handleAll, rateLimiter,LeakyBucketDriver } from "@carbonteq/resilience";

exports.handleRequest = async (req, res) => {
  const hash = crypto.createHash("md5").update(req.ip).digest("hex");
  const rateLimiterPolicy = rateLimiter(handleAll, {
    redisClient: new Redis(),
    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,
  rateLimiter,
  RedisSamplingBreaker,
  SlidingWindowCounterDriver
} from '@carbonteq/resilience';
import axios from "axios";
import Redis from "ioredis";

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

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, {
      redisClient: new Redis(),
      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);
};

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!

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