<commit_before>require 'rate_limiter_pa/version'
require 'rate_limiter_pa/default_store'

module Rack
  class RateLimiterPa
    DEFAULT_BLOCK = Proc.new { |env| env['REMOTE_ADDR'] }

    def initialize(app, options = {}, &block)
      options = { limit: 20, reset_in: 3600, store: DefaultStore.new }.merge(options)

      @app = app
      @limit_total = options[:limit].to_i
      @reset_in = options[:reset_in].to_i
      @limit_reset = Time.now + @reset_in
      @block = block || DEFAULT_BLOCK
      @store = options[:store]
    end

    def call(env)
      set_id(env)
      if @id
        set_limits(@id)
        reset_limits if reset_time_reached?
        adjust_limit_remaining
        store_client(@id, @limit_remaining)
        return [429, {}, ['Too many requests']] if limit_reached?
      end

      @app.call(env).tap { |env| add_headers(env[1]) unless unlimited_calls? }
    end

    def set_id(env)
      @id = @block.call(env)
    end

    def unlimited_calls?
      true unless @id
    end

    def get_current_client(id)
      unless @store.get(id)
        store_client(id, @limit_total)
      end

      @store.get(id)
    end

    def store_client(id, limit_remaining)
      client = { limit_remaining: limit_remaining, limit_reset: @limit_reset }
      @store.set(id, client)
    end

    def add_headers(headers)
      add_limit_total_header(headers)
      add_limit_remaining_header(headers)
      add_limit_reset_header(headers)
    end

    def add_limit_total_header(headers)
      headers.merge! 'X-RateLimit-Limit' => @limit_total.to_s
    end

    def add_limit_remaining_header(headers)
      headers.merge! 'X-RateLimit-Remaining' => @limit_remaining.to_s
    end

    def add_limit_reset_header(headers)
      headers.merge! 'X-RateLimit-Reset' => @limit_reset.to_i.to_s
    end

    def set_limits(id)
      @limit_remaining = get_current_client(id)[:limit_remaining]
      @limit_reset = get_current_client(id)[:limit_reset]
    end

    def reset_limits
      @limit_reset = Time.now + @reset_in
      @limit_remaining = @limit_total
    end

    def adjust_limit_remaining
      @limit_remaining -= 1
    end

    def limit_reset_left
      @limit_reset - Time.now
    end

    def limit_reached?
      @limit_remaining < 0
    end

    def reset_time_reached?
      limit_reset_left <= 0
    end
  end
end<commit_msg>[#6] changed @limit_reset to @limit_reset_at<commit_after>require 'rate_limiter_pa/version'
require 'rate_limiter_pa/default_store'

module Rack
  class RateLimiterPa
    DEFAULT_BLOCK = Proc.new { |env| env['REMOTE_ADDR'] }

    def initialize(app, options = {}, &block)
      options = { limit: 20, reset_in: 3600, store: DefaultStore.new }.merge(options)

      @app = app
      @limit_total = options[:limit].to_i
      @reset_in = options[:reset_in].to_i
      @limit_reset_at = Time.now + @reset_in
      @block = block || DEFAULT_BLOCK
      @store = options[:store]
    end

    def call(env)
      set_id(env)
      if @id
        set_limits(@id)
        reset_limits if reset_time_reached?
        adjust_limit_remaining
        store_client(@id, @limit_remaining)
        return [429, {}, ['Too many requests']] if limit_reached?
      end

      @app.call(env).tap { |env| add_headers(env[1]) unless unlimited_calls? }
    end

    def set_id(env)
      @id = @block.call(env)
    end

    def unlimited_calls?
      true unless @id
    end

    def get_current_client(id)
      unless @store.get(id)
        store_client(id, @limit_total)
      end

      @store.get(id)
    end

    def store_client(id, limit_remaining)
      client = { limit_remaining: limit_remaining, limit_reset: @limit_reset_at }
      @store.set(id, client)
    end

    def add_headers(headers)
      add_limit_total_header(headers)
      add_limit_remaining_header(headers)
      add_limit_reset_header(headers)
    end

    def add_limit_total_header(headers)
      headers.merge! 'X-RateLimit-Limit' => @limit_total.to_s
    end

    def add_limit_remaining_header(headers)
      headers.merge! 'X-RateLimit-Remaining' => @limit_remaining.to_s
    end

    def add_limit_reset_header(headers)
      headers.merge! 'X-RateLimit-Reset' => @limit_reset_at.to_i.to_s
    end

    def set_limits(id)
      @limit_remaining = get_current_client(id)[:limit_remaining]
      @limit_reset_at = get_current_client(id)[:limit_reset]
    end

    def reset_limits
      @limit_reset_at = Time.now + @reset_in
      @limit_remaining = @limit_total
    end

    def adjust_limit_remaining
      @limit_remaining -= 1
    end

    def limit_reset_left
      @limit_reset_at - Time.now
    end

    def limit_reached?
      @limit_remaining < 0
    end

    def reset_time_reached?
      limit_reset_left <= 0
    end
  end
end
{'fraction_non_alphanumeric': 0.09606895101955014, 'fraction_numerical': 0.005675846121505151, 'mean_word_length': 2.8401937046004844, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1167029', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1608, 'n_words': 317}