From f8193ab3c46475486eab38543650e2388bd3aedc Mon Sep 17 00:00:00 2001 From: Timothy J Fontaine Date: Wed, 22 May 2013 11:32:54 -0700 Subject: [PATCH] timers: use uv_now instead of Date.now 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 --- lib/timers.js | 8 ++++---- lib/tls.js | 4 +++- src/timer_wrap.cc | 9 +++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/timers.js b/lib/timers.js index 2b0745c5a49afa..1412928c51f938 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -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; @@ -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; @@ -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); } } @@ -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; diff --git a/lib/tls.js b/lib/tls.js index 3af078a966e889..cc71bd9761878d 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -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 @@ -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); diff --git a/src/timer_wrap.cc b/src/timer_wrap.cc index 88ea6b5e264db7..90bb553f050c5a 100644 --- a/src/timer_wrap.cc +++ b/src/timer_wrap.cc @@ -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); @@ -163,6 +165,13 @@ class TimerWrap : public HandleWrap { MakeCallback(wrap->object_, ontimeout_sym, ARRAY_SIZE(argv), argv); } + static Handle Now(const Arguments& args) { + HandleScope scope(node_isolate); + + double now = static_cast(uv_now(uv_default_loop())); + return scope.Close(v8::Number::New(now)); + } + uv_timer_t handle_; };