summaryrefslogtreecommitdiff
path: root/lib/gitlab/application_rate_limiter/increment_per_actioned_resource.rb
blob: 7a68dd104a8c923748166f84cb2d174f7c7dd3fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module Gitlab
  module ApplicationRateLimiter
    class IncrementPerActionedResource < BaseStrategy
      def initialize(resource_key)
        @resource_key = resource_key
      end

      def increment(cache_key, expiry)
        with_redis do |redis|
          redis.pipelined do |pipeline|
            pipeline.sadd(cache_key, resource_key)
            pipeline.expire(cache_key, expiry)
            pipeline.scard(cache_key)
          end.last
        end
      end

      def read(cache_key)
        with_redis do |redis|
          redis.scard(cache_key)
        end
      end

      private

      attr_accessor :resource_key
    end
  end
end