X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_pool_api.c;h=83413d25262558c715640d8f1516e93bf76f8c2e;hp=5b04472566e05111f51d04e56216dabcc6407561;hb=e65e4aef04bbda60b8fb12985afbfaa069de9aee;hpb=6aca56d611a22eb559098f3c02c31634a6f9ff9f diff --git a/http_request_pool_api.c b/http_request_pool_api.c index 5b04472..83413d2 100644 --- a/http_request_pool_api.c +++ b/http_request_pool_api.c @@ -23,17 +23,32 @@ #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 #endif -#ifndef HAVE_CURL_MULTI_STRERROR -# define curl_multi_strerror(dummy) "unknown error" +#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 +63,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 +259,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); } /* }}} */ @@ -261,13 +275,24 @@ PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool) int MAX; fd_set R, W, E; struct timeval timeout = {1, 0}; +#ifdef HAVE_CURL_MULTI_TIMEOUT + long max_tout = 1000; + + if ((CURLM_OK == curl_multi_timeout(pool->ch, &max_tout)) && (max_tout != -1)) { + timeout.tv_sec = max_tout / 1000; + timeout.tv_usec = (max_tout % 1000) * 1000; + } +#endif FD_ZERO(&R); FD_ZERO(&W); FD_ZERO(&E); if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) { - if (MAX == -1 || SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) { + if (MAX == -1) { + http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / HTTP_MCROSEC)); + return SUCCESS; + } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) { return SUCCESS; } }