使用Go语言进行一次TLS连接上的HTTP GET请求。

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

Http GET requests over one tls connection using Go

问题

目前,我正在使用HTTP GET请求与服务器建立连接。
Get请求是从线程内部调用的。对于每个Get请求,都使用一个线程,但问题是对于每个Get请求,都会建立一个连接。因此,如果有10000个Get请求,将建立10000个连接。然而,我希望首先在我和服务器之间建立一个TLS连接,然后创建一个线程,并从该线程中通过已经建立的连接发送Get请求。

例如:

for {
    1. 建立TLS连接
    2. 创建线程 go func()
}

func() {
    resp, err := http.Get(url) // 应该通过已经建立的TLS连接发送
}
英文:

Currently, I am using http GET requests to connect to a server.
Get request is calling from inside a thread. For each Get request one thread is being used but problem is for each Get request, one connection is established. Thus if there are 10000 Get request then 10000 connections will be established. However, I want first to establishe a TLS connection between me and the server, then create a thread and from that thread I want to send Get over that already established connection.

e.g.

for {
    1. establish a tls connection
    2. create thread go func()
}

func() {
    resp, err := http.Get(url) // should be over already established tls connection
}

答案1

得分: 2

首先,澄清一下,Go语言使用的是goroutines,而不是线程。Goroutines是协作式轻量级进程。Go运行时将一些goroutines分配到可用的操作系统线程上进行时间片切换。这种线程抽象是Go语言的一个重要特性。

关于使用goroutines进行并发GET请求的问题,最好的方法是最初让标准API来处理请求在连接上的多路复用。可能会有大量的请求和较少的支持连接。只要使用保持活动连接(keep-alive connections),你就不需要关心细节。

如果需要对代理、TLS配置、保持活动连接、压缩和其他设置进行控制,可以创建一个Transport。

HTTP/1.1可以同时处理http和https的保持活动连接。当向同一服务器发出多个请求时,这是一个好处。只要在每个请求之后强制关闭连接,你将从保持活动连接中获得很多好处。

通常的原则是:不要过早地进行优化。尽可能清楚地描述你需要做什么。对其进行基准测试(Go语言有一个有用的微基准测试工具)。在决定是否以及如何优化性能之前,请先进行基准测试。

英文:

Firstly, just to clarify, Go has goroutines, not threads. Goroutines are co-operative light-weight processes. The Go runtime time-slices a number of goroutines onto the available operating system threads. This thread abstraction is an important feature of Go.

On the question of making many concurrent GET requests using goroutines, it is best initially to let the standard API handle multiplexing of requests onto connections for you. There may be a large number of requests and a smaller number of supporting connections. Provided you use keep-alive connections, you should not need to care about the details.

For control over proxies, TLS configuration, keep-alives, compression, and other settings, create a Transport

HTTP/1.1 handles keep-alive connections with both http and https. This is a benefit when many requests are made to the same server. As long as you don't force it to close each connection after each request, you will get a lot of benefit from keep-alive connections in your case.

The usual mantra applies: don't optimise prematurely. Describe what you need to do as clearly as possible. Benchmark it (Go has a useful micro-benchmark tool). Do this before you decide whether or how to optimise the performance.

huangapple
  • 本文由 发表于 2015年4月1日 17:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/29386789.html
匿名

发表评论

匿名网友

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

确定