Class: CommandTower::Services::RateLimits::Check

Inherits:
ApplicationService show all
Defined in:
app/services/command_tower/services/rate_limits/check.rb

Constant Summary collapse

INCREMENT_SCRIPT =
<<~LUA
  local count = redis.call('INCR', KEYS[1])
  if count == 1 then
    redis.call('EXPIRE', KEYS[1], tonumber(ARGV[1]))
  end
  return { count, redis.call('TTL', KEYS[1]) }
LUA

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from ApplicationService

call, inherited

Methods included from Transactional

#fail_transaction!, #transaction

Methods inherited from CommandTower::ServiceBase

#command_tower_lifecycle, inherited, #internal_validate, #service_lifecycle_error_codes, #service_lifecycle_log_level, #validate!

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from CommandTower::ServiceLogging

included

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
# File 'app/services/command_tower/services/rate_limits/check.rb', line 18

def call
  count, ttl = CommandTower::RedisConnection.with do |redis|
    redis.eval(INCREMENT_SCRIPT, keys: [key], argv: [ttl_seconds])
  end

  context.count = count.to_i
  context.ttl = ttl.to_i
rescue Redis::BaseError => e
  context.fail!(application_error: CommandTower::Errors::InternalError.new(cause: e))
end