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

Fix memory leak caused by caching all unique paths seen #2084

Merged
merged 1 commit into from
Jul 10, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [#2060](https://github.com/ruby-grape/grape/pull/2060): Drop support for Ruby 2.4 - [@dblock](https://github.com/dblock).
* [#2060](https://github.com/ruby-grape/grape/pull/2060): Upgraded Rubocop to 0.84.0 - [@dblock](https://github.com/dblock).
* [#2077](https://github.com/ruby-grape/grape/pull/2077): Simplify logic for defining declared params - [@dnesteryuk](https://github.com/dnesteryuk).
* [#2084](https://github.com/ruby-grape/grape/pull/2084): Fix memory leak in path normalization - [@fcheung](https://github.com/fcheung).
* Your contribution here.

#### Fixes
Expand Down
18 changes: 5 additions & 13 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ module Grape
class Router
attr_reader :map, :compiled

class NormalizePathCache < Grape::Util::Cache
def initialize
@cache = Hash.new do |h, path|
normalized_path = +"/#{path}"
normalized_path.squeeze!('/')
normalized_path.sub!(%r{/+\Z}, '')
normalized_path = '/' if normalized_path.empty?
h[path] = -normalized_path
end
end
end

def self.normalize_path(path)
NormalizePathCache[path]
path = +"/#{path}"
path.squeeze!('/')
path.sub!(%r{/+\Z}, '')
path = '/' if path == ''
path
end

def self.supported_methods
Expand Down