Giter VIP home page Giter VIP logo

rxjs-ratelimiter's Introduction

rxjs-ratelimiter

build status Known Vulnerabilities

An rxjs library to facilitate dealing with rate limited resources (say, external REST APIs). Great for use with @angular/http.

This library is fully tested using rxjs's marble testing and rxjs's TestScheduler to provide virtual time scheduling.

How to install

npm install -S rxjs-ratelimiter
yarn add rxjs-ratelimiter

How to use

This example uses angular 2:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import RateLimiter from 'rxjs-ratelimiter'

@Injectable()
export class RateLimitedApi {
  // allow at most 6 requests every 1000ms
  private rateLimiter = new RateLimiter(6, 1000)

  constructor(private http: Http) {}

  makeRequest(path: string): Promise<Response> {
    return this.rateLimiter.limit(
      this.http.get(`https://my.api/${path}`)
    ).toPromise()
  }
}

rateLimiter.limit returns an observable that rate limits the subscription to the passed observable. To understand how this works it is important to realise that cold observables create requests lazily (when subscribed to, not when instantiated). In this example the http request happens when the rate limiter passes a subscription through to the observable returned by this.http.get, not at the time that this.http.get is called.

Retrying requests

A consequence of the lazy nature of rate limited observable is that resubscriptions by operators chained from them (such as retry) will also be subject to rate limitation.

this.rateLimiter.limit(
  this.http.get(`https://my-api.co/`)
).pipe(retry())

This will produce an observable that will retry the HTTP request until it succeeds. The rate limiter also applies to the retries so at most one request can happen per second, less if the rate limiter is being used for other requests. To have the rate limiter apply to the initial request but not the retries the following modification can be made:

this.rateLimiter.limit(this.http.get(`https://some.api/`).pipe(retry()))

rxjs 5 support

Version 0.1.1 supports rxjs5. Versions 1.0.0 and onward will only support rxjs 6 (and later).

rxjs-ratelimiter's People

Contributors

greenkeeper[bot] avatar insidewhy avatar jaufgang avatar renovate-bot avatar renovate[bot] avatar

Stargazers

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

Watchers

 avatar  avatar

rxjs-ratelimiter's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update circleci/node docker tag to v17
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-config-prettier to v9
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency mocha to v10 (mocha, @types/mocha)
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency pretty-quick to v4
  • chore(deps): update dependency typescript to v5
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
  • circleci/node 10
npm
package.json
  • @types/chai 4.2.11
  • @types/mocha 5.2.7
  • @typescript-eslint/eslint-plugin 2.3.1
  • @typescript-eslint/parser 2.3.1
  • chai 4.2.0
  • concurrently 4.1.2
  • eslint 6.8.0
  • eslint-config-prettier 6.3.0
  • husky 3.1.0
  • mocha 6.2.0
  • prettier 1.18.2
  • pretty-quick 1.11.1
  • rxjs 6.5.3
  • typescript 3.6.3
  • rxjs ^6

  • Check this box to trigger a request for Renovate to run again on this repository

Support rxjs 7

This library has a peer dependency to rxjs 6. I guess it would support rxjs 7, too.

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.