You are here

function _varnish_read_socket in Varnish 8

Same name and namespace in other branches
  1. 6 varnish.module \_varnish_read_socket()
  2. 7 varnish.module \_varnish_read_socket()

Low-level socket read function.

@params $client an initialized socket client

$retty how many times to retry on "temporarily unavalble" errors

2 calls to _varnish_read_socket()
_varnish_execute_command in ./varnish.module
_varnish_terminal_run in ./varnish.module
Send one or more commands to Varnish.

File

./varnish.module, line 315
varnish.module

Code

function _varnish_read_socket($client, $retry = 2) {

  // Status and length info is always 13 characters.
  $header = socket_read($client, 13, PHP_BINARY_READ);
  if ($header == FALSE) {
    $error = socket_last_error();

    // 35 = socket-unavailable, so it might be blocked from our write.
    // This is an acceptable place to retry.
    if ($error == 35 && $retry > 0) {
      return _varnish_read_socket($client, $retry - 1);
    }
    else {
      \Drupal::logger('varnish')
        ->error('Socket error: @error', [
        '@error' => socket_strerror($error),
      ]);
      return [
        'code' => $error,
        'msg' => socket_strerror($error),
      ];
    }
  }
  $msg_len = (int) substr($header, 4, 6) + 1;
  $status = [
    'code' => substr($header, 0, 3),
    'msg' => socket_read($client, $msg_len, PHP_BINARY_READ),
  ];
  return $status;
}