
Redis का उपयोग करके बेहतर PHP रेट लिमिटिंग।
Limitrr PHP मेरी दूसरी लाइब्रेरी, Limitrr से बहुत अधिक प्रेरित है, जिसे NodeJS के लिए बनाया गया था। इसे यहाँ देखें।
Limitrr PHP उपयोगकर्ताओं को अपने एप्लिकेशन में आसानी से दर सीमा (रेट लिमिटिंग) एकीकृत करने की अनुमति देता है। अन्य समान पैकेजों के विपरीत, यह उपयोगिता उपयोगकर्ता को न केवल अनुरोधों की संख्या से, बल्कि पूर्ण किए गए कार्यों की संख्या (जैसे किसी समयावधि में सफलतापूर्वक बनाए गए खातों की एक निश्चित संख्या की अनुमति देना) से भी सीमित करने की अनुमति देती है, और ऐसे प्रतिबंधों को कस्टम विकल्पों के साथ लागू करती है। इसके अलावा, कस्टम डिस्क्रिमिनेटर संभव हैं - अब आपको केवल उपयोगकर्ता के IP से सीमित नहीं होना होगा।
यह लाइब्रेरी SlimPHP प्रोजेक्ट के भीतर विभिन्न रूट्स पर आसानी से दर सीमित करने के लिए एक मिडलवेयर फ़ंक्शन भी प्रदान करती है।
यदि आप इस प्रोजेक्ट की सराहना करते हैं, तो कृपया इसे GitHub पर 🌟 करें।
पुल रिक्वेस्ट का स्वागत है
आप limitrr-php लाइब्रेरी को अपने टर्मिनल में निम्नलिखित कमांड लाइन चलाकर इंस्टॉल कर सकते हैं (मान लें कि आपके पास composer इंस्टॉल है):
composer require eddiejibson/limitrr-php "^1.0"
require "/vendor/autoload.php"; //Require composer's autoload
$options = [
//Redis keystore information
"redis" => [
"host" => "666.chae.sh",
"port" => 6379,
"password" => "supersecret",
],
"routes" => [
"default" => [
"requestsPerExpiry" => 5,
],
],
];
//Initialize the Limitrr class and pass the options defined above into it
//Note that the options are not required.
$limitrr = new \eddiejibson\limitrr\Limitrr($options);
//Various examples like this can be found further into the documentation,
//for each function.
$result = $limitrr->get(["discriminator" => $ip]);
echo $result["requests"] + " Requests";
echo $result["completed"] + " Completed";
//Note that this library is no means just for SlimPHP, it just happens to
//provide a middleware function for those who may need it.
//Usage within SlimPHP
$app = new Slim\App();
//Use the Limitrr SlimPHP middleware function, if you wish:
$app->add(new \eddiejibson\limitrr\RateLimitMiddleware($limitrr)); //Make sure to pass in the main Limitrr
//instance we defined above into the middleware function. This is mandatory.
//You can also add the get IP middleware function, it will append the user's real IP
//(behind Cloudflare or not) to the request.
$app->add(new \eddiejibson\limitrr\getIpMiddleware());
//Example usage within a route
$app->get("/hello/{name}", function ($request, $response, $args) {
$name = $args["name"];
$ip = $request->getAttribute('realip'); //Get the IP that was defined within Limitrr's get IP middleware function
return $response->getBody()->write("Hello, ${name}. Your IP is ${ip}.");
});
//You do not have to app the middleware function to every single route, globally.
//You can do it indivually, too - along with passing options into such. Like so:
$app->get("/createUser/{name}", function ($request, $response, $args) {
//Non intensive actions like simple verification will have a different limit to intensive ones.
//and will only be measured in terms of each request via the middleware.
//No further action is required.
if (strlen($args["name"]) < 5) {
//Dummy function creating user
$res = $someRandomClass->registerUser();
if ($res) {
//Intensive actions like actually registering a user should have a
//different limit to normal requests, hence the completedActionsPerExpiry option.
//and should only be added to once this task has been completed fully
//In this example, we will be limiting the amount of completed actions a certain IP can make.
//Anything can be passed in here, however. For example, a email address or user ID.
//$request->getAttribute('realip') was determined by calling the middleware earlier - getIpMiddleware()
$limitrr->complete(["discriminator"] => $ip);
}
}
})->add(new \eddiejibson\limitrr\RateLimitMiddleware($limitrr, ["route"=>"createUser"]));
//You can also pass the route name within the limitrr middleware function
$app->run();
रिटर्न: Array
$limitrr->get([
"discriminator" => $discriminator, //आवश्यक
"route" => $route, //आवश्यक नहीं, डिफ़ॉल्ट मान लिया जाता है
"type" => $type //आवश्यक नहीं
]);
फ़ंक्शन में एक ऐरे के माध्यम से पास किया जाना चाहिए
default रूट से गणना प्राप्त करेगाrequests या completed निर्दिष्ट कर सकते हैं और केवल वही एक पूर्णांक के रूप में लौटाया जाएगा।$limitrr->get([
"discriminator" => $discriminator,
"type" => $type,
"route" => $route
]); //discriminator के अलावा, सभी पैरामीटर वैकल्पिक हैं।
//यदि फ़ंक्शन में type पास नहीं किया गया है, तो यह
//अनुरोधों और पूर्ण कार्यों दोनों की मात्रा लौटाएगा
//जहां discriminator वह चीज़ है जिसे सीमित किया जा रहा है
//जैसे प्रति discriminator पूर्ण कार्यों/अनुरोधों की x संख्या
$limitrr->get(["discriminator" => $discriminator]);
//यह आमतौर पर उपयोगकर्ता का IP होता है।
$limitrr->get(["discriminator" => $ip]);
//यह प्रदान किए गए discriminator के अंतर्गत संग्रहीत अनुरोधों और पूर्ण कार्यों दोनों की मात्रा को एक ऑब्जेक्ट में लौटाएगा। आप इसे इस तरह संभाल सकते हैं:
$result = $limitrr->get(["discriminator" => $ip]);
echo $result["requests"] + " Requests";
echo $result["completed"] + " Completed";
//उपरोक्त उदाहरण default रूट से अनुरोध और पूर्ण कार्य गणना प्राप्त करेगा।
//यदि आप किसी भिन्न रूट से मान प्राप्त करना चाहते हैं, तो आप इसे भी निर्दिष्ट कर सकते हैं।
//इसे इस तरह किया जा सकता है:
$result = $limitrr->get(["discriminator" => $ip, "route" => "exampleRouteName"]);
echo $result["requests"] . " Requests made through the route exampleRouteName";
echo $result["completed"] . " Completed Tasks made through the route exampleRouteName";
//आप केवल एक प्रकार का मान भी प्राप्त कर सकते हैं - अनुरोध और पूर्ण दोनों के बजाय।
$result = $limitrr->get(["discriminator" => $ip, "route" => "exampleRouteName", "type" => "completed"]);
echo $result["completed"] . " Completed tasks made through the route exampleRouteName";
रिटर्न: Integer
$limitrr->get([
"discriminator" => $discriminator, //आवश्यक
"route" => $route, //आवश्यक नहीं, डिफ़ॉल्ट मान लिया जाता है
]);
फ़ंक्शन में एक ऐरे के माध्यम से पास किया जाना चाहिए
default रूट से गणना प्राप्त करेगारिटर्न: Boolean
$limitrr->reset([
"discriminator" => $discriminator, //आवश्यक
"route" => $route, //आवश्यक नहीं, डिफ़ॉल्ट मान लिया जाता है,
"type" => $type //आवश्यक नहीं
]);
फ़ंक्शन में एक ऐरे के माध्यम से पास किया जाना चाहिए
default रूट से गणना रीसेट करेगाrequests या completed? यदि यह सेट नहीं है, तो दोनों हटा दिए जाएँगे।//जहां discriminator वह चीज़ है जिसे सीमित किया जा रहा है
//जैसे प्रति discriminator पूर्ण कार्यों/अनुरोधों की x संख्या
//यह अनुरोधों और पूर्ण कार्य गणना दोनों को हटा देगा
$limitrr->reset(["discriminator" => $discriminator]);
//यह आमतौर पर उपयोगकर्ता का IP होता है।
$limitrr->reset(["discriminator" => $ip]);
//यदि आप किसी विशेष रूट से गणना रीसेट करना चाहते हैं, तो यह भी किया जा सकता है।
//चूंकि type निर्दिष्ट नहीं है, यह अनुरोध और पूर्ण गणना दोनों को हटा देगा
$result = $limitrr->reset([
"discriminator" => $ip,
"route" => "exampleRouteName"
]);
if ($result) {
echo "Requests removed from the route exampleRouteName";
} else {
//कुछ और करें
}
//यदि आप अनुरोधों या पूर्ण कार्यों में से किसी एक को हटाना चाहते हैं,
//लेकिन दूसरे को नहीं, तो यह भी किया जा सकता है।
//पास किया गया मान "requests" या "completed" हो सकता है।
//इस उदाहरण में, हम एक निश्चित IP के लिए अनुरोध गणना हटाएँगे
$result = $limitrr->reset([
"discriminator" => $ip,
"type" => "requests"
]);
if ($result) {
echo "Request count for the specified IP were removed"
} else {
//कुछ और करें
}
आवश्यक: false
प्रकार: Array या String
विवरण: Redis कनेक्शन जानकारी।
या तो Redis इंस्टेंस का URI वाला एक स्ट्रिंग पास करें या कनेक्शन जानकारी वाला एक ऑब्जेक्ट:
6379"127.0.0.1"""0 //एक Redis URI वाला स्ट्रिंग पास करें।
"redis" => "redis://127.0.0.1:6379/0"
//वैकल्पिक रूप से, कनेक्शन जानकारी के साथ एक ऐरे का उपयोग करें।
"redis" => [
"port" => 6379, //Redis पोर्ट। आवश्यक: false. डिफ़ॉल्ट 6379
"host" => "127.0.0.1", //Redis होस्टनाम। आवश्यक: false. डिफ़ॉल्ट "127.0.0.1".
"password" => "mysecretpassword1234", //Redis पासवर्ड। आवश्यक: false. डिफ़ॉल्ट null.
"database" => 0 //Redis डेटाबेस। आवश्यक: false. डिफ़ॉल्ट 0.
]
आवश्यक: false
प्रकार: Array
विवरण: Limitrr से संबंधित विभिन्न विकल्प।
"limitrr"429 (बहुत अधिक अनुरोध)"options" => [
"keyName" => "myApp", //वह कुंजी नाम जिसके अंतर्गत सभी अनुरोध संग्रहीत किए जाएँगे। आवश्यक: false. डिफ़ॉल्ट "limitrr"
"errorStatusCode" => 429 //क्या महत्वपूर्ण त्रुटियाँ जैसे Redis कीस्टोर से कनेक्ट करने में विफलता को पकड़ा और प्रदर्शित किया जाना चाहिए?
]
आवश्यक: false
प्रकार: Array
विवरण: रूट प्रतिबंध परिभाषित करें।
routes ऑब्जेक्ट के अंदर, आप कई अलग-अलग रूट परिभाषित कर सकते हैं और उनके अंदर कस्टम नियम निर्धारित कर सकते हैं। आप जो कस्टम नियम निर्धारित कर सकते हैं वे हैं:
100requestsPerExpiry के मान पर या यदि सेट नहीं है तो 5 पर होता है।900 (15 मिनट)expiry के मान पर या यदि सेट नहीं है तो 900 (15 मिनट) पर होता है।requests में कोई स्ट्रिंग सेट नहीं की गई थी, तो यह डिफ़ॉल्ट रूप से होगा। इसके अलावा, यदि में कोई मान सेट नहीं किया गया है, तो यह में पाए गए स्ट्रिंग पर हल होगा। या, यदि वह भी सेट नहीं किया गया था, तो इसका मान होगा।"routes" => [
//डिफ़ॉल्ट रूट नियमों को ओवरराइट करें - सभी कुंजियाँ सेट होनी आवश्यक नहीं हैं,
//केवल वे जिन्हें आप ओवरराइट करना चाहते हैं
"default" => [
"expiry": 1000
],
"exampleRoute" => [
"requestsPerExpiry" => 100,
"completedActionsPerExpiry" => 5,
"expiry" => 900,
"completedExpiry" => 900,
"errorMsgs" => [
"requests" => "As you have made too many requests, you are being rate limited.",
"completed" => "As you performed too many successful actions, you have been rate limited."
]
],
//यदि सभी कुंजियाँ सेट नहीं हैं, तो वे
//डिफ़ॉल्ट मानों पर वापस आ जाएँगे
"exampleRoute2" => [
"requestsPerExpiry" => 500
]
]
"As you have made too many requests, you are being rate limited."completedrequests"As you performed too many successful actions, you have been rate limited."