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

Improve thread scheduling #487

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 1 addition & 2 deletions lib/ferrum/browser/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Ferrum
class Browser
class Process
KILL_TIMEOUT = 2
WAIT_KILLED = 0.05

attr_reader :host, :port, :ws_url, :pid, :command,
:default_user_agent, :browser_version, :protocol_version,
Expand All @@ -38,7 +37,7 @@ def self.process_killer(pid)
::Process.kill("USR1", pid)
start = Utils::ElapsedTime.monotonic_time
while ::Process.wait(pid, ::Process::WNOHANG).nil?
sleep(WAIT_KILLED)
::Thread.pass
next unless Utils::ElapsedTime.timeout?(start, KILL_TIMEOUT)

::Process.kill("KILL", pid)
Expand Down
7 changes: 2 additions & 5 deletions lib/ferrum/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ def initialize(page)
# @param [Integer] connections
# how many connections are allowed for network to be idling,
#
# @param [Float] duration
# Sleep for given amount of time and check again.
#
# @param [Float] timeout
# During what time we try to check idle.
#
Expand All @@ -59,13 +56,13 @@ def initialize(page)
# browser.at_xpath("//a[text() = 'No UI changes button']").click
# browser.network.wait_for_idle # => false
#
def wait_for_idle(connections: 0, duration: 0.05, timeout: @page.timeout)
def wait_for_idle(connections: 0, timeout: @page.timeout)
start = Utils::ElapsedTime.monotonic_time

until idle?(connections)
return false if Utils::ElapsedTime.timeout?(start, timeout)

sleep(duration)
::Thread.pass
end

true
Expand Down
2 changes: 2 additions & 0 deletions lib/ferrum/utils/attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def with_retry(errors:, max:, wait:)

attempts += 1
sleep(wait)

::Thread.pass
retry
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/ferrum/utils/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ def reset
@iteration
end
end

def wait(timeout)
synchronize do
unless @set
iteration = @iteration
ns_wait_until(timeout) do
iteration < @iteration || @set
::Thread.pass
end
else
true
end
end
end
end
end
end
Loading