Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ActiveSupport::CurrentAttributes to store input payload #5

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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