网络套接字服务器端的处理模型 [英] Web sockets server side processing model

查看:145
本文介绍了网络套接字服务器端的处理模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要实现服务器支持使用网络插座,做服务器保持与每个客户端的开放HTTP连接的客户端?哪有这种规模?

To implement a server supporting clients using web sockets, do servers keep an open HTTP connection with each client? How can this scale?

实施这种类型的服务器时,什么是编程模型?即:大多数的网络应用程序的servlet,等它支持连接 - >请求 - >响应 - >紧密型模式。而与网络插座在连接无限期停留开放。

What are the "programming models" when implementing this type of server? Ie: most web apps have servlets, etc which support a connect->request->response->close type model. Whereas with web sockets the connect stays open indefinitely.

推荐答案

您通常需要在工作的异步型号为这些长期连接工作。有做异步I / O几种不同的技术;所有这些都有各自的优点和缺点。

You generally need to work in an asynchronous model for these long-lived connections to work. There are several different techniques for doing asynchronous I/O; all of which have their advantages and disadvantages.

其中一个应该已经熟悉的人谁使用JavaScript和 AJAX 工作是回调模型;在你发送请求,并安装一个回调完成时被调用。这是怎么 XMLHTT prequest 的作品,而不会阻塞所有其他网页,而他们等待一个页面的请求来完成。这也是扭曲的Python网络框架是如何工作的,虽然它可以调用取决于你使用的接口的对象或回调函数的方法。

One which should already be familiar to anyone who has worked with JavaScript and AJAX is the callback model; in which you send a request, and install a callback to be called when it completes. This is how XMLHTTPRequest works, without blocking all of the other pages while they wait for one page's request to finish. This is also how the Twisted Python networking framework works, though it can call methods on objects or callback functions depending on the interfaces you use.

另一个强大的模式是二郎式的方法,称为Actor模型,有很多很多轻量级进程(线程一样,但与没有共享状态),其每一个通过异步消息相互通信。 Erlang的运行时间已经实施,使产卵数千过程非常有效;那么你可以为每个连接一个进程,并将它们发送消息到实现应用程序的后台其他进程。 Erlang进程也可以自动安排在多个操作系统线程,采取多核心系统的优势。 ejabberd ,一个流行的Jabber服务器(一个聊天协议,它需要许多长寿的开放连接),二郎中实现,因为是 Facebook的聊天系统

Another powerful model is the Erlang style approach, called the Actor model, has many, many lightweight processes (like threads, but with no shared state), each of which communicate with each other via asynchronous messages. The Erlang runtime has been implemented to make spawning thousands of processes very efficient; then you can just have one process for each connection, and have them send messages to other processes implementing the backend of your application. Erlang processes can also be automatically scheduled on multiple OS threads, to take full advantage of multi-core systems. ejabberd, a popular Jabber server (a chat protocol, which requires many long-lived open connections), is implemented in Erlang, as is the Facebook Chat system.

新的去语言从谷歌采用了类似的做法,更接近霍尔的沟通比Erlang的演员模型顺序,但里面有很多的相似之处

The new Go language from Google uses a similar approach, closer to Hoare's Communicating Sequential than Erlang's Actor model, but which has a lot of similarities.

在Mac OS X 10.6,苹果推出大中央调度,与块(基本闭包)中的C,C ++以及和Objective-C。这允许类似的AJAX或扭曲式事件驱动回调模式,但与该顺序执行多线程,多核心环境来管理访问共享资源明确管理的队列。双绞线和JavaScript运行两个单线程,所以只能采取单核心的优势,除非你使用多个操作系统进程,它可以是相当沉重的重量,并增加它们之间的通信费用。

In Mac OS X 10.6, Apple introduced Grand Central Dispatch, along with blocks (essentially closures) in C, C++, and Objective-C. This allows something like the AJAX or Twisted style event driven callback model, but with explicitly managed queues that are executed sequentially to manage access to shared resources in a multithreaded, multi-core environment. Twisted and JavaScript both run single threaded, and so can only take advantage of a single core, unless you use multiple operating system processes, which can be fairly heavy weight and increase the costs of communication between them.

还有一些更传统的车型,像Unix 选择 功能,或更现代的,有能力的 的epoll 的kqueue() 。在这些,你通常有一个主循环在你的程序,其中规定了一堆的事件来监视(网络I / O返回更多的数据,文件I / O返回更多的数据,一个新的网络连接时,等等) ,然后调用系统调用阻塞,直到其中一个事件发生了,在这一点上,你检查所出现的是一个,然后适当地处理它。这些系统调用通常用于提供上述的更高级别的框架。

Then there are the more traditional models, like the Unix select function, or the more modern and capable epoll or kqueue(). In these, you generally have a main loop in your program, which sets up a bunch of events to watch for (network I/O returns some more data, file I/O returns more data, a new network connection is made, etc), and then calls a system call that blocks until one of those events has occurred, at which point you check which one has occurred and then handle it appropriately. These system calls are generally used to provide the higher-level frameworks described above.

有关的可用选项的一系列惊人的一个很好的概述(侧重于比较传统,和较低的水平,接近的Unix),看到的的C10K问题,不同的技术来帮助在一次处理10000个并发连接的调查。这也有C和C ++库一个很好的列表在抽象各种可用的API,如的libevent

For a very good overview of the staggering array of options available (focusing on the more traditional, and lower level, Unix approaches), see The C10K Problem, a survey of different techniques for helping to deal with 10,000 simultaneous connections at once. This also has a good list of C and C++ libraries for abstracting over the various APIs available, such as libevent.

一个最后的选择,当然,是使用一个过程或为每个连接一个OS线程。问题是,过程是非常重的重量,甚至线程是相当重的重量相比,许多的这些选项。一般情况下,为了获得最佳性能,你会希望有一个进程或每CPU线程,每个使用异步I / O API弄清楚时,它需要做的工作,然后派遣了工作几个对象或回调之一已注册处理连接,或正在等待消息的几个二郎风格轻量级进程中的一个,或诸如此类的东西。

A final option, of course, is to use one process or one OS thread for each connection. The problem is, processes are very heavy weight, and even threads are fairly heavy weight compared to many of these options. In general, for the best performance, you would want to have one process or thread per CPU, each using an asynchronous I/O API to figure out when it needs to do work, and then dispatching that work to one of several objects or callbacks that have been registered to handle connections, or one of several Erlang style lightweight processes that is waiting for a message, or something of the sort.

作为一个方面说明,在网络插座的连接不是HTTP连接,而是一个新的协议中, WebSocket协议,虽然你可以使用相同的端口如HTTP,并且为了升级HTTP连接到一个网络插座与现有的防火墙规则兼容。

As a side note, the connection in web sockets are not HTTP connections, but a new protocol, the websocket protocol, though you can use the same port as HTTP, and upgrade an HTTP connection to a web socket in order to be compatible with existing firewall rules.

这篇关于网络套接字服务器端的处理模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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