在Erlang风格的Node.Js? [英] Node.Js in Erlang style?

查看:165
本文介绍了在Erlang风格的Node.Js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Node.Js和Erlang,我都是一个完整的noob。
但是不可能构建一个模仿Erlang行为的Node.js应用程序?



你可以通过分布式的node.js服务器公园传递json消息,甚至将新的代码传递给那些没有离线的服务器,就像erlang一样。



如果你有一个消息处理程序回调在接收到消息时被激活,然后该消息处理程序可以检查消息是否是代码更新消息,从而用新代码替换自己(当前处理程序)。



所以应该有可能让Node.Js服务器没有停机时间代码更新太多了,对吧?

解决方案

  1. 是的,您可以发布JSON消息

  2. 部分使用热代码替换有点复杂让我解释...

好的,首先你显然需要有验证等地方,那不应该是一个大问题。第一个小问题来自于JSON,它不允许任何JS代码/函数,你可以通过发送数据作为字符串来解决这个问题。



下一个问题,当你想要替换函数/方法时,你需要确保它保持它的作用域,以便新编译的函数可以访问相同的东西。



有些黑暗 eval 魔术这是肯定可能,但不要指望它在任何地方都像Erlang一样自然:

  var Script = process.binding('evals')。 

var hello ='Hello World';
var test = 42;
function Swappable(initCode){
this.execute = function(){}
this.swap = function(code){
this.execute = eval('func =' +代码);
}
this.swap(initCode);
}

//注意:Swappable的范围是有限的,它不会继承它创建的本地范围...
var foo = new Swappable('function (){console.log(hello); return function(){console.log(test)}}')
var cb = foo.execute();
cb();

foo.swap('function(){console.log(Huh,old world?); return function(){console.log(test * test)}}');
var cb = foo.execute();
cb();
console.log(bar.execute());
foo.execute();



输出



  Hello World 
42
嗯,老世界?
1764

这不能保证在所有案例和范围的100%中工作。此外,语法是可怕的,所以我建议你想要热插拔,留在Erlang。



记住:正确的工作正确的工具。 / strong>



更新

在不久的将来,将不会有任何更好的看法, >
https://github.com/ ry / node / issues / issue / 46#issue / 46 / comment / 610779


I am a complete noob when it comes to both Node.Js and Erlang. But wouldn't it be possible to build a Node.js app that emulates Erlang behavior?

e.g. you pass json messages across an distributed node.js server park and even pass new code to those servers w/o going offline, just like erlang.

If you have a message handler callback that is activated when a message is received, then this message handler could check if the message is a code update message and thus replace itself(the current handler) with the new code.

So it should be possible to have Node.Js servers with no downtime for code updates w/o too much fuss, right?

解决方案

Not completely right.

  1. Yes you could distribute JSON messages
  2. The part with hot code replacement is a bit more complicated let me explain...

OK, first you obviously need to have validation etc. in place, that shouldn't be a big problem. The first small problem arises from JSON, which does not allow for any JS code/functions in it, well you can work around that by sending the data as a string.

Next problem, when you want to replace function/method you need to make sure that it keeps it's scope, so that the newly compiled functions has access to the same things.

With some dark eval magic this is certainly possible, but don't expect it to be anywhere near as natural as it's in Erlang:

var Script = process.binding('evals').Script;

var hello = 'Hello World';
var test = 42;
function Swappable(initCode) {
    this.execute = function() {}
    this.swap = function(code) {
        this.execute = eval('func = ' + code);
    }
    this.swap(initCode);
}

// Note: Swappable's scope is limited, it won't inherit the local scope in which it was created...
var foo = new Swappable('function(){console.log(hello);return function(){console.log(test)}}')
var cb = foo.execute();
cb();

foo.swap('function(){console.log("Huh, old world?");return function(){console.log(test * test)}}');
var cb = foo.execute();
cb();
console.log(bar.execute());
foo.execute();

Output

Hello World
42
Huh, old world?
1764

This is not guaranteed to work in 100% of all cases and scopes. Also, the syntax is horrible, so I'd suggest if you want hot swapping, stay with Erlang.

Remember: Right tool for the right job.

Update
There won't be anything better than that in the near future see:
https://github.com/ry/node/issues/issue/46#issue/46/comment/610779

这篇关于在Erlang风格的Node.Js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆