Просмотр исходного кода

Allow setting stream chunk size in SocketHandler (#1129)

Klemen Bratec 7 лет назад
Родитель
Сommit
e8db808dd3
2 измененных файлов с 65 добавлено и 0 удалено
  1. 39 0
      src/Monolog/Handler/SocketHandler.php
  2. 26 0
      tests/Monolog/Handler/SocketHandlerTest.php

+ 39 - 0
src/Monolog/Handler/SocketHandler.php

@@ -27,6 +27,7 @@ class SocketHandler extends AbstractProcessingHandler
     private $timeout = 0;
     private $timeout = 0;
     private $writingTimeout = 10;
     private $writingTimeout = 10;
     private $lastSentBytes = null;
     private $lastSentBytes = null;
+    private $chunkSize = null;
     private $persistent = false;
     private $persistent = false;
     private $errno;
     private $errno;
     private $errstr;
     private $errstr;
@@ -127,6 +128,16 @@ class SocketHandler extends AbstractProcessingHandler
         $this->writingTimeout = (float) $seconds;
         $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
      * Get current connection string
      *
      *
@@ -177,6 +188,16 @@ class SocketHandler extends AbstractProcessingHandler
         return $this->writingTimeout;
         return $this->writingTimeout;
     }
     }
 
 
+    /**
+     * Get current chunk size
+     *
+     * @return float
+     */
+    public function getChunkSize()
+    {
+        return $this->chunkSize;
+    }
+
     /**
     /**
      * Check to see if the socket is currently available.
      * 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);
         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
      * Wrapper to allow mocking
      */
      */
@@ -268,6 +299,7 @@ class SocketHandler extends AbstractProcessingHandler
     {
     {
         $this->createSocketResource();
         $this->createSocketResource();
         $this->setSocketTimeout();
         $this->setSocketTimeout();
+        $this->setStreamChunkSize();
     }
     }
 
 
     private function createSocketResource()
     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)
     private function writeToSocket($data)
     {
     {
         $length = strlen($data);
         $length = strlen($data);

+ 26 - 0
tests/Monolog/Handler/SocketHandlerTest.php

@@ -77,6 +77,13 @@ class SocketHandlerTest extends TestCase
         $this->assertEquals(10.25, $this->handler->getWritingTimeout());
         $this->assertEquals(10.25, $this->handler->getWritingTimeout());
     }
     }
 
 
+    public function testSetChunkSize()
+    {
+        $this->createHandler('localhost:1234');
+        $this->handler->setChunkSize(1025);
+        $this->assertEquals(1025, $this->handler->getChunkSize());
+    }
+
     public function testSetConnectionString()
     public function testSetConnectionString()
     {
     {
         $this->createHandler('tcp://localhost:9090');
         $this->createHandler('tcp://localhost:9090');
@@ -120,6 +127,19 @@ class SocketHandlerTest extends TestCase
         $this->writeRecord('Hello world');
         $this->writeRecord('Hello world');
     }
     }
 
 
+    /**
+     * @expectedException UnexpectedValueException
+     */
+    public function testExceptionIsThrownIfCannotSetChunkSize()
+    {
+        $this->setMockHandler(array('streamSetChunkSize'));
+        $this->handler->setChunkSize(8192);
+        $this->handler->expects($this->once())
+            ->method('streamSetChunkSize')
+            ->will($this->returnValue(false));
+        $this->writeRecord('Hello world');
+    }
+
     /**
     /**
      * @expectedException RuntimeException
      * @expectedException RuntimeException
      */
      */
@@ -304,6 +324,12 @@ class SocketHandlerTest extends TestCase
                 ->will($this->returnValue(true));
                 ->will($this->returnValue(true));
         }
         }
 
 
+        if (!in_array('streamSetChunkSize', $methods)) {
+            $this->handler->expects($this->any())
+                ->method('streamSetChunkSize')
+                ->will($this->returnValue(8192));
+        }
+
         $this->handler->setFormatter($this->getIdentityFormatter());
         $this->handler->setFormatter($this->getIdentityFormatter());
     }
     }
 }
 }