瀏覽代碼

Merge pull request #1318 from b3nl/patch-1

Adding curl sharing to fix #1313
Jordi Boggiano 6 年之前
父節點
當前提交
64125db2ae
共有 1 個文件被更改,包括 46 次插入7 次删除
  1. 46 7
      src/Monolog/Handler/LogglyHandler.php

+ 46 - 7
src/Monolog/Handler/LogglyHandler.php

@@ -14,6 +14,7 @@ namespace Monolog\Handler;
 use Monolog\Logger;
 use Monolog\Logger;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\LogglyFormatter;
 use Monolog\Formatter\LogglyFormatter;
+use function array_key_exists;
 
 
 /**
 /**
  * Sends errors to Loggly.
  * Sends errors to Loggly.
@@ -28,6 +29,13 @@ class LogglyHandler extends AbstractProcessingHandler
     protected const ENDPOINT_SINGLE = 'inputs';
     protected const ENDPOINT_SINGLE = 'inputs';
     protected const ENDPOINT_BATCH = 'bulk';
     protected const ENDPOINT_BATCH = 'bulk';
 
 
+    /**
+     * Caches the curl handlers for every given endpoint.
+     *
+     * @var array
+     */
+    protected $curlHandlers = [];
+
     protected $token;
     protected $token;
 
 
     protected $tag = [];
     protected $tag = [];
@@ -50,6 +58,42 @@ class LogglyHandler extends AbstractProcessingHandler
         parent::__construct($level, $bubble);
         parent::__construct($level, $bubble);
     }
     }
 
 
+    /**
+     * Loads and returns the shared curl handler for the given endpoint.
+     *
+     * @param string $endpoint
+     *
+     * @return resource
+     */
+    protected function getCurlHandler(string $endpoint)
+    {
+        if (!array_key_exists($endpoint, $this->curlHandlers)) {
+            $this->curlHandlers[$endpoint] = $this->loadCurlHandler($endpoint);
+        }
+
+        return $this->curlHandlers[$endpoint];
+    }
+
+    /**
+     * Starts a fresh curl session for the given endpoint and returns its handler.
+     *
+     * @param string $endpoint
+     *
+     * @return resource
+     */
+    private function loadCurlHandler(string $endpoint)
+    {
+        $url = sprintf("https://%s/%s/%s/", static::HOST, $endpoint, $this->token);
+
+        $ch = curl_init();
+
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_POST, true);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+        return $ch;
+    }
+
     /**
     /**
      * @param string[]|string $tag
      * @param string[]|string $tag
      */
      */
@@ -94,7 +138,7 @@ class LogglyHandler extends AbstractProcessingHandler
 
 
     protected function send(string $data, string $endpoint): void
     protected function send(string $data, string $endpoint): void
     {
     {
-        $url = sprintf("https://%s/%s/%s/", static::HOST, $endpoint, $this->token);
+        $ch = $this->getCurlHandler($endpoint);
 
 
         $headers = ['Content-Type: application/json'];
         $headers = ['Content-Type: application/json'];
 
 
@@ -102,15 +146,10 @@ class LogglyHandler extends AbstractProcessingHandler
             $headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag);
             $headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag);
         }
         }
 
 
-        $ch = curl_init();
-
-        curl_setopt($ch, CURLOPT_URL, $url);
-        curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
 
-        Curl\Util::execute($ch);
+        Curl\Util::execute($ch, 5, false);
     }
     }
 
 
     protected function getDefaultFormatter(): FormatterInterface
     protected function getDefaultFormatter(): FormatterInterface