aboutsummaryrefslogtreecommitdiffstats
path: root/funcs
diff options
context:
space:
mode:
authorkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2006-12-16 04:25:09 +0000
committerkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2006-12-16 04:25:09 +0000
commit00990781f38f426b1c2139cfe52ccf7c7e635d04 (patch)
tree552c1ad3635c7b86410f6688df10090c9e0e7816 /funcs
parentce8a3bd620fcf63987362698b154d70188ab8926 (diff)
instead of initializing the curl library every time the CURL() function is invoked, do it only once per thread (this allows multiple calls to CURL() in the dialplan for a channel to run much more quickly, and also to re-use connections to the server) (thanks to JerJer for frequently complaining about this performance problem)
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@48513 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'funcs')
-rw-r--r--funcs/func_curl.c48
1 files changed, 31 insertions, 17 deletions
diff --git a/funcs/func_curl.c b/funcs/func_curl.c
index f6091d025..08e786eaf 100644
--- a/funcs/func_curl.c
+++ b/funcs/func_curl.c
@@ -51,13 +51,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
struct MemoryStruct {
char *memory;
size_t size;
};
-
static void *myrealloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
@@ -82,32 +82,46 @@ static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *da
return realsize;
}
-static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
+static const char *global_useragent = "asterisk-libcurl-agent/1.0";
+
+static void curl_instance_cleanup(void *data)
{
- CURL *curl;
+ CURL **curl = data;
- curl = curl_easy_init();
+ curl_easy_cleanup(*curl);
+}
+
+AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
- if (!curl) {
+static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
+{
+ CURL **curl;
+
+ if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
return -1;
+
+ if (!*curl) {
+ if (!(*curl = curl_easy_init()))
+ return -1;
+ curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
+ curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
+ curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+ curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
}
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);
- curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
- curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
- curl_easy_setopt(curl, CURLOPT_URL, url);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
- curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
+ curl_easy_setopt(*curl, CURLOPT_URL, url);
+ curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk);
if (post) {
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
+ curl_easy_setopt(*curl, CURLOPT_POST, 1);
+ curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
}
- curl_easy_perform(curl);
- curl_easy_cleanup(curl);
+ curl_easy_perform(*curl);
+
+ if (post)
+ curl_easy_setopt(*curl, CURLOPT_POST, 0);
+
return 0;
}