Coder Social home page Coder Social logo

ratelimiter's Introduction

Rate Limiter

Rate Limiter - X Requests per Y Seconds - Server Side Using Files

HOW TO USE

ALLOW WRITE PERMISSION

Simple Example

<?php
require_once('ratelimiter.php');
//...

function getFromAPI(){
	if (ratelimiter(8, 1)){//8 REQUESTS PER SECOND
		return file_get_contents("API_LINK");
	}
	else{
		return false;
	}
}

Test Example

<?php
require_once('ratelimiter.php');

for ($i=1; $i <= 10; $i++) { 
	if (ratelimiter(8, 1)){//8 REQUESTS PER SECOND
		echo "$i - REQUEST DONE";
	}
	else{
		echo "$i - ERROR - Rate Limit";
	}	
}

Sleep Example - (Wait Example)

<?php
require_once('ratelimiter.php');

for ($i=1; $i <= 10; $i++) { 
	$rate = ratelimiter(7, 1, false);
	while(!ratelimiter(7, 1, true)){//ONLY VERIFY
		usleep(1 * 1000000);
	}
}

MULTIPLE RATE-LIMITS

<?php
require_once('ratelimiter.php');

for ($i=1; $i <= 10; $i++) {
	if (ratelimiter(5, 1, false, 'apple')){
		echo 'Eat apple';
	}
	else
	{
		echo 'Error - apple';
	}

	if (ratelimiter(5, 1, false, 'orange')){
		echo 'Eat orange';
	}
	else
	{
		echo 'Error - orange';
	}
}

CHECK IF HAS ALLOWANCE

<?php
require_once('ratelimiter.php');

for ($i=1; $i <= 5; $i++) {
	if (ratelimiter(10, 1)){
		echo 'Foo';
	}
	else
	{
		echo 'Error';
	}
}
var_dump(ratelimiter(10, 1, true));
//return true

NOTES

The function parameters define the file name. Examples:

ratelimiter(3, 2)

  • request_allowance__3_2.dat
  • request_lastcheck__3_2.dat

ratelimiter(10, 1)

  • request_allowance__10_1.dat
  • request_lastcheck__10_1.dat

ratelimiter(10, 1, false, 'CUSTOM_NAME')

  • request_allowance_CUSTOM_NAME_10_1.dat
  • request_lastcheck_CUSTOM_NAME_10_1.dat

Donation

paypal

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.