Skip to content

Commit e8db808

Browse files
klemenbSeldaek
authored andcommitted
Allow setting stream chunk size in SocketHandler (#1129)
1 parent fd29c89 commit e8db808

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Monolog/Handler/SocketHandler.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SocketHandler extends AbstractProcessingHandler
2727
private $timeout = 0;
2828
private $writingTimeout = 10;
2929
private $lastSentBytes = null;
30+
private $chunkSize = null;
3031
private $persistent = false;
3132
private $errno;
3233
private $errstr;
@@ -127,6 +128,16 @@ public function setWritingTimeout($seconds)
127128
$this->writingTimeout = (float) $seconds;
128129
}
129130

131+
/**
132+
* Set chunk size. Only has effect during connection in the writing cycle.
133+
*
134+
* @param float $bytes
135+
*/
136+
public function setChunkSize($bytes)
137+
{
138+
$this->chunkSize = $bytes;
139+
}
140+
130141
/**
131142
* Get current connection string
132143
*
@@ -177,6 +188,16 @@ public function getWritingTimeout()
177188
return $this->writingTimeout;
178189
}
179190

191+
/**
192+
* Get current chunk size
193+
*
194+
* @return float
195+
*/
196+
public function getChunkSize()
197+
{
198+
return $this->chunkSize;
199+
}
200+
180201
/**
181202
* Check to see if the socket is currently available.
182203
*
@@ -219,6 +240,16 @@ protected function streamSetTimeout()
219240
return stream_set_timeout($this->resource, $seconds, $microseconds);
220241
}
221242

243+
/**
244+
* Wrapper to allow mocking
245+
*
246+
* @see http://php.net/manual/en/function.stream-set-chunk-size.php
247+
*/
248+
protected function streamSetChunkSize()
249+
{
250+
return stream_set_chunk_size($this->resource, $this->chunkSize);
251+
}
252+
222253
/**
223254
* Wrapper to allow mocking
224255
*/
@@ -268,6 +299,7 @@ private function connect()
268299
{
269300
$this->createSocketResource();
270301
$this->setSocketTimeout();
302+
$this->setStreamChunkSize();
271303
}
272304

273305
private function createSocketResource()
@@ -290,6 +322,13 @@ private function setSocketTimeout()
290322
}
291323
}
292324

325+
private function setStreamChunkSize()
326+
{
327+
if ($this->chunkSize && !$this->streamSetChunkSize()) {
328+
throw new \UnexpectedValueException("Failed setting chunk size with stream_set_chunk_size()");
329+
}
330+
}
331+
293332
private function writeToSocket($data)
294333
{
295334
$length = strlen($data);

tests/Monolog/Handler/SocketHandlerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public function testSetWritingTimeout()
7777
$this->assertEquals(10.25, $this->handler->getWritingTimeout());
7878
}
7979

80+
public function testSetChunkSize()
81+
{
82+
$this->createHandler('localhost:1234');
83+
$this->handler->setChunkSize(1025);
84+
$this->assertEquals(1025, $this->handler->getChunkSize());
85+
}
86+
8087
public function testSetConnectionString()
8188
{
8289
$this->createHandler('tcp://localhost:9090');
@@ -120,6 +127,19 @@ public function testExceptionIsThrownIfCannotSetTimeout()
120127
$this->writeRecord('Hello world');
121128
}
122129

130+
/**
131+
* @expectedException UnexpectedValueException
132+
*/
133+
public function testExceptionIsThrownIfCannotSetChunkSize()
134+
{
135+
$this->setMockHandler(array('streamSetChunkSize'));
136+
$this->handler->setChunkSize(8192);
137+
$this->handler->expects($this->once())
138+
->method('streamSetChunkSize')
139+
->will($this->returnValue(false));
140+
$this->writeRecord('Hello world');
141+
}
142+
123143
/**
124144
* @expectedException RuntimeException
125145
*/
@@ -304,6 +324,12 @@ private function setMockHandler(array $methods = array())
304324
->will($this->returnValue(true));
305325
}
306326

327+
if (!in_array('streamSetChunkSize', $methods)) {
328+
$this->handler->expects($this->any())
329+
->method('streamSetChunkSize')
330+
->will($this->returnValue(8192));
331+
}
332+
307333
$this->handler->setFormatter($this->getIdentityFormatter());
308334
}
309335
}

0 commit comments

Comments
 (0)