You are here

public function SearchApiSolrConnection::ping in Search API Solr 7

Calls the /admin/ping servlet, to test the connection to the server.

Parameters

int|false $timeout: Maximum time to wait for ping in seconds, -1 for unlimited (default 2).

Return value

float|false Seconds taken to ping the server, FALSE if timeout occured.

Overrides SearchApiSolrConnectionInterface::ping

File

includes/solr_connection.inc, line 229

Class

SearchApiSolrConnection
Represents a Solr server resource.

Code

public function ping($timeout = 2) {
  $start = microtime(TRUE);
  if ($timeout <= 0.0) {
    $timeout = -1;
  }
  $pingUrl = $this
    ->constructUrl(self::PING_SERVLET);

  // Attempt a HEAD request to the Solr ping url.
  $options = array(
    'method' => 'HEAD',
    'timeout' => $timeout,
  );
  $response = $this
    ->makeHttpRequest($pingUrl, $options);
  if ($response->code == 200) {

    // Add 1 µs to the ping time so we never return 0.
    return microtime(TRUE) - $start + 1.0E-6;
  }
  else {
    return FALSE;
  }
}