1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-21 16:46:11 +02:00

Allow setting stream chunk size in SocketHandler (#1129)

This commit is contained in:
Klemen Bratec
2018-06-18 11:31:46 +02:00
committed by Jordi Boggiano
parent fd29c89e3c
commit e8db808dd3
2 changed files with 65 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ class SocketHandler extends AbstractProcessingHandler
private $timeout = 0;
private $writingTimeout = 10;
private $lastSentBytes = null;
private $chunkSize = null;
private $persistent = false;
private $errno;
private $errstr;
@@ -127,6 +128,16 @@ class SocketHandler extends AbstractProcessingHandler
$this->writingTimeout = (float) $seconds;
}
/**
* Set chunk size. Only has effect during connection in the writing cycle.
*
* @param float $bytes
*/
public function setChunkSize($bytes)
{
$this->chunkSize = $bytes;
}
/**
* Get current connection string
*
@@ -177,6 +188,16 @@ class SocketHandler extends AbstractProcessingHandler
return $this->writingTimeout;
}
/**
* Get current chunk size
*
* @return float
*/
public function getChunkSize()
{
return $this->chunkSize;
}
/**
* Check to see if the socket is currently available.
*
@@ -219,6 +240,16 @@ class SocketHandler extends AbstractProcessingHandler
return stream_set_timeout($this->resource, $seconds, $microseconds);
}
/**
* Wrapper to allow mocking
*
* @see http://php.net/manual/en/function.stream-set-chunk-size.php
*/
protected function streamSetChunkSize()
{
return stream_set_chunk_size($this->resource, $this->chunkSize);
}
/**
* Wrapper to allow mocking
*/
@@ -268,6 +299,7 @@ class SocketHandler extends AbstractProcessingHandler
{
$this->createSocketResource();
$this->setSocketTimeout();
$this->setStreamChunkSize();
}
private function createSocketResource()
@@ -290,6 +322,13 @@ class SocketHandler extends AbstractProcessingHandler
}
}
private function setStreamChunkSize()
{
if ($this->chunkSize && !$this->streamSetChunkSize()) {
throw new \UnexpectedValueException("Failed setting chunk size with stream_set_chunk_size()");
}
}
private function writeToSocket($data)
{
$length = strlen($data);