英文:
Handle concurrent http requests in Go
问题
我想重新编写一个具有大量并发用户的应用程序。想象一个通过Websocket
连接的聊天应用程序,拥有数百万用户。我过去使用Python
+Django
+UWSGI
进行优化,并且这个架构到今天为止一直工作得很好。现在我发现连接变慢,出现超时等问题。
我决定使用Go
语言并放弃Python,我的问题是我找不到类似于UWSGI的东西,我知道Go具有内置的并发能力。这意味着Go服务器不需要像UWSGI那样的东西,可以处理并发请求吗?还是Go中提到的并发只是简单的I/O?
我需要最大的性能和并发性。我是否正确理解了这个问题?这两者是等价的吗?Go能帮助我吗?我们不需要像UWSGI那样的东西,Go的核心能力在这个问题上可以更快、更好地工作吗?
英文:
I want to re-write an app with large amount of concurrent users. Imagine a chat app that has millions of users connected via Websocket
. Used to I fine tune Python
+Django
+UWSGI
and an architecture that worked by today. Now I see slow connections and timeouts and so on.
I decided to use Go
language and drop Python and my question is that I couldn't find anything like UWSGI and I know Go has builtin concurrent capabilities. This means a Go server don't need something like UWSGI and can handle concurrent requests? or concurrency mentioned in go is just about simple I/O?
I need performance and concurrency in it maximum. I understand this issue well? These two are equivalent? Go can help me? and we don't need something like UWSGI and Go's core capabilities can work much faster and better in this issue?
答案1
得分: 0
你应该看一下Go通道。Go通道与Go协程结合使用,可以轻松扩展同一进程中的并发性。与Java线程相比,Go协程非常高效,并且上下文切换较少,仅消耗4kb内存。在Go中,你不共享内存,而是通过将状态推送到通道来进行状态通信,这样就不需要上下文切换来访问共享内存。
Go中的通道的工作方式类似于使用Apache Kafka或RabbitMq等排队系统。
我在以下博文中详细介绍了这个问题。
https://marcofranssen.nl/concurrency-in-go
如果你对Go还不太熟悉,我还建议你看一下我的其他文章。
https://marcofranssen.nl/categories/golang
按照从旧到新的顺序阅读它们,以熟悉这门语言,之后再进行快速入门教程。
一旦你了解了Go通道,就知道如何高效地实现内部逻辑。对于外部面向API,我建议选择https://grpc.io/,它允许在TCP级别进行通信。它允许在两个方向上进行流式实现。
英文:
You should have a look at Go channels. Go channels in combination with Go routines can easily scale concurrency within the same process. Compared to Java Threading, Go routines are very efficient and have less context switching. Only 4kb mem consumption. In Go you don't share memory, but instead communicate state by pushing them on the channels, which results in no context switching to access shared memory.
Channels in Go work in a similar fashion as you would use some queuing system like Apache Kafka or RabbitMq.
I wrote an extensive example on this in following blogpost.
https://marcofranssen.nl/concurrency-in-go
If you are really new to go I can also advice to have a look at my other articles.
https://marcofranssen.nl/categories/golang
Read them from old to new to get familiar with the language after doing the quick start tutorial.
Once you know about Go channels you know how to efficiently implement the internal logic. For the external facing API I would choose https://grpc.io/ which allows to communicate on the TCP level. It allows to do streaming implementations in both directions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论