Skip to content

Commit cd7d92e

Browse files
committed
Refactor: review and move timeout pieces back
Fixes #153
1 parent d118d93 commit cd7d92e

File tree

2 files changed

+49
-72
lines changed

2 files changed

+49
-72
lines changed

lib/logstash/filters/grok.rb

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@
144144
# filter. This newly defined patterns in `pattern_definitions` will not be available outside of that particular `grok` filter.
145145
#
146146
class LogStash::Filters::Grok < LogStash::Filters::Base
147-
require 'logstash/filters/grok/timeout_support'
148-
149-
include TimeoutSupport
150-
151147
config_name "grok"
152148

153149
# A hash of matches of field => value
@@ -289,7 +285,7 @@ def register
289285
@match_counter = metric.counter(:matches)
290286
@failure_counter = metric.counter(:failures)
291287

292-
@timeout = @timeout_millis > 0.0 ? RubyTimeout.new(@timeout_millis) : NoopTimeout.new
288+
@timeout = @timeout_millis > 0.0 ? RubyTimeout.new(@timeout_millis) : NoopTimeout::INSTANCE
293289
@matcher = ( @timeout_scope.eql?('event') ? EventTimeoutMatcher : PatternTimeoutMatcher ).new(self)
294290
end # def register
295291

@@ -479,4 +475,52 @@ def trunc_value
479475
end
480476
end
481477
end
478+
479+
def with_timeout(context, &block)
480+
@timeout.exec(&block)
481+
rescue TimeoutError => error
482+
handle_timeout(context, error)
483+
end
484+
public :with_timeout
485+
486+
def handle_timeout(context, error)
487+
raise GrokTimeoutException.new(context.grok, context.field, context.input)
488+
end
489+
490+
# @private
491+
class GrokContext
492+
attr_reader :grok, :field, :input
493+
494+
def initialize(field, input)
495+
@field = field
496+
@input = input
497+
end
498+
499+
def set_grok(grok)
500+
@grok = grok
501+
end
502+
end
503+
504+
# @private
505+
class NoopTimeout
506+
INSTANCE = new
507+
508+
def exec
509+
yield
510+
end
511+
end
512+
513+
# @private
514+
class RubyTimeout
515+
def initialize(timeout_millis)
516+
# divide by float to allow fractional seconds, the Timeout class timeout value is in seconds but the underlying
517+
# executor resolution is in microseconds so fractional second parameter down to microseconds is possible.
518+
# see https://github.com/jruby/jruby/blob/9.2.7.0/core/src/main/java/org/jruby/ext/timeout/Timeout.java#L125
519+
@timeout_seconds = timeout_millis / 1000.0
520+
end
521+
522+
def exec(&block)
523+
Timeout.timeout(@timeout_seconds, TimeoutError, &block)
524+
end
525+
end
482526
end # class LogStash::Filters::Grok

lib/logstash/filters/grok/timeout_support.rb

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)