def call(env)
      # Accept an optional start-of-request timestamp from the Rack env for
      # upstream timing and for testing.
      now = env.fetch('ratelimit.timestamp', Time.now).to_f

      if apply_rate_limit?(env) && classification = classify(env)
        # Increment the request counter.
        epoch = ratelimit_epoch(now)
        count = @counter.increment(classification, epoch)
        remaining = @max - count

        # If exceeded, return a 429 Rate Limit Exceeded response.
        if remaining < 0
          # Only log the first hit that exceeds the limit.
          if @logger && remaining == -1
            @logger.info '%s: %s exceeded %d request limit for %s' % [@name, classification, @max, format_epoch(epoch)]
          end

          retry_after = seconds_until_epoch(epoch)

          [ @status,
            { 'X-Ratelimit' => ratelimit_json(remaining, epoch),
              'Retry-After' => retry_after.to_s },
            [ @error_message % retry_after ] ]

        # Otherwise, pass through then add some informational headers.
        else
          @app.call(env).tap do |status, headers, body|
            amend_headers headers, 'X-Ratelimit', ratelimit_json(remaining, epoch)
          end
        end
      else
        @app.call(env)
      end
    end
def call(env)
      # Accept an optional start-of-request timestamp from the Rack env for
      # upstream timing and for testing.
      now = env.fetch('ratelimit.timestamp', Time.now).to_f

      if apply_rate_limit?(env) && classification = classify(env)
        # Increment the request counter.
        epoch = ratelimit_epoch(now)
        count = @counter.increment(classification, epoch)
        remaining = @max - count

        # If exceeded, return a 429 Rate Limit Exceeded response.
        if remaining < 0
          # Only log the first hit that exceeds the limit.
          if @logger && remaining == -1
            @logger.info '%s: %s exceeded %d request limit for %s' % [@name, classification, @max, format_epoch(epoch)]
          end

          retry_after = seconds_until_epoch(epoch)

          [ @status,
            { 'X-Ratelimit' => ratelimit_json(remaining, epoch),
              'Retry-After' => retry_after.to_s },
            [ @error_message % retry_after ] ]

        # Otherwise, pass through then add some informational headers.
        else
          @app.call(env).tap do |status, headers, body|
            amend_headers headers, 'X-Ratelimit', ratelimit_json(remaining, epoch)
          end
        end
      else
        @app.call(env)
      end
    end
Handle a Rack request:
   * Check whether the rate limit applies to the request.
   * Classify the request by IP, API token, etc.
   * Calculate the end of the current time window.
   * Increment the counter for this classification and time window.
   * If count exceeds limit, return a 429 response.
   * If it's the first request that exceeds the limit, log it.
   * If the count doesn't exceed the limit, pass through the request.