Skip to content

Commit

Permalink
Make Sprockets::CachedEnvironment thread-safe by using Concurrent::Map
Browse files Browse the repository at this point in the history
* Unfortunately this on its own does not work yet due to Sprockets
  recursively calling Sprockets::CachedEnvironment#load.
* Reproduce the recursion issue with:
  bundle exec bin/sprockets -I test/fixtures/asset $PWD/test/fixtures/asset/application.js
  • Loading branch information
eregon committed Dec 19, 2022
1 parent 1276b43 commit 2d454bc
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/sprockets/cached_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def initialize(environment)
initialize_configuration(environment)

@cache = environment.cache
@stats = {}
@entries = {}
@uris = {}
@processor_cache_keys = {}
@resolved_dependencies = {}
@stats = Concurrent::Map.new
@entries = Concurrent::Map.new
@uris = Concurrent::Map.new
@processor_cache_keys = Concurrent::Map.new
@resolved_dependencies = Concurrent::Map.new
end

# No-op return self as cached environment.
Expand All @@ -31,27 +31,27 @@ def cached

# Internal: Cache Environment#entries
def entries(path)
@entries.fetch(path){ @entries[path] = super(path) }
@entries.compute_if_absent(path) { super(path) }
end

# Internal: Cache Environment#stat
def stat(path)
@stats.fetch(path){ @stats[path] = super(path) }
@stats.compute_if_absent(path) { super(path) }
end

# Internal: Cache Environment#load
def load(uri)
@uris.fetch(uri){ @uris[uri] = super(uri) }
@uris.compute_if_absent(uri) { super(uri) }
end

# Internal: Cache Environment#processor_cache_key
def processor_cache_key(str)
@processor_cache_keys.fetch(str){ @processor_cache_keys[str] = super(str) }
@processor_cache_keys.compute_if_absent(str) { super(str) }
end

# Internal: Cache Environment#resolve_dependency
def resolve_dependency(str)
@resolved_dependencies.fetch(str){ @resolved_dependencies[str] = super(str) }
@resolved_dependencies.compute_if_absent(str) { super(str) }
end

private
Expand Down

0 comments on commit 2d454bc

Please sign in to comment.