123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::SocketLock Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: rb/lib/selenium/webdriver/common/socket_lock.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(port, timeout) ⇒ SocketLock

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 27

def initialize(port, timeout)
  @port    = port
  @server  = nil
  @timeout = timeout
end

Instance Attribute Details

#can_lock?Boolean (readonly, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 68

def can_lock?
  @server = TCPServer.new(Platform.localhost, @port)
  @server.close_on_exec = true
  true
rescue SocketError, Errno::EADDRINUSE, Errno::EBADF => e
  WebDriver.logger.debug("#{self}: #{e.message}", id: :driver_service)
  false
end

#did_lock?Boolean (readonly, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 77

def did_lock?
  !@server.nil?
end

Instance Method Details

#current_time (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 60

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

#lock (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 50

def lock
  max_time = current_time + @timeout

  sleep 0.1 until can_lock? || current_time >= max_time

  return if did_lock?

  raise Error::WebDriverError, "unable to bind to locking port #{@port} within #{@timeout} seconds"
end

#locked

Attempt to acquire a lock on the given port. Control is yielded to an execution block if the lock could be successfully obtained.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 38

def locked
  lock

  begin
    yield
  ensure
    release
  end
end

#release (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/socket_lock.rb', line 64

def release
  @server&.close
end