doufei16736
doufei16736
2015-10-20 00:46
浏览 28
已采纳

网络/ Golang-剖析“ GET”请求

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.

图片转代码服务由CSDN问答提供 功能建议

我想这是之前已被询问并得到回答的东西,但我不知道该寻找什么 它。 在我看来,这是您将在大学计算机网络课程中学到的东西-我从未学习过。 如果有人可以将我指向答案,那就太好了。

因此,问题是:

在许多语言中,例如Go和JavaScript ,通过http / https向API发出“ GET”请求相对简单。 但是,在一个相对高级的描述中,表面之下发生了什么?

我之所以问是因为我需要更好的理解,以掌握如何处理在事情不做时收到的各种错误。

有关我要寻求的答案的想法,我将参考进行http调用的Golang方法。 在Golang中,您可以导入 net / http 包,并以 http.Get(“ http://www.example.com”)进行调用。 但是,您还可以指定一个“客户端”,您可以对其进行配置以具有特定的自定义标头等内容。您可以在客户端内部指定一些更复杂的内容,例如所使用的传输方式,还有我不真正感兴趣的东西 除非它解决了我的问题。

基本上可以归结为:Go是完全并发的,在我的情况下是并行的。 我每秒向一个API发出约300个请求。 golang docs “的建议是,客户端可以安全地并发使用,因此只能实例化一次 -本质上,我有一个全局客户端,每个请求都通过该客户端流动。

如果我收到一条错误消息,说连接已被终止,是否会影响一个请求,很多请求,我有吗?

还可以对客户端执行任何操作以获取另一个连接吗?

此外,一次是每个连接一个请求,还是为每个请求建立了新连接? 请求之间的连接是否持久,由什么决定它们何时终止?

这个问题可能是一个很棘手的问题,可能没有任何意义。 如果任何人都可以回答它的任何部分,将不胜感激。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhanbi0027
    dongzhanbi0027 2015-10-20 01:36
    已采纳

    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.

    点赞 评论

相关推荐