Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nan to support node 0.12 #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
language: node_js

node_js:
- 0.8
- 0.10
- '0.8'
- '0.10'
- '0.12'

matrix:
include:
- node_js: '4'
env: CC=clang CXX=clang++

notifications:
email:
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
'target_name': 'toobusy',
'include_dirs': [
"<!(node -e \"require('nan')\")",
],
'sources': [
'toobusy.cc',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"homepage": "https://github.com/lloyd/node-toobusy",
"version": "0.2.4",
"dependencies": {
"bindings": "1.1.0"
"bindings": "1.1.0",
"nan": "2.0.0"
},
"devDependencies": {
"should": "1.2.1",
Expand Down
7 changes: 3 additions & 4 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ describe('maxLag', function() {

describe('toobusy()', function() {
it('should return true after a little load', function(done) {
toobusy.maxLag(50);
function load() {
if (toobusy()) return done();
if (toobusy()) return done(null);
var start = new Date();
while ((new Date() - start) < 250) {
for (var i = 0; i < 1e5;) i++;
}
while ((new Date() - start) < 100) {}
setTimeout(load, 0);
}
load();
Expand Down
57 changes: 30 additions & 27 deletions toobusy.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <v8.h>
#include <nan.h>
#include <node.h>
#include <uv.h>
#include <stdlib.h>
Expand All @@ -22,7 +23,8 @@ static uv_timer_t s_timer;
static uint32_t s_currentLag;
static uint64_t s_lastMark;

Handle<Value> TooBusy(const Arguments& args) {
NAN_METHOD(TooBusy) {
Nan::HandleScope scope;
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.
bool block = false;
Expand All @@ -34,44 +36,40 @@ Handle<Value> TooBusy(const Arguments& args) {
double r = (rand() / (double) RAND_MAX) * 100.0;
if (r < pctToBlock) block = true;
}
return block ? True() : False();
info.GetReturnValue().Set(Nan::New(block));
}

Handle<Value> ShutDown(const Arguments& args) {
NAN_METHOD(ShutDown) {
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.

uv_timer_stop(&s_timer);
return Undefined();
}

Handle<Value> Lag(const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(s_currentLag));
NAN_METHOD(Lag) {
Nan::HandleScope scope;
info.GetReturnValue().Set(Nan::New(s_currentLag));
}

Handle<Value> HighWaterMark(const Arguments& args) {
HandleScope scope;

if (args.Length() >= 1) {
if (!args[0]->IsNumber()) {
return v8::ThrowException(
v8::Exception::Error(
v8::String::New("expected numeric first argument")));
NAN_METHOD(HighWaterMark) {
Nan::HandleScope scope;
if (info.Length() >= 1) {
if (!info[0]->IsNumber()) {
Nan::ThrowError("expected numeric first argument");
return;
}
int hwm = args[0]->Int32Value();
int hwm = info[0]->Int32Value();
if (hwm < 10) {
return v8::ThrowException(
v8::Exception::Error(
v8::String::New("maximum lag should be greater than 10ms")));
Nan::ThrowError("maximum lag should be greater than 10ms");
return;
}
HIGH_WATER_MARK_MS = hwm;
}

return scope.Close(Number::New(HIGH_WATER_MARK_MS));
info.GetReturnValue().Set(Nan::New(HIGH_WATER_MARK_MS));
}

static void every_second(uv_timer_t* handle, int status)
static void every_second(uv_timer_t* handle)
{
uint64_t now = uv_hrtime();

Expand All @@ -85,13 +83,18 @@ static void every_second(uv_timer_t* handle, int status)
s_lastMark = now;
};

extern "C" void init(Handle<Object> target) {
HandleScope scope;
__attribute__((unused)) static void every_second(uv_timer_t* handle, int) {
every_second(handle);
}

extern "C" NAN_MODULE_INIT(init) {
Nan::HandleScope scope;

Nan::SetMethod(target, "toobusy", TooBusy);
Nan::SetMethod(target, "shutdown", ShutDown);
Nan::SetMethod(target, "lag", Lag);
Nan::SetMethod(target, "maxLag", HighWaterMark);

target->Set(String::New("toobusy"), FunctionTemplate::New(TooBusy)->GetFunction());
target->Set(String::New("shutdown"), FunctionTemplate::New(ShutDown)->GetFunction());
target->Set(String::New("lag"), FunctionTemplate::New(Lag)->GetFunction());
target->Set(String::New("maxLag"), FunctionTemplate::New(HighWaterMark)->GetFunction());
uv_timer_init(uv_default_loop(), &s_timer);
uv_timer_start(&s_timer, every_second, POLL_PERIOD_MS, POLL_PERIOD_MS);
};
Expand Down