Example #1
0
 /**
  * Execute the rate limiting for the given request.
  *
  * @param \Dingo\Api\Http\Request $request
  * @param int                     $limit
  * @param int                     $expires
  *
  * @return void
  */
 public function rateLimitRequest(Request $request, $limit = 0, $expires = 0)
 {
     $this->request = $request;
     // If the throttle instance is already set then we'll just carry on as
     // per usual.
     if ($this->throttle instanceof Throttle) {
         //
         // If the developer specified a certain amount of requests or expiration
         // time on a specific route then we'll always use the route specific
         // throttle with the given values.
     } elseif ($limit > 0 || $expires > 0) {
         $this->throttle = new Route(['limit' => $limit, 'expires' => $expires]);
         $this->keyPrefix = md5($request->path());
         // Otherwise we'll use the throttle that gives the consumer the largest
         // amount of requests. If no matching throttle is found then rate
         // limiting will not be imposed for the request.
     } else {
         $this->throttle = $this->getMatchingThrottles()->sort(function ($a, $b) {
             return $a->getLimit() < $b->getLimit();
         })->first();
     }
     if (is_null($this->throttle)) {
         return;
     }
     $this->prepareCacheStore();
     $this->cache('requests', 0, $this->throttle->getExpires());
     $this->cache('expires', $this->throttle->getExpires(), $this->throttle->getExpires());
     $this->cache('reset', time() + $this->throttle->getExpires() * 60, $this->throttle->getExpires());
     $this->increment('requests');
 }