Skip to content

Commit

Permalink
Fix memory leak caused by caching all unique paths seen
Browse files Browse the repository at this point in the history
This optimisation was introduced in #2002. If an api's paths include
an id in the path then this cache can grow in an unbounded manner.
  • Loading branch information
fcheung committed Jul 10, 2020
1 parent 197d5b4 commit 6373142
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
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

0 comments on commit 6373142

Please sign in to comment.