Skip to content

Commit

Permalink
memoize redis instance
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenhinkle committed Feb 22, 2024
1 parent 45e82d5 commit 904bce8
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions modules/veteran/app/sidekiq/representatives/update.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'redis'
require 'sidekiq'
require 'sentry_logging'
require 'va_profile/models/validation_address'
Expand All @@ -12,22 +13,55 @@ class Update
include Sidekiq::Job
include SentryLogging

# Performs the job of parsing JSON data, validating the address, and updating the record.
# @param json_data [String] JSON string containing address data.
REDIS_OPTIONS = REDIS_CONFIG[:redis].to_h
REDIS_RATE_LIMIT_KEY = 'rep_update_rate_limit'
RATE_LIMIT_PERIOD = 60 # seconds
RATE_LIMIT_COUNT = 30

Check failure on line 20 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/TrailingWhitespace: Trailing whitespace detected.
def perform(json_data)
data = JSON.parse(json_data)
validation_address = build_validation_address(data['request_address'])
response = validate_address(validation_address)

return unless address_valid?(response)
if rate_limited?
Representatives::Update.perform_async(json_data)
else
begin
data = JSON.parse(json_data)
validation_address = build_validation_address(data['request_address'])
response = validate_address(validation_address)

Check failure on line 29 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/TrailingWhitespace: Trailing whitespace detected.
return unless address_valid?(response)

Check failure on line 31 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/TrailingWhitespace: Trailing whitespace detected.
update_address_record(data, response)
increment_rate_limit
rescue JSON::ParserError => e
log_error(e)
end
end
end

Check failure on line 39 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/TrailingWhitespace: Trailing whitespace detected.
private

Check failure on line 41 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/TrailingWhitespace: Trailing whitespace detected.
def rate_limited?
redis = redis_instance
count = redis.get(REDIS_RATE_LIMIT_KEY).to_i
count >= RATE_LIMIT_COUNT
end

Check failure on line 47 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/TrailingWhitespace: Trailing whitespace detected.
def increment_rate_limit
redis = redis_instance

update_address_record(data, response)
rescue JSON::ParserError => e
log_error(e)
if redis.exists(REDIS_RATE_LIMIT_KEY)
redis.incr(REDIS_RATE_LIMIT_KEY)
else
# Expire the key after the rate limit period
redis.set(REDIS_RATE_LIMIT_KEY, 1, ex: RATE_LIMIT_PERIOD)
end
end

private

Check failure on line 59 in modules/veteran/app/sidekiq/representatives/update.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAccessModifier: Useless `private` access modifier.

def redis_instance
@redis ||= Redis.new(REDIS_OPTIONS)
end

# Builds a validation address object from the provided address data.
# @param request_address [Hash] A hash containing address fields.
# @return [VAProfile::Models::ValidationAddress] A validation address object.
Expand Down

0 comments on commit 904bce8

Please sign in to comment.