Skip to content

Commit

Permalink
Improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangmiao committed Feb 19, 2013
1 parent 02ba0f2 commit 1061e17
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
raw raw
build build
.lock-wscript .lock-wscript
node_modules
13 changes: 8 additions & 5 deletions examples/low-level.js
Original file line number Original file line Diff line number Diff line change
@@ -1,28 +1,31 @@
var Curl = require('../lib/Curl') var Curl = require('..').Curl


var p = console.log; var p = console.log;
var url = process.argv[2]; var url = process.argv[2];


var curl = new Curl(); var curl = new Curl();


if (!url) if (!url)
url = 'www.yahoo.com'; url = 'www.yahoo.com';


curl.setopt('URL', url); curl.setopt('URL', url);
curl.setopt('CONNECTTIMEOUT', 2); curl.setopt('CONNECTTIMEOUT', 2);


// on 'data' must be returns chunk.length, or means interrupt the transfer // on 'data' must be returns chunk.length, or means interrupt the transfer
curl.on('data', function(chunk) { curl.on('data', function(chunk) {
return chunk.length; p("receive " + chunk.length)
return chunk.length;
}); });


curl.on('error', function(e) { curl.on('error', function(e) {
curl.close(); p("error: " + e.message)
curl.close();
}); });




curl.on('end', function() { curl.on('end', function() {
curl.close(); p('done.')
curl.close();
}); });


curl.perform(); curl.perform();
16 changes: 13 additions & 3 deletions lib/Curl.toffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@ Curl::perform = ->
Curl.process() Curl.process()
@ @


m = 0
p = console.log
Curl.process = -> Curl.process = ->
if Curl.in_process if Curl.in_process
return return
do once = -> do once = ->
num = Curl.process_() n = Curl.process_()
if num > 0 if n > 0
Curl.in_process = true Curl.in_process = true
setTimeout once, 80 if n > 8192 && m < 10
++m
process.nextTick once
else
m = 0
w = (8192 - n) * 80 / 8192
if w < 0
w = 0
setTimeout once, w
else else
Curl.in_process = false Curl.in_process = false


Expand Down
1 change: 1 addition & 0 deletions lib/CurlBuilder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/CurlBuilder.toffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CurlBuilder
curl = -> curl = ->
curl.perform.apply curl, arguments curl.perform.apply curl, arguments


curl.Curl = Curl
curl.perform = (args...) -> curl.perform = (args...) ->
if @running if @running
throw new Error 'the cURL session is busy, use curl.create to create another cURL Session' throw new Error 'the cURL session is busy, use curl.create to create another cURL Session'
Expand Down
6 changes: 5 additions & 1 deletion src/node-curl.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class NodeCurl
static bool is_ref; static bool is_ref;
static std::map< CURL*, NodeCurl* > curls; static std::map< CURL*, NodeCurl* > curls;
static int count; static int count;
static int transfered;


CURL * curl; CURL * curl;
v8::Persistent<v8::Object> handle; v8::Persistent<v8::Object> handle;
Expand Down Expand Up @@ -107,6 +108,7 @@ class NodeCurl
// curl write function mapping // curl write function mapping
static size_t write_function(char *ptr, size_t size, size_t nmemb, void *userdata) static size_t write_function(char *ptr, size_t size, size_t nmemb, void *userdata)
{ {
transfered += size * nmemb;
NodeCurl *nodecurl = (NodeCurl*)userdata; NodeCurl *nodecurl = (NodeCurl*)userdata;
return nodecurl->on_write(ptr, size * nmemb); return nodecurl->on_write(ptr, size * nmemb);
} }
Expand Down Expand Up @@ -294,6 +296,7 @@ class NodeCurl
// int process() // int process()
static v8::Handle<v8::Value> process(const v8::Arguments & args) static v8::Handle<v8::Value> process(const v8::Arguments & args)
{ {
transfered = 0;
if (running_handles > 0) if (running_handles > 0)
{ {
CURLMcode code; CURLMcode code;
Expand Down Expand Up @@ -333,7 +336,7 @@ class NodeCurl
} }
} }
} }
return v8::Integer::New(running_handles); return v8::Integer::New(transfered + (int)(running_handles > 0));
} }


// perform() // perform()
Expand Down Expand Up @@ -452,5 +455,6 @@ int NodeCurl::running_handles = 0;
bool NodeCurl::is_ref = false; bool NodeCurl::is_ref = false;
std::map< CURL*, NodeCurl* > NodeCurl::curls; std::map< CURL*, NodeCurl* > NodeCurl::curls;
int NodeCurl::count = 0; int NodeCurl::count = 0;
int NodeCurl::transfered = 0;


#endif #endif

0 comments on commit 1061e17

Please sign in to comment.