- persistent handles for curl_share and curl_multi
authorMichael Wallner <mike@php.net>
Sun, 28 Jan 2007 10:49:47 +0000 (10:49 +0000)
committerMichael Wallner <mike@php.net>
Sun, 28 Jan 2007 10:49:47 +0000 (10:49 +0000)
http.c
http_request_datashare_api.c
http_request_pool_api.c
package2.xml
php_http_request_pool_api.h

diff --git a/http.c b/http.c
index eccffd8e0bc7ed4576f5958e7d88db1b41ec35e8..fc7f15a3964361f984200bc6403ebf905642351e 100644 (file)
--- a/http.c
+++ b/http.c
@@ -31,6 +31,7 @@
 #include "php_http_request_method_api.h"
 #ifdef HTTP_HAVE_CURL
 #      include "php_http_request_api.h"
+#      include "php_http_request_pool_api.h"
 #      include "php_http_request_datashare_api.h"
 #      ifdef HTTP_HAVE_PERSISTENT_HANDLES
 #              include "php_http_persistent_handle_api.h"
@@ -289,6 +290,7 @@ PHP_MINIT_FUNCTION(http)
 #ifdef HTTP_HAVE_CURL
 #      ifdef HTTP_HAVE_PERSISTENT_HANDLES
                        (SUCCESS != PHP_MINIT_CALL(http_persistent_handle)) ||
+                       (SUCCESS != PHP_MINIT_CALL(http_request_pool)) ||
 #      endif
                        (SUCCESS != PHP_MINIT_CALL(http_request))       ||
 #      ifdef ZEND_ENGINE_2
index 013ecac3585344c78fa510229b775703f8789a06..0e7dbf6d8c172924c7b08fb9a628a15f60058baa 100644 (file)
@@ -21,6 +21,9 @@
 #include "php_http_request_datashare_api.h"
 #include "php_http_request_api.h"
 #include "php_http_request_object.h"
+#ifdef HTTP_HAVE_PERSISTENT_HANDLES
+#      include "php_http_persistent_handle_api.h"
+#endif
 
 #ifndef HAVE_CURL_SHARE_STRERROR
 #      define curl_share_strerror(dummy) "unknown error"
 #      define curl_easy_strerror(dummy) "unknown error"
 #endif
 
+#ifdef HTTP_HAVE_PERSISTENT_HANDLES
+#      define HTTP_CURL_SHARE_CTOR(ch) (SUCCESS == http_persistent_handle_acquire("http_request_datashare", &(ch)))
+#      define HTTP_CURL_SHARE_DTOR(chp) http_persistent_handle_release("http_request_datashare", (chp))
+#else
+#      define HTTP_CURL_SHARE_CTOR(ch) ((ch) = curl_share_init())
+#      define HTTP_CURL_SHARE_DTOR(chp) curl_share_cleanup(*(chp)); *(chp) = NULL
+#endif
+
 static HashTable http_request_datashare_options;
 static http_request_datashare http_request_datashare_global;
 static int http_request_datashare_compare_handles(void *h1, void *h2);
@@ -47,6 +58,16 @@ PHP_MINIT_FUNCTION(http_request_datashare)
 {
        curl_lock_data val;
        
+#ifdef HTTP_HAVE_PERSISTENT_HANDLES
+       if (SUCCESS != http_persistent_handle_provide("http_request_datashare", curl_share_init, (http_persistent_handle_dtor) curl_share_cleanup)) {
+               return FAILURE;
+       }
+#endif
+       
+       if (!http_request_datashare_init_ex(&http_request_datashare_global, 1)) {
+               return FAILURE;
+       }
+       
        zend_hash_init(&http_request_datashare_options, 4, NULL, NULL, 1);
 #define ADD_DATASHARE_OPT(name, opt) \
        val = opt; \
@@ -56,8 +77,6 @@ PHP_MINIT_FUNCTION(http_request_datashare)
        ADD_DATASHARE_OPT("ssl", CURL_LOCK_DATA_SSL_SESSION);
        ADD_DATASHARE_OPT("connect", CURL_LOCK_DATA_CONNECT);
        
-       http_request_datashare_init_ex(&http_request_datashare_global, 1);
-       
        return SUCCESS;
 }
 
@@ -92,8 +111,7 @@ PHP_HTTP_API http_request_datashare *_http_request_datashare_init_ex(http_reques
        }
        memset(share, 0, sizeof(http_request_datashare));
        
-       HTTP_CHECK_CURL_INIT(share->ch, curl_share_init(), ;);
-       if (!share->ch) {
+       if (!HTTP_CURL_SHARE_CTOR(share->ch)) {
                if (free_share) {
                        pefree(share, persistent);
                }
@@ -180,7 +198,7 @@ PHP_HTTP_API void _http_request_datashare_dtor(http_request_datashare *share TSR
                zend_llist_destroy(share->handles);
                efree(share->handles);
        }
-       curl_share_cleanup(share->ch);
+       HTTP_CURL_SHARE_DTOR(&share->ch);
 #ifdef ZTS
        if (share->persistent) {
                int i;
index 154161404a4f3d0cb8f9c6cf9f1df4770ff6c59f..29df94f12a5203e4bcc2b0416f8588e88176a567 100644 (file)
@@ -23,6 +23,7 @@
 #include "php_http_request_object.h"
 #include "php_http_request_pool_api.h"
 #include "php_http_requestpool_object.h"
+#include "php_http_persistent_handle_api.h"
 
 #ifndef HTTP_DEBUG_REQPOOLS
 #      define HTTP_DEBUG_REQPOOLS 0
 #      define curl_multi_strerror(dummy) "unknown error"
 #endif
 
+#ifdef HTTP_HAVE_PERSISTENT_HANDLES
+#      define HTTP_CURL_MULTI_CTOR(ch) (SUCCESS == http_persistent_handle_acquire("http_request_pool", &(ch)))
+#      define HTTP_CURL_MULTI_DTOR(chp) http_persistent_handle_release("http_request_pool", (chp))
+#else
+#      define HTTP_CURL_MULTI_CTOR(ch) ((ch) = curl_multi_init())
+#      define HTTP_CURL_MULTI_DTOR(chp) curl_multi_cleanup(*(chp)); *(chp) = NULL
+#endif
+
 static int http_request_pool_compare_handles(void *h1, void *h2);
 
+#ifdef HTTP_HAVE_PERSISTENT_HANDLES
+PHP_MINIT_FUNCTION(http_request_pool)
+{
+       if (SUCCESS != http_persistent_handle_provide("http_request_pool", curl_multi_init, (http_persistent_handle_dtor) curl_multi_cleanup)) {
+               return FAILURE;
+       }
+       return SUCCESS;
+}
+#endif
+
 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
 {
@@ -48,8 +67,7 @@ PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool
                pool->ch = NULL;
        }
 
-       HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
-       if (!pool->ch) {
+       if (!HTTP_CURL_MULTI_CTOR(pool->ch)) {
                if (free_pool) {
                        efree(pool);
                }
@@ -245,7 +263,7 @@ PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
        pool->unfinished = 0;
        zend_llist_clean(&pool->finished);
        zend_llist_clean(&pool->handles);
-       curl_multi_cleanup(pool->ch);
+       HTTP_CURL_MULTI_DTOR(&pool->ch);
 }
 /* }}} */
 
index 12bcc978745d136853e3db71133b0f05583b9835..f90f0228feb1c72c53fae4f60f5f782f28cd4143 100644 (file)
@@ -40,7 +40,8 @@ support. Parallel requests are available for PHP 5 and greater.
  <license>BSD, revised</license>
  <notes><![CDATA[
 + Added HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"]) (Clay Loveless)
-+ Added --enable-http-persistent-handles (Clay Loveless)
++ Added --enable-http-persistent-handles (per-process persistent cURL handles) (Clay Loveless)
++ Added http_persistent_handles_count(), http_persistent_handles_clean([string name])
 ]]></notes>
  <contents>
   <dir name="/">
index aff9c1eebcf2d81465052f796be5d3c78c3df6e1..84772553739222621696928b89cc01f40beb63e0 100644 (file)
@@ -26,6 +26,10 @@ typedef struct _http_request_pool_t {
 typedef int (*http_request_pool_apply_func)(http_request_pool *pool, zval *request TSRMLS_DC);
 typedef int (*http_request_pool_apply_with_arg_func)(http_request_pool *pool, zval *request, void *arg TSRMLS_DC);
 
+#ifdef HTTP_HAVE_PERSISTENT_HANDLES
+PHP_MINIT_FUNCTION(http_request_pool);
+#endif
+
 #define http_request_pool_responsehandler(p, r, c) _http_request_pool_responsehandler((p), (r), (c) TSRMLS_CC)
 extern int _http_request_pool_responsehandler(http_request_pool *pool, zval *req, void *ch TSRMLS_DC);