MediaWiki  master
PoolCounterClient.php
Go to the documentation of this file.
1 <?php
22 
24 use PoolCounter;
25 
34  private $conn;
35 
39  private $hostName;
40 
44  private $manager;
45 
49  public function setManager( PoolCounterConnectionManager $manager ): void {
50  $this->manager = $manager;
51  }
52 
56  public function getConn() {
57  if ( !isset( $this->conn ) ) {
58  $status = $this->manager->get( $this->key );
59  if ( !$status->isOK() ) {
60  return $status;
61  }
62  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
63  $this->conn = $status->value['conn'];
64  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
65  $this->hostName = $status->value['hostName'];
66 
67  // Set the read timeout to be 1.5 times the pool timeout.
68  // This allows the server to time out gracefully before we give up on it.
69  stream_set_timeout( $this->conn, 0, (int)( $this->timeout * 1e6 * 1.5 ) );
70  }
71  // TODO: Convert from Status to StatusValue
72  return Status::newGood( $this->conn );
73  }
74 
79  public function sendCommand( ...$args ) {
80  $args = str_replace( ' ', '%20', $args );
81  $cmd = implode( ' ', $args );
82  $status = $this->getConn();
83  if ( !$status->isOK() ) {
84  return $status;
85  }
86  $conn = $status->value;
87  wfDebug( "Sending pool counter command: $cmd\n" );
88  if ( fwrite( $conn, "$cmd\n" ) === false ) {
89  return Status::newFatal( 'poolcounter-write-error', $this->hostName );
90  }
91  $response = fgets( $conn );
92  if ( $response === false ) {
93  return Status::newFatal( 'poolcounter-read-error', $this->hostName );
94  }
95  $response = rtrim( $response, "\r\n" );
96  wfDebug( "Got pool counter response: $response\n" );
97  $parts = explode( ' ', $response, 2 );
98  $responseType = $parts[0];
99  switch ( $responseType ) {
100  case 'LOCKED':
101  $this->onAcquire();
102  break;
103  case 'RELEASED':
104  $this->onRelease();
105  break;
106  case 'DONE':
107  case 'NOT_LOCKED':
108  case 'QUEUE_FULL':
109  case 'TIMEOUT':
110  case 'LOCK_HELD':
111  break;
112  case 'ERROR':
113  default:
114  $parts = explode( ' ', $parts[1], 2 );
115  $errorMsg = $parts[1] ?? '(no message given)';
116  return Status::newFatal( 'poolcounter-remote-error', $errorMsg, $this->hostName );
117  }
118  return Status::newGood( constant( "PoolCounter::$responseType" ) );
119  }
120 
125  public function acquireForMe( $timeout = null ) {
126  $status = $this->precheckAcquire();
127  if ( !$status->isGood() ) {
128  return $status;
129  }
130  return $this->sendCommand( 'ACQ4ME', $this->key, $this->workers, $this->maxqueue,
131  $timeout ?? $this->timeout );
132  }
133 
138  public function acquireForAnyone( $timeout = null ) {
139  $status = $this->precheckAcquire();
140  if ( !$status->isGood() ) {
141  return $status;
142  }
143  return $this->sendCommand( 'ACQ4ANY', $this->key, $this->workers, $this->maxqueue,
144  $timeout ?? $this->timeout );
145  }
146 
150  public function release() {
151  $status = $this->sendCommand( 'RELEASE' );
152 
153  if ( $this->conn ) {
154  $this->manager->close( $this->conn );
155  $this->conn = null;
156  }
157 
158  return $status;
159  }
160 }
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
setManager(PoolCounterConnectionManager $manager)
Helper for \MediaWiki\PoolCounter\PoolCounterClient.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58
Semaphore semantics to restrict how many workers may concurrently perform a task.
Definition: PoolCounter.php:51
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85