我应该直接连接到CouchDB的套接字并传递HTTP请求或使用node.js作为代理吗? [英] Should I connect directly to CouchDB's socket and pass HTTP requests or use node.js as a proxy?

查看:188
本文介绍了我应该直接连接到CouchDB的套接字并传递HTTP请求或使用node.js作为代理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是我的原始问题产生了所有这些

我正在使用Appcelerator Titanium来开发iPhone应用程序(最终也是Android)。我正在使用Titanium的 Titanium直接连接到CouchDB的端口.Network.TCPSocket对象。我相信它使用Apple SDK的CFSocket / NSStream类。

I'm using Appcelerator Titanium to develop an iPhone app (eventually Android too). I'm connecting to CouchDB's port directly by using Titanium's Titanium.Network.TCPSocket object. I believe it utilizes the Apple SDK's CFSocket/NSStream class.

连接后,我只需写:

'GET / mydb / _changes?filter = app / myfilter& feed = continuous& gameid = 4& heartbeat = 30000 HTTP / 1.1 \\\\\\\\ n'

直接发送到套接字。它使永久打开并在每次更新数据库时返回JSON数据并匹配过滤器和更改请求。很酷。

directly to the socket. It keeps it open "forever" and returns JSON data whenever the db is updated and matches the filter and change request. Cool.

我想知道,这样可以直接连接到CouchDB的套接字,或者我最好先打开socket到node.js,也许使用这个 CouchDB node.js模块来通过node.js处理CouchDB代理?

I'm wondering, is it ok to connect directly to CouchDB's socket like this, or would I be better off opening the socket to node.js instead, and maybe using this CouchDB node.js module to handle the CouchDB proxy through node.js?

我主要担心的是表现。我对CouchDB没有足够的经验来知道是否点击它的套接字并直接传递虚假的HTTP请求是好的做法。寻找任何后果或其他建议的经验和意见。

My main concern is performance. I just don't have enough experience with CouchDB to know if hitting its socket and passing faux HTTP requests directly is good practice or not. Looking for experience and opinions on any ramifications or alternate suggestions.

推荐答案

这是我的话。 : - )

It's me again. :-)

CouchDB继承了Erlang的超级并发处理,Erlang是它编写的语言.Erlang在这些进程之间使用轻量级进程和消息传递,以在高并发性下实现出色的性能加载。它也将利用所有cpu内核。

CouchDB inherits super concurrency handling from Erlang, the language it was written in. Erlang uses lightweight processes and message passing between those processes to achieve excellent performance under high concurrent load. It will take advantage of all cpu cores, too.

Nodejs运行一个进程,基本上只在该进程中执行一件事。它的基于事件的非阻塞IO方法确实允许它在等待IO块时进行多任务处理,但它一次只能做一件事。

Nodejs runs a single process and basically only does one thing at a time within that process. Its event-based, non-blocking IO approach does allow it to multitask while it waits for chunks of IO but it still only does one thing at a time.

两者都应该容易处理数以万计的连接,但我希望CouchDB能够比Node更好地处理并发(并且您的工作量更少)。请记住,如果将Node放在CouchDB之前,Node会增加一些延迟。但是,如果你将它们放在不同的机器上,这可能只会引人注意。

Both should easily handle tens of thousands of connections, but I would expect CouchDB to handle concurrency better (and with less effort on your part) than Node. And keep in mind that Node adds some latency if you put it in front of CouchDB. That may only be noticeable if you have them on different machines, though.

只要你写一个格式正确的HTTP,就可以通过TCPSocket直接写入Couch请求遵循规范。 (你没有传递一个虚假的请求......这是一个真实的HTTP请求,就像其他任何一个一样。)

Writing directly to Couch via TCPSocket is a-ok as long as your write a well-formed HTTP request that follows the spec. (You're not passing a faux request...that's a real HTTP request you're sending just like any other.)

注意:HTTP 1.1确实要求你在请求中包含一个Host头,因此您需要更正代码以反映该OR,只需使用HTTP 1.0,而不需要它来保持简单。 (我很好奇你为什么不使用Titanium.Network.HTTPClient。它是否只在请求完成后才给你请求体?或者什么?)

Note: HTTP 1.1 does require you to include a Host header in the request, so you'll need to correct your code to reflect that OR just use HTTP 1.0 which doesn't require it to keep things simple. (I'm curious why you're not using Titanium.Network.HTTPClient. Does it only give you the request body after the request finishes or something?)

无论如何, CouchDB可以完全处理直接连接 - 除非你花费很多精力进入你的Node代理 - 当你有10万人同时玩游戏时,它可能会给用户带来更好的体验。

Anyway, CouchDB can totally handle direct connections and--unless you put a lot of effort into your Node proxy--it's probably going to give users a better experience when you have 100k of them playing the game at once.

编辑:如果您使用Node编写实际的HTTP代理。这比使用您提供的模块运行速度快得多,并且实现起来更简单。 (而不是定义您自己的API然后向Couch发出请求,您可以将某些请求传递给CouchDB并阻止其他请求,例如,出于安全原因。

If you use Node write an actual HTTP proxy. That will run a lot faster than using the module you provided and be simpler to implement. (Rather than defining your own API that then makes requests to Couch you can just pass certain requests on to CouchDB and block others, say, for security reasons.

看看多节点的工作原理:
http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/

Also take a look at how "multinode" works: http://www.sitepen.com/blog/2010/07/14/multi-node-concurrent-nodejs-http-server/

这篇关于我应该直接连接到CouchDB的套接字并传递HTTP请求或使用node.js作为代理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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