Erlang 风格的 Node.Js?

发布于 2024-10-07 11:32:38 字数 287 浏览 2 评论 0原文

对于 Node.Js 和 Erlang 来说,我完全是个菜鸟。 但是是否可以构建一个模拟 Erlang 行为的 Node.js 应用程序呢?

例如,您可以在分布式 Node.js 服务器公园中传递 json 消息,甚至可以将新代码传递到这些服务器而不需要离线,就像 erlang 一样。

如果您有一个在收到消息时激活的消息处理程序回调,则该消息处理程序可以检查该消息是否是代码更新消息,从而用新代码替换其自身(当前处理程序)。

因此,应该可以让 Node.Js 服务器无需停机进行代码更新,而无需太多麻烦,对吧?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷︶言冷语的世界 2024-10-14 11:32:39

我假设使用 脚本模块 您可以执行 javascript 而无需重新加载服务器。

主管

Nodejs 的一个小主管脚本。
它运行您的程序并监视
代码更改,因此您可以拥有热代码
重新加载行为,没有
担心内存泄漏并进行
确保你清理了所有模块间的内容
参考文献,并且没有全新的
需要系统。

但话又说回来,当它检测到文件更改时,它会重新加载(非常短的离线时间)。

I assume With script module you could execute javascript without reloading the server.

supervisor

A little supervisor script for nodejs.
It runs your program, and watches for
code changes, so you can have hot-code
reloading-ish behavior, without
worrying about memory leaks and making
sure you clean up all the inter-module
references, and without a whole new
require system.

But then again it will reload(very short time offline) when it detects file changes.

獨角戲 2024-10-14 11:32:38

不完全正确。

  1. 是的,你可以分发 JSON 消息。
  2. 热代码替换部分有点复杂,让我解释一下……

好吧,首先你显然需要进行验证等,这不应该是一个大问题。第一个小问题来自 JSON,它不允许其中包含任何 JS 代码/函数,您可以通过以字符串形式发送数据来解决这个问题。

下一个问题是,当您想要替换函数/方法时,您需要确保它保留其范围,以便新编译的函数可以访问相同的内容。

借助一些黑暗的eval魔法,这是当然可能,但不要指望它会像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();

输出

Hello World
42
Huh, old world?
1764

这不保证在100中工作占所有案例和范围的百分比。另外,语法很糟糕,所以我建议如果你想要热插拔,就继续使用 Erlang。

请记住:用正确的工具完成正确的工作。

更新
在不久的将来不会有比这更好的事情了,请参阅:
https://github.com/ry/node/issues /issue/46#issue/46/comment/610779

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

软的没边 2024-10-14 11:32:38

这是一个比较使用 Erlang 和 Node.js 的体验的博客:

http://blog.mysyncpad.com/post/2073441622/node-js-vs-erlang-syncpads-experience

这是另一个比较,故意比较速度:

< a href="http://jlouisramblings.blogspot.com/2010/12/differences- Between-nodejs-and-erlang_14.html" rel="nofollow">http://jlouisramblings.blogspot.com/2010/12/ -nodejs-和-erlang_14.html之间的差异

Here is a blog comparing experiences using Erlang and Node.js:

http://blog.mysyncpad.com/post/2073441622/node-js-vs-erlang-syncpads-experience

Here is another comparison which purposefully does not compare speed as such:

http://jlouisramblings.blogspot.com/2010/12/differences-between-nodejs-and-erlang_14.html

简单 2024-10-14 11:32:38

没有足够的点来进行内联评论,但我想回复 Ivo Wetzel 在 rvirding 帖子中的评论。 mysyncpad 上有一个更新的博客,其中作者使用了nodejs 和v8 开发人员特别推荐的nodejs 版本。

http://blog.mysyncpad.com/post/2143658273/syncpad -node-js-服务器重访

Don't have enough points to comment inline, but I wanted to respond to Ivo Wetzel's comment above at rvirding's post. There is an updated blog on mysyncpad where the author uses the version of nodejs specifically recommended by the nodejs and v8 developers.

http://blog.mysyncpad.com/post/2143658273/syncpad-node-js-server-revisited

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文