Understanding goroutines for web API

huangapple go评论67阅读模式
英文:

Understanding goroutines for web API

问题

刚开始学习Go语言,希望创建一个简单的Web API。我正在研究使用Gorilla mux(http://www.gorillatoolkit.org/pkg/mux)来处理Web请求。

我不确定如何最好地使用Go的并发选项来处理这些请求。我是否在某个地方读到过main函数实际上是一个goroutine,或者我应该在接收到每个请求时将其分派给一个goroutine?如果我理解得不对,请原谅。

英文:

Just starting out with Go and hoping to create a simple Web API. I'm looking into using Gorilla mux (http://www.gorillatoolkit.org/pkg/mux) to handle web requests.

I'm not sure how to best use Go's concurrency options to handle the requests. Did I read somewhere that the main function is actually a goroutine or should I dispatch each request to a goroutine as they are received? Apologies if I'm "way off".

答案1

得分: 10

假设你正在使用Go语言的http.ListenAndServe来处理你的HTTP请求,文档明确说明每个传入的连接都会由一个单独的goroutine来处理。你通常会在main函数中调用ListenAndServe

Gorilla mux只是一个用于更灵活地路由请求到处理程序的包,比http.DefaultServeMux更灵活。它实际上并不处理传入的连接或请求,只是简单地将其传递给你的处理程序。

我强烈建议你阅读一些文档,特别是这篇关于编写Web应用程序的指南:https://golang.org/doc/articles/wiki/#tmp_3

英文:

Assuming you're using the Go's http.ListenAndServe to serve your http requests, the documentation clearly states that each incoming connection is handled by a separate goroutine for you. http://golang.org/pkg/net/http/#Server.Serve
You would usually call ListenAndServe from your main function.

Gorilla mux is simply a package for more flexible routing of requests to your handlers than the http.DefaultServeMux. It doesn't actually handle the incoming connection or request just simply relays it to your handler.

I highly suggest you read a bit of the documentation, specifically this guide https://golang.org/doc/articles/wiki/#tmp_3 on writing web applications.

答案2

得分: 0

我尽量提供翻译,尽管我投票关闭了这个问题,因为它太宽泛。

无论如何,这些都不是真正必要的。你想得太多了。如果你还没有阅读过这篇文章,它看起来是一个不错的教程:http://thenewstack.io/make-a-restful-json-api-go/

你可以像大多数典型的 REST 框架一样设置路由,让 web 服务器/框架在请求处理层面处理并发。只有在需要从一个文件夹中聚合 10 个文件的数据时,你才需要使用 goroutine 来生成请求的响应。这是一个假设的例子,但在这种情况下,你可以为每个文件启动一个 goroutine,通过从一个非阻塞的 select 通道中读取信息来聚合所有的数据,然后返回结果。如果有意义的话,你可以期望代码的所有入口点都以异步、非阻塞的方式被调用...

英文:

I'm providing an answer even though I voted to close for being too broad.

Anyway, none of that is really necessary. You're over thinking it. If you haven't read this it looks like a decent tutorial; http://thenewstack.io/make-a-restful-json-api-go/

You can really just set up routes like you would with most typical rest frameworks and let the webserver/framework worry about concurrency at the request handling level. You would only employ goroutines to generate the response of a request, say if you needed to aggregate data from 10 files that are all in a folder. Contrived example, but this is where you would spin off 1 goroutine per file, aggregate all the information by reading off a channel in a non-blocking select and then return the result. You can expect all points of entry to your code are called in an asynchronous, non-blocking fashion if that makes sense...

huangapple
  • 本文由 发表于 2015年5月7日 00:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/30082009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定