Class: Idempotency

Inherits:
Object
  • Object
show all
Extended by:
Dry::Configurable
Defined in:
lib/idempotency.rb,
lib/idempotency/cache.rb,
lib/idempotency/rails.rb,
lib/idempotency/hanami.rb,
lib/idempotency/version.rb,
lib/idempotency/constants.rb,
lib/idempotency/testing/helpers.rb,
lib/idempotency/instrumentation/statsd_listener.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: Events, Hanami, Instrumentation, Rails, Testing Classes: Cache, Constants

Constant Summary collapse

VERSION =
'0.3.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Idempotency.config, cache: Cache.new(config:)) ⇒ Idempotency

Returns a new instance of Idempotency.



58
59
60
61
# File 'lib/idempotency.rb', line 58

def initialize(config: Idempotency.config, cache: Cache.new(config:))
  @config = config
  @cache = cache
end

Class Method Details

.configureObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/idempotency.rb', line 45

def self.configure
  super

  if config.metrics.statsd_client
    config.instrumentation_listeners << Idempotency::Instrumentation::StatsdListener.new(
      config.metrics.statsd_client,
      config.metrics.namespace
    )
  end

  config.instrumentation_listeners.each(&:setup_subscriptions)
end

.notifierObject



15
16
17
18
19
20
21
# File 'lib/idempotency.rb', line 15

def self.notifier
  @monitor.synchronize do
    @notifier ||= Dry::Monitor::Notifications.new(:idempotency_gem).tap do |n|
      Events::ALL_EVENTS.each { |event| n.register_event(event) }
    end
  end
end

.use_cache(request, request_identifiers, lock_duration: nil, action: nil, &blk) ⇒ Object



63
64
65
# File 'lib/idempotency.rb', line 63

def self.use_cache(request, request_identifiers, lock_duration: nil, action: nil, &blk)
  new.use_cache(request, request_identifiers, lock_duration:, action:, &blk)
end

Instance Method Details

#use_cache(request, request_identifiers, lock_duration: nil, action: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/idempotency.rb', line 68

def use_cache(request, request_identifiers, lock_duration: nil, action: nil)
  duration_start = Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  action_name = action || "#{request.request_method}:#{request.path}"

  with_apm_instrumentation('idempotency.use_cache', action_name) do
    return yield unless cache_request?(request)

    request_headers = request.env
    idempotency_key = unquote(request_headers[Constants::RACK_HEADER_KEY] || SecureRandom.hex)

    fingerprint = calculate_fingerprint(request, idempotency_key, request_identifiers)

    cached_response = cache.get(fingerprint)

    if (cached_status, cached_headers, cached_body = cached_response)
      cached_headers.merge!(Constants::HEADER_KEY => idempotency_key)
      instrument(Events::CACHE_HIT, request:, action:, duration: calculate_duration(duration_start))

      return [cached_status, cached_headers, cached_body]
    end

    lock_duration ||= config.default_lock_expiry
    response_status, response_headers, response_body = cache.with_lock(fingerprint, lock_duration) do
      yield
    end

    if cache_response?(response_status)
      cache.set(fingerprint, response_status, response_headers, response_body)
      response_headers.merge!({ Constants::HEADER_KEY => idempotency_key })
    end

    instrument(Events::CACHE_MISS, request:, action:, duration: calculate_duration(duration_start))
    [response_status, response_headers, response_body]
  end
rescue Idempotency::Cache::LockConflict
  instrument(Events::LOCK_CONFLICT, request:, action:, duration: calculate_duration(duration_start))
  [409, {}, config.response_body.concurrent_error]
end