Skip to content

Commit

Permalink
Use ActiveSupport::CurrentAttributes to store input payload (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxshend authored Apr 4, 2022
1 parent bd3b223 commit bece51f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 30 deletions.
1 change: 0 additions & 1 deletion io_to_response_payload_ratio.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.6.0"

spec.add_dependency "rails", ">= 6.0"
spec.add_dependency "concurrent-ruby", "~> 1.0"
end
26 changes: 11 additions & 15 deletions lib/io_to_response_payload_ratio/aggregator.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
# frozen_string_literal: true

require "concurrent"

module IoToResponsePayloadRatio
# Thread-safe payload size aggregator.
class Aggregator
def initialize(sources)
@sources = sources
@active = Concurrent::ThreadLocalVar.new(false)
@state = Concurrent::ThreadLocalVar.new(empty_state)
end

attr_reader :sources

def active?
active.value
InputPayload.active.present?
end

def start!
active.value = true
InputPayload.active = true
end

def stop!
active.value = false
end

def reset!
state.value = empty_state
InputPayload.active = false
end

def increment(source, val)
return unless active?

state.value[source.to_sym] += val
InputPayload.state ||= empty_state
InputPayload.state[source.to_sym] += val
end

def get(source)
state.value[source.to_sym]
InputPayload.state ||= empty_state
InputPayload.state[source.to_sym]
end

private

attr_reader :active, :state

def empty_state
sources.map { |kind| [kind, 0] }.to_h
end

class InputPayload < ActiveSupport::CurrentAttributes
attribute :state, :active
end
end
end
1 change: 0 additions & 1 deletion lib/io_to_response_payload_ratio/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Controller
extend ActiveSupport::Concern

def process_action(*)
IoToResponsePayloadRatio.aggregator.reset!
IoToResponsePayloadRatio.aggregator.start!

super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
end

before do
aggregator.reset!
aggregator.start!
end

Expand Down
12 changes: 0 additions & 12 deletions spec/io_to_response_payload_ratio/aggregator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@
end
end

describe ".reset!" do
it "nullifies the state" do
aggregator.start!
aggregator.increment(sources.first, 42)
aggregator.stop!
aggregator.reset!

expect(aggregator.get(sources.first)).to eq(0)
end
end

describe ".increment" do
it "increments the specified source value" do
aggregator.start!
Expand Down Expand Up @@ -66,7 +55,6 @@

Thread.new do
aggregator.start!
aggregator.reset!

expect(aggregator.get(sources.first)).to eq(0)
expect(aggregator.active?).to be true
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
require "io_to_response_payload_ratio"

RSpec.configure do |config|
# For proper work of ActiveSupport::CurrentAttributes reset
config.include ActiveSupport::CurrentAttributes::TestHelper

config.example_status_persistence_file_path = ".rspec_status"
config.infer_base_class_for_anonymous_controllers = true

Expand Down

0 comments on commit bece51f

Please sign in to comment.