Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From 22a6f3b87f9f22a2f70de1cec574593a67dcbd2d Mon Sep 17 00:00:00 2001
From: Timothy J Fontaine <tjfontaine@gmail.com>
Date: Wed, 28 Aug 2013 10:19:08 -0700
Subject: [PATCH] timers: update loop time before main module
We use the loop time for timers to avoid extra syscalls, but because
there's a non-trivial amount of startup time the loop time is stale and
a timer created on the first turn of the loop will fire "early"
explicitly update the loop time before executing the user's javascript
---
lib/module.js | 8 ++++++++
src/uv.cc | 7 +++++++
2 files changed, 15 insertions(+)
diff --git a/lib/module.js b/lib/module.js
index d9e9744..ecf33e3 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -290,6 +290,14 @@ Module._load = function(request, parent, isMain) {
return NativeModule.require(filename);
}
+ if (isMain) {
+ // We use the loop time for timers to avoid extra syscalls, but because
+ // there's a non-trivial amount of startup time the loop time is stale and
+ // a timer created on the first turn of the loop will fire "early"
+ // explicitly update the loop time before executing the user's javascript
+ process.binding('uv').update_time();
+ }
+
var module = new Module(filename, parent);
if (isMain) {
diff --git a/src/uv.cc b/src/uv.cc
index 2da32d4..4e9faa9 100644
--- a/src/uv.cc
+++ b/src/uv.cc
@@ -44,6 +44,11 @@ void ErrName(const FunctionCallbackInfo<Value>& args) {
}
+void UpdateTime(const FunctionCallbackInfo<Value>& args) {
+ uv_update_time(uv_default_loop());
+}
+
+
void Initialize(Handle<Object> target) {
v8::HandleScope handle_scope(node_isolate);
target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "errname"),
@@ -53,6 +58,8 @@ void Initialize(Handle<Object> target) {
Integer::New(UV_ ## name, node_isolate));
UV_ERRNO_MAP(V)
#undef V
+
+ NODE_SET_METHOD(target, "update_time", UpdateTime);
}
--
1.8.2.1 (Apple Git-45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.