[svn-commits] kpfleming: branch 1.4 r48513 - /branches/1.4/funcs/func_curl.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Fri Dec 15 21:25:10 MST 2006


Author: kpfleming
Date: Fri Dec 15 22:25:09 2006
New Revision: 48513

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48513
Log:
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)

Modified:
    branches/1.4/funcs/func_curl.c

Modified: branches/1.4/funcs/func_curl.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/funcs/func_curl.c?view=diff&rev=48513&r1=48512&r2=48513
==============================================================================
--- branches/1.4/funcs/func_curl.c (original)
+++ branches/1.4/funcs/func_curl.c Fri Dec 15 22:25:09 2006
@@ -51,12 +51,12 @@
 #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)
 {
@@ -82,32 +82,46 @@
 	return realsize;
 }
 
+static const char *global_useragent = "asterisk-libcurl-agent/1.0";
+
+static void curl_instance_cleanup(void *data)
+{
+	CURL **curl = data;
+
+	curl_easy_cleanup(*curl);
+}
+
+AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
+
 static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
 {
-	CURL *curl;
-
-	curl = curl_easy_init();
-
-	if (!curl) {
+	CURL **curl;
+
+	if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
 		return -1;
-	}
-
-	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");
+
+	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_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_perform(curl);
-	curl_easy_cleanup(curl);
+		curl_easy_setopt(*curl, CURLOPT_POST, 1);
+		curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
+	}
+
+	curl_easy_perform(*curl);
+
+	if (post)
+		curl_easy_setopt(*curl, CURLOPT_POST, 0);
+
 	return 0;
 }
 



More information about the svn-commits mailing list