All Projects → spatie → Guzzle Rate Limiter Middleware

spatie / Guzzle Rate Limiter Middleware

Licence: mit
A rate limiter middleware for Guzzle

Projects that are alternatives of or similar to Guzzle Rate Limiter Middleware

Guzzle Services
Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.
Stars: ✭ 226 (+140.43%)
Mutual labels:  api, guzzle
Guzzle Advanced Throttle
A Guzzle middleware that can throttle requests according to (multiple) defined rules. It is also possible to define a caching strategy, e.g. get the response from cache when the rate limit is exceeded or always get a cached value to spare your rate limits. Using wildcards in host names is also supported.
Stars: ✭ 120 (+27.66%)
Mutual labels:  api, guzzle
Python Nomad
Client library Hashicorp Nomad
Stars: ✭ 90 (-4.26%)
Mutual labels:  api
Tutorialdb
A search 🔎 engine for programming/dev tutorials, See it in action 👉
Stars: ✭ 93 (-1.06%)
Mutual labels:  api
Tpp
Repositório central para organização total (Issues, README's, LICENSE's, etc)
Stars: ✭ 92 (-2.13%)
Mutual labels:  api
Habanero
client for Crossref search API
Stars: ✭ 90 (-4.26%)
Mutual labels:  api
Apipeline
Feature-rich and pluggable offline-first API wrapper for all your javascript environements ! Easily wire-up your API and make your app work offline in minutes.
Stars: ✭ 92 (-2.13%)
Mutual labels:  api
Run
⚡The resource runtime
Stars: ✭ 90 (-4.26%)
Mutual labels:  api
Telebot.nim
Async client for Telegram Bot API in pure Nim [Bot API 5.1]
Stars: ✭ 93 (-1.06%)
Mutual labels:  api
Adoc
📄🖊轻松的的 API MD文档编写工具
Stars: ✭ 92 (-2.13%)
Mutual labels:  api
Tesla Api
🚘 A Ruby gem and unofficial documentation of Tesla's JSON API for the Model S, 3, X, and Y.
Stars: ✭ 1,317 (+1301.06%)
Mutual labels:  api
Rapidql
Query multiple APIs and DBs and join them in a single query
Stars: ✭ 91 (-3.19%)
Mutual labels:  api
Deluge Webapi
Plugin for Deluge WebUI providing sane JSON API
Stars: ✭ 90 (-4.26%)
Mutual labels:  api
Behat Api Extension
API extension for Behat, used to ease testing of JSON-based APIs
Stars: ✭ 92 (-2.13%)
Mutual labels:  api
Packagist Api
The easiest way to work with the packagist API
Stars: ✭ 90 (-4.26%)
Mutual labels:  api
Discord.jl
The Julia Discord API Wrapper
Stars: ✭ 93 (-1.06%)
Mutual labels:  api
Go Tgbot
Golang telegram bot API wrapper, session-based router and middleware
Stars: ✭ 90 (-4.26%)
Mutual labels:  api
Amadeus Node
Node library for the Amadeus Self-Service travel APIs
Stars: ✭ 91 (-3.19%)
Mutual labels:  api
Api Client Generator
Angular REST API client generator from Swagger YAML or JSON file with camel case settigs
Stars: ✭ 92 (-2.13%)
Mutual labels:  api
X
新生命X组件,数据中间件XCode、日志、网络、RPC、序列化、缓存、Windows服务
Stars: ✭ 1,322 (+1306.38%)
Mutual labels:  api

A rate limiter middleware for Guzzle

Latest Version on Packagist Build Status Quality Score StyleCI Total Downloads

A rate limiter middleware for Guzzle. Here's what you need to know:

  • Specify a maximum amount of requests per minute or per second
  • When the limit is reached, the process will sleep until the request can be made
  • Implement your own driver to persist the rate limiter's request store. This is necessary if the rate limiter needs to work across separate processes, the package ships with an InMemoryStore.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/guzzle-rate-limiter-middleware

Usage

Create a Guzzle middleware stack and register it on the client.

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;

$stack = HandlerStack::create();
$stack->push(RateLimiterMiddleware::perSecond(3));

$client = new Client([
    'handler' => $stack,
]);

You can create a rate limiter to limit per second or per minute.

RateLimiterMiddleware::perSecond(3); // Max. 3 requests per second

RateLimiterMiddleware::perMinute(5); // Max. 5 requests per minute

Custom stores

By default, the rate limiter works in memory. This means that if you have a second PHP process (or Guzzle client) consuming the same API, you'd still possibly hit the rate limit. To work around this issue, the rate limiter's state should be persisted to a cache. Implement the Store interface with your own cache, and pass the store to the rate limiter.

use MyApp\RateLimiterStore;
use Spatie\GuzzleRateLimiterMiddleware\RateLimit;

RateLimiterMiddleware::perSecond(3, new RateLimiterStore());

A Laravel example of a custom Store:

<?php

namespace MyApp;

use Spatie\GuzzleRateLimiterMiddleware\Store;
use Illuminate\Support\Facades\Cache;

class RateLimiterStore implements Store
{
    public function get(): array
    {
        return Cache::get('rate-limiter', []);
    }

    public function push(int $timestamp, int $limit)
    {
        Cache::put('rate-limiter', array_merge($this->get(), [$timestamp]));
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].