Go如何处理Google App Engine上的并发请求
I'm a little confused on how Go handles concurrent requests on Google App Engine. So I'm hoping someone can provide some clarity.
Here are the facts that I have gathered:
Go is single threaded on App Engine. - this is because it is possible to do arbitrary pointer arithmetic by creating race conditions with multiple threads
If Go is single threaded on App Engine then point 3 is moot. This leaves 1 and 2. If Go on App Engine is single threaded and threads are required to continue execution while blocking for I/O, then it seems that an App Engine Go instance will block all goroutines while waiting on I/O.
Is this correct? If not how does Go's concurrency really work on App Engine?
To help quantify things. If I were to hold a connection open for 30 seconds. How may concurrent connections could a single AE Go instance maintain?
Thank you.
EDIT: here's the feature request which will allow Go Instance to handle more then 10 concurrent request Allow configurable limit of concurrent requests per instance. Please star it.
我对Go如何处理Google App Engine上的并发请求有些困惑。 因此,我希望有人可以提供一些说明。 p>
以下是我收集到的事实: p>
-
Go是App Engine上的单线程。 -这是因为可以通过创建具有多个线程的竞争条件来执行任意指针算术 p> li >
-
将例行程序多路复用到多个OS线程上,因此如果一个线程应该阻塞, 例如在等待I / O时,其他人将继续运行。 p> li>
-
[App Engine有10个并发限制] [该限制是通过限制每个运行时的并发线程来实施的。 在大多数此类情况下,我们的调度程序将尝试启动一个新实例。 p> li> ol>
如果Go在App Engine上是单线程的,则 第三点没有意义。 剩下1和2。如果Go on App Engine是单线程的,并且要求线程在阻止I / O的同时继续执行,那么看来App Engine Go实例将在等待I / O时阻止所有goroutine。 p>
这正确吗? 如果不是,那么Go的并发在App Engine上如何真正起作用? p>
以帮助量化事物。 如果我要保持连接打开30秒钟。 单个AE Go实例如何维护并发连接? p>
谢谢。 p>
编辑:这是功能请求,它将 允许Go实例 处理10个以上的并发请求允许以下限制: 每个 实例并发请求。 请为它加注星标。 p> blockquote> div>
A Go App Engine instance allows 10 concurrent requests, but only runs one CPU thread. In effect, multiple requests can be processed concurrently, but only one will be able to do CPU work at a time. If one request is, say, waiting for a datastore API call to return, another request is free to be processed by the same instance.
Your statement "If Go is single threaded on App Engine then point 3 is moot." is incorrect. There is still a limit of 10 concurrent in-flight requests to a single Go App Engine instance. The documentation is a bit loose with words when it talks about "threads".
I must admit I have no inside knowledge of AppEngine. This is all speculation and guessing, but I think it is somewhat reasonable.
Your app will never reach the ten thread limit. This is because there are very few reasons for threads to be created. First, the maximum number of goroutines running at once is set to one to enforce memory safety. Second, unlike a normal go program, app engine does not actually need to make syscalls. The only time it does is for networking. All IO in appengine can be muxed into one epoll thread. This means all you need is two threads at any given time. You can then add a third or forth thread in case every once in a while you need to run other syscalls such as allocating memory and accepting/closing connections. These are fast syscalls that block for a very small amount of time. Even with these other syscalls, you are still very far from the ten thread limit.
Concurrency is not affected because in the end, everything you do in appengine boils down to waiting for something over the network to return. You do not need multiple threads to be doing many things at once.