Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timeout_html_pipeline_filter.rb « filter « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9b71163ab1b60ea4b16e881a48b5953d05946ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

module Banzai
  module Filter
    # HTML Filter that wraps a filter in a Gitlab::RenderTimeout.
    # This way partial results can be returned, and the entire pipeline
    # is not killed.
    #
    # This should not be used for any filter that must be allowed to complete,
    # like a `ReferenceRedactorFilter`
    #
    class TimeoutHtmlPipelineFilter < HTML::Pipeline::Filter
      RENDER_TIMEOUT = 10.seconds

      def call
        Gitlab::RenderTimeout.timeout(foreground: RENDER_TIMEOUT) { call_with_timeout }
      rescue Timeout::Error => e
        class_name = self.class.name.demodulize
        timeout_counter.increment(source: class_name)
        Gitlab::ErrorTracking.track_exception(e, project_id: context[:project]&.id, class_name: class_name)

        # we've timed out, but some work may have already been completed,
        # so go ahead and return the document
        doc
      end

      def call_with_timeout
        raise NotImplementedError
      end

      private

      def timeout_counter
        Gitlab::Metrics.counter(:banzai_filter_timeouts_total, 'Count of the Banzai filters that time out')
      end
    end
  end
end