Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Networked GC #75

Closed
wants to merge 4 commits into from

3 participants

@thejh

Doesn't break any tests, adds a new working test.

Note: The test needs isaacs/tap-runner#6. With older tap-runner versions, it complains about missing gc() function.

thejh added some commits
@thejh

Ah, btw: This needs substack/dnode-protocol#8.

@temsa

need !

@substack substack closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2011
  1. @thejh
Commits on Nov 16, 2011
  1. @thejh

    add automatic culling

    thejh authored
  2. @thejh

    Merge remote-tracking branch 'origin/master' into networked-gc

    thejh authored
    Conflicts:
    	index.js
    	package.json
  3. @thejh

    add a (working) gc test :)

    thejh authored
    Note: this needs isaacs/tap-runner#6.
    With older tap-runner versions, it complains about missing
    `gc()` function.
This page is out of date. Refresh to see the latest.
Showing with 81 additions and 5 deletions.
  1. +8 −5 examples/auth/client.js
  2. +16 −0 index.js
  3. +1 −0  package.json
  4. +56 −0 test/gc.js
View
13 examples/auth/client.js
@@ -16,11 +16,14 @@ DNode.connect(7007, function (remote,conn) {
remote.authenticate(user, pass, function (session) {
if (session) {
sys.puts('Authentication success');
- session.quote(function (q) {
- sys.puts('\nAnd now for a quote by ' + q.who + ':\n\n');
- sys.puts(q.quote + '\n\n');
- conn.end();
- });
+ function showNext() {
+ session.quote(function (q) {
+ sys.puts('\nAnd now for a quote by ' + q.who + ':\n\n');
+ sys.puts(q.quote + '\n\n');
+ });
+ setTimeout(showNext, 5000);
+ }
+ showNext();
}
else {
sys.puts('Authentication failure');
View
16 index.js
@@ -5,6 +5,7 @@ var EventEmitter = require('events').EventEmitter;
var protocol = require('dnode-protocol');
var Lazy = require('lazy');
+var weak = require('weak');
var SocketIO = require('./lib/stream_socketio');
exports = module.exports = dnode;
@@ -220,6 +221,21 @@ dnode.prototype.listen = function () {
function createClient (proto, stream) {
var client = proto.create();
+ client.remoteStore.on('wrapped', function(id, wrappedObject) {
+ var f = wrappedObject[id].f
+ wrappedObject[id] = {}
+ f = weak(f, function() {
+ var cullNotify = typeof wrappedObject[id] === 'object';
+ delete wrappedObject[id];
+ if (cullNotify) {
+ delete client.remoteStore.items[id];
+ client.remoteStore.emit('cull', id);
+ }
+ })
+ wrappedObject[id].__defineGetter__('f', function() {
+ return weak.get(f);
+ })
+ })
process.nextTick(function () {
if (client.listeners('error').length === 0) {
View
1  package.json
@@ -25,6 +25,7 @@
"socket.io" : "0.8.6",
"socket.io-client" : "https://github.com/substack/socket.io-client/tarball/master",
"lazy" : ">=1.0.5 <1.1",
+ "weak": "*",
"traverse" : "0.4.x",
"dnode-protocol" : "0.1.x",
"jsonify" : "0.0.x"
View
56 test/gc.js
@@ -0,0 +1,56 @@
+#! node --expose-gc
+var dnode = require('../');
+var test = require('tap').test;
+var weak = require('weak');
+
+test('gc', function (t) {
+ t.plan(2);
+ var port = Math.floor(Math.random() * 40000 + 10000);
+
+ var gcLoopIterations = 0;
+ var server_ok = false, client_ok = false;
+
+ function forced_gc_loop() {
+ gc();
+ if (++gcLoopIterations < 50) {
+ setTimeout(forced_gc_loop, 100);
+ } else {
+ if (!server_ok || !client_ok) t.end();
+ setTimeout(function() {
+ process.exit();
+ }, 1000);
+ }
+ }
+ forced_gc_loop();
+
+ function onSuccess() {
+ t.end();
+ setTimeout(process.exit, 500);
+ }
+
+ var server = dnode({
+ callMeBack : function (fn) {
+ fn('calling back!');
+ weak(fn, function() {
+ t.ok(true);
+ server_ok = true;
+ if (client_ok) onSuccess();
+ });
+ }
+ }).listen(port);
+
+ server.on('ready', function () {
+ dnode.connect(port, function (remote, conn) {
+ function weakCb() {
+ t.ok(true);
+ client_ok = true;
+ if (server_ok) onSuccess();
+ }
+ (function() {
+ var cb = function(){};
+ remote.callMeBack(cb);
+ weak(cb, weakCb);
+ })();
+ });
+ });
+});
Something went wrong with that request. Please try again.