Sha256: f6d4567298b9c35cf73f7c0dada50b1d8e23231671b4af504de9d8e3809327c6

Contents?: true

Size: 950 Bytes

Versions: 7

Compression:

Stored size: 950 Bytes

Contents

module RProxy
  class CheckSnapshotService

    def initialize(redis, config)
      @redis = redis
      @snapshot_expire_in = 15 * 60
      @usage_threshold = config.usage_threshold
      @cb_url = config.callback_url
      @logger = config.logger
    end

    def call(user, pass, result)

      s_key = "proxy:#{user}-#{pass}:snapshot"
      snapshot_value = @redis.get(s_key)

      if snapshot_value.nil? || snapshot_value.empty?
        @redis.setex(s_key, @snapshot_expire_in, result)
      else
        tmp = snapshot_value.to_i - result.to_i

        if tmp >= @usage_threshold
          begin
            connection = RProxy::CallbackService.call(@cb_url, user, pass, tmp)
            connection.assign_logger(@logger)
            @redis.setex(s_key, @snapshot_expire_in, result)
          rescue => e
            @logger.error("callback service: @id:#{s_key}, #{e.message}") if @logger
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
r_proxy-0.2.6 lib/r_proxy/check_snapshot_service.rb
r_proxy-0.2.5 lib/r_proxy/check_snapshot_service.rb
r_proxy-0.2.4 lib/r_proxy/check_snapshot_service.rb
r_proxy-0.2.3 lib/r_proxy/check_snapshot_service.rb
r_proxy-0.2.2 lib/r_proxy/check_snapshot_service.rb
r_proxy-0.2.1 lib/r_proxy/check_snapshot_service.rb
r_proxy-0.2.0 lib/r_proxy/check_snapshot_service.rb