网络/Go语言 – “GET”请求的解剖

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

Network/Golang - Anatomy of a "GET" request

问题

我想这个问题以前可能已经被问过并得到了答案,但我不知道该搜索什么来找到它。这似乎是你在计算机网络课程中学到的内容,而我没有学过。如果有人能指导我找到答案,那就太好了。

所以,问题是:

在许多语言中,比如Go和JavaScript,通过http/https向API发送“GET”请求相对简单。但在相对高级的描述中,底层发生了什么?

我之所以问这个问题,是因为我需要更好地理解如何处理当事情不按预期运行时收到的各种错误。

为了给你一个大致的答案,我将参考Golang中进行http调用的方法。在Golang中,你可以导入net/http包,并通过http.Get("http://www.example.com")这样的方式进行调用。但你也可以指定一个“client”,你可以配置它具有特定的自定义头部等。你还可以在客户端内部指定一些更复杂的东西,比如使用的传输方式等,这些都不是我真正感兴趣的,除非它们能解决我的问题。

基本上,问题可以归结为:Go是完全并发的,而在我的情况下是并行的。我每秒向一个API发送大约300个请求。根据golang文档的建议,客户端在并发使用时是安全的,因此应该只实例化一次 - 实际上,我有一个全局客户端,通过它流动每个请求。

如果我收到一个连接被关闭的错误,那会影响一个请求还是多个请求?我需要对客户端做些什么来获取另一个连接吗?

此外,是每次请求一个连接,还是每个请求都会建立一个新的连接?连接在请求之间是否保持持久性,什么决定了它们何时关闭?

这个问题可能有些杂乱无章,可能不太清楚。如果有人能回答其中的任何部分,我将非常感激。

英文:

I imagine this is something that has been asked before, and answered, but I have no idea what to search to find it. This occurs to me as something that you would learn as part of a college course on computer networking - something I never took. If someone can point me towards the answer, that would be great.

So, the question is:

In many languages, like Go and JavaScript, it is relatively simple to make a "GET" request to an API over http/https. But what is happening under the surface, in a relatively high-level description?

I ask because I need a better understanding to grasp how to handle various errors that I receive when things don't behave as they should.

For an idea of the kind of answer I'm going for, I'll refer to the Golang method of making http calls. In Golang, you can import the net/http package and make the call as such http.Get("http://www.example.com"). But you can also specify a "client" which you can configure to have things like specific customized headers, etc. You can specify some more complex things inside the client like the transport used, and more, stuff that I'm not really interested in unless it solves my problem.

Basically, it comes down to this: Go is completely concurrent, and in my case parallel. I'm making around 300 requests every second to one API. The suggestion from the golang docs is that clients are safe for concurrent use, so should only instantiated once - essentially, I have one global client through which flows every request.

If I get an error saying that a connection has been killed, does that affect one request, many requests, do I have to do anything to the client to get another connection?

Also, is it one request per connection at a time, or is there a new connection made for every request? Are connections persistent between requests, what determines when they die?

This question is probably pretty rambling, and may not make sense. If anyone can answer any section of it, that would greatly be appreciated.

答案1

得分: 3

网络:

DNS将域名解析为IP地址;TCP添加端口(如HTTP默认端口80)和其他信息;HTTP添加头部、表单数据和其他信息。最后,你的HTTP加载到IP数据包中,通过网络适配器发送,并由互联网中的路由器进行处理,最终到达正确的HTTP服务器。

net/http:

我们在Golang文档中对所有与传输层相关的问题都有答案,主要在这里。请仔细阅读。

更新:

连接错误只会影响一个请求。即使多个请求使用同一个TCP连接,因为请求-响应是一个一个进行的,所以一个连接错误不会导致两个请求失败。

英文:

the network:

dns->ip->tcp->http. DNS resolve the domain to a ip address; tcp adding a port (like HTTP default 80) and others; http adding headers, form data and others. finally your http load in ip packet send from your network adapter and well processed by routers in Internet and will arrive to right http server.

net/http:

we have answer to all your transport layer questions in golang document, mainly this. please read it carefully.

update:

a connection error just effect one request. even if multiple request use same tcp connection, because the request-reponse pair goes one bye one, so one connection error will never make two request fail.

huangapple
  • 本文由 发表于 2015年10月20日 08:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/33226248.html
匿名

发表评论

匿名网友

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

确定