Skip to content

Commit

Permalink
Support idle_timeout in global compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Dec 1, 2023
1 parent 4970961 commit 1fb9375
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/sass/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Sass
# sass.close
class Compiler
def initialize
@dispatcher = ResilientDispatcher.new
@dispatcher = ResilientDispatcher.new(Dispatcher)
end

# Compiles the Sass file at +path+ to CSS.
Expand Down
7 changes: 4 additions & 3 deletions lib/sass/compiler/resilient_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class Compiler
#
# It recovers from failures and continues to function.
class ResilientDispatcher
def initialize
@dispatcher = Dispatcher.new
def initialize(dispatcher_class)
@dispatcher_class = dispatcher_class
@dispatcher = @dispatcher_class.new
@mutex = Mutex.new
end

Expand All @@ -29,7 +30,7 @@ def connect(...)
@mutex.synchronize do
@dispatcher.connect(...)
rescue Errno::EBUSY
@dispatcher = Dispatcher.new
@dispatcher = @dispatcher_class.new
@dispatcher.connect(...)
end
end
Expand Down
40 changes: 39 additions & 1 deletion lib/sass/embedded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,48 @@ def instance
@mutex.synchronize do
return @instance if @instance

@instance = Compiler.new
@instance = Class.new(Compiler) do
def initialize
@dispatcher = self.class.const_get(:ResilientDispatcher).new(Class.new(self.class.const_get(:Dispatcher)) do
def initialize
super

idle_timeout = 10
@last_accessed_time = current_time

Thread.new do
duration = idle_timeout
loop do
sleep(duration.negative? ? idle_timeout : duration)
evicted = @mutex.synchronize do
duration = @last_accessed_time + idle_timeout - current_time
@id = 0xffffffff if @observers.empty? && duration.negative?
end
break if evicted
end
close
end
end

def unsubscribe(id)
@last_accessed_time = current_time

super
end

private

def current_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end)
end
end.new

at_exit do
@instance.close
end

@instance
end
end
Expand Down

0 comments on commit 1fb9375

Please sign in to comment.