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

Replaced atomic and thread_safe for concurrent_ruby. #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions lib/logstash/filters/throttle.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "logstash/filters/base"
require "logstash/namespace"
require "thread_safe"
require "atomic"
require "concurrent/atomics"
require "concurrent"

# The throttle filter is for throttling the number of events. The filter is
# configured with a lower bound, the "before_count", and upper bound, the "after_count",
Expand Down Expand Up @@ -143,12 +144,12 @@
# Mike Pilone (@mikepilone)
#

class ThreadSafe::TimeslotCache < ThreadSafe::Cache
class Concurrent::TimeslotCache < Concurrent::Map
attr_reader :created

def initialize(epoch, options = nil, &block)
@created = epoch
@latest = Atomic.new(epoch)
@latest = Concurrent::AtomicFixnum.new(epoch)

super(options, &block)
end
Expand Down Expand Up @@ -214,7 +215,7 @@ class LogStash::Filters::Throttle < LogStash::Filters::Base
# performs initialization of the filter
public
def register
@key_cache = ThreadSafe::Cache.new
@key_cache = Concurrent::Map.new
@max_age_orig = @max_age
end # def register

Expand All @@ -228,7 +229,7 @@ def filter(event)

while true
# initialise timeslot cache (if required)
@key_cache.compute_if_absent(key) { ThreadSafe::TimeslotCache.new(epoch) }
@key_cache.compute_if_absent(key) { Concurrent::TimeslotCache.new(epoch) }
timeslot_cache = @key_cache[key] # try to get timeslot cache
break unless timeslot_cache.nil? # retry until succesful

Expand All @@ -243,14 +244,14 @@ def filter(event)

while true
# initialise timeslot and counter (if required)
timeslot_cache.compute_if_absent(timeslot_key) { Atomic.new(0) }
timeslot_cache.compute_if_absent(timeslot_key) { Concurrent::AtomicFixnum.new(0) }
timeslot = timeslot_cache[timeslot_key] # try to get timeslot
break unless timeslot.nil? # retry until succesful

@logger.warn? and @logger.warn(
"filters/#{self.class.name}: timeslot disappeared, increase max_age to prevent this.")
end

timeslot.update { |v| v + 1 } # increment counter
count = timeslot.value # get latest counter value

Expand Down
5 changes: 2 additions & 3 deletions logstash-filter-throttle.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ Gem::Specification.new do |s|
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
s.add_runtime_dependency "thread_safe"
s.add_runtime_dependency "atomic"
s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
s.add_runtime_dependency "concurrent-ruby", "~> 1.0"

s.add_development_dependency 'logstash-devutils'
end