Skip to content

Commit

Permalink
timers: use uv_now instead of Date.now
Browse files Browse the repository at this point in the history
This saves a few calls to gettimeofday which can be expensive, and
potentially subject to clock drift. Instead use the loop time which
uses hrtime internally.

fixes #5497
  • Loading branch information
tjfontaine committed May 23, 2013
1 parent f58eb8f commit f8193ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var lists = {};
// the main function - creates lists on demand and the watchers associated
// with them.
function insert(item, msecs) {
item._idleStart = Date.now();
item._idleStart = Timer.now();
item._idleTimeout = msecs;

if (msecs < 0) return;
Expand Down Expand Up @@ -75,7 +75,7 @@ function listOnTimeout() {

debug('timeout callback %d', msecs);

var now = Date.now();
var now = Timer.now();
debug('now: %s', now);

var first;
Expand Down Expand Up @@ -165,7 +165,7 @@ exports.active = function(item) {
if (!list || L.isEmpty(list)) {
insert(item, msecs);
} else {
item._idleStart = Date.now();
item._idleStart = Timer.now();
L.append(list, item);
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ var Timeout = function(after) {

Timeout.prototype.unref = function() {
if (!this._handle) {
var now = Date.now();
var now = Timer.now();
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
if (delay < 0) delay = 0;
Expand Down
4 changes: 3 additions & 1 deletion lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var stream = require('stream');
var assert = require('assert').ok;
var constants = require('constants');

var Timer = process.binding('timer_wrap').Timer;

var DEFAULT_CIPHERS = 'ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:' + // TLS 1.2
'RC4:HIGH:!MD5:!aNULL:!EDH'; // TLS 1.0

Expand Down Expand Up @@ -720,7 +722,7 @@ function onhandshakestart() {

var self = this;
var ssl = self.ssl;
var now = Date.now();
var now = Timer.now();

assert(now >= ssl.lastHandshakeTime);

Expand Down
9 changes: 9 additions & 0 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class TimerWrap : public HandleWrap {
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("Timer"));

NODE_SET_METHOD(constructor, "now", Now);

NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(constructor, "ref", HandleWrap::Ref);
NODE_SET_PROTOTYPE_METHOD(constructor, "unref", HandleWrap::Unref);
Expand Down Expand Up @@ -163,6 +165,13 @@ class TimerWrap : public HandleWrap {
MakeCallback(wrap->object_, ontimeout_sym, ARRAY_SIZE(argv), argv);
}

static Handle<Value> Now(const Arguments& args) {
HandleScope scope(node_isolate);

double now = static_cast<double>(uv_now(uv_default_loop()));
return scope.Close(v8::Number::New(now));
}

uv_timer_t handle_;
};

Expand Down

0 comments on commit f8193ab

Please sign in to comment.