diff options
Diffstat (limited to 'lib/gitlab/etag_caching/router.rb')
-rw-r--r-- | lib/gitlab/etag_caching/router.rb | 105 |
1 files changed, 15 insertions, 90 deletions
diff --git a/lib/gitlab/etag_caching/router.rb b/lib/gitlab/etag_caching/router.rb index 769ac2784d1..742b72ecde9 100644 --- a/lib/gitlab/etag_caching/router.rb +++ b/lib/gitlab/etag_caching/router.rb @@ -2,99 +2,24 @@ module Gitlab module EtagCaching - class Router - Route = Struct.new(:regexp, :name, :feature_category) - # We enable an ETag for every request matching the regex. - # To match a regex the path needs to match the following: - # - Don't contain a reserved word (expect for the words used in the - # regex itself) - # - Ending in `noteable/issue/<id>/notes` for the `issue_notes` route - # - Ending in `issues/id`/realtime_changes` for the `issue_title` route - USED_IN_ROUTES = %w[noteable issue notes issues realtime_changes - commit pipelines merge_requests builds - new environments].freeze - RESERVED_WORDS = Gitlab::PathRegex::ILLEGAL_PROJECT_PATH_WORDS - USED_IN_ROUTES - RESERVED_WORDS_REGEX = Regexp.union(*RESERVED_WORDS.map(&Regexp.method(:escape))) - RESERVED_WORDS_PREFIX = %Q(^(?!.*\/(#{RESERVED_WORDS_REGEX})\/).*) + module Router + Route = Struct.new(:regexp, :name, :feature_category, :router) do + delegate :match, to: :regexp + delegate :cache_key, to: :router + end - ROUTES = [ - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/noteable/issue/\d+/notes\z), - 'issue_notes', - 'issue_tracking' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/noteable/merge_request/\d+/notes\z), - 'merge_request_notes', - 'code_review' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/issues/\d+/realtime_changes\z), - 'issue_title', - 'issue_tracking' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/commit/\S+/pipelines\.json\z), - 'commit_pipelines', - 'continuous_integration' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/merge_requests/new\.json\z), - 'new_merge_request_pipelines', - 'continuous_integration' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/merge_requests/\d+/pipelines\.json\z), - 'merge_request_pipelines', - 'continuous_integration' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/pipelines\.json\z), - 'project_pipelines', - 'continuous_integration' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/pipelines/\d+\.json\z), - 'project_pipeline', - 'continuous_integration' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/builds/\d+\.json\z), - 'project_build', - 'continuous_integration' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/clusters/\d+/environments\z), - 'cluster_environments', - 'continuous_delivery' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/environments\.json\z), - 'environments', - 'continuous_delivery' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/import/github/realtime_changes\.json\z), - 'realtime_changes_import_github', - 'importers' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/import/gitea/realtime_changes\.json\z), - 'realtime_changes_import_gitea', - 'importers' - ), - Gitlab::EtagCaching::Router::Route.new( - %r(#{RESERVED_WORDS_PREFIX}/merge_requests/\d+/cached_widget\.json\z), - 'merge_request_widget', - 'code_review' - ) - ].freeze + module Helpers + def build_route(attrs) + EtagCaching::Router::Route.new(*attrs, self) + end + end - def self.match(path) - ROUTES.find { |route| route.regexp.match(path) } + # Performing RESTful routing match before GraphQL would be more expensive + # for the GraphQL requests because we need to traverse all of the RESTful + # route definitions before falling back to GraphQL. + def self.match(request) + Router::Graphql.match(request) || Router::Restful.match(request) end end end end - -Gitlab::EtagCaching::Router.prepend_if_ee('EE::Gitlab::EtagCaching::Router') |