-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ActiveSupport::CurrentAttributes to store input payload (#5)
- Loading branch information
Showing
6 changed files
with
14 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ | |
end | ||
|
||
before do | ||
aggregator.reset! | ||
aggregator.start! | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters