2 回答

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
你必須關(guān)閉resp.Body。MakeRequests
導(dǎo)致錯(cuò)誤的另一個(gè)問題是您嘗試訪問 StatusCodewhen respis nil 由于錯(cuò)誤。
這是固定的代碼。
func MakeRequests(url string, ch chan<- int, wg *sync.WaitGroup) {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
resp, err := http.Get(url)
if resp != nil {
ch <- resp.StatusCode
defer func() {
_, err = io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
} else {
ch <- -1 // because main expect exactly totalHit values in ch
}
if err != nil {
fmt.Println(err)
}
wg.Done()
}

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用以下庫:
Requests:一個(gè) Go 庫,用于減少發(fā)出 HTTP 請(qǐng)求時(shí)的麻煩(20k/s req)
https://github.com/alessiosavi/Requests
這個(gè)想法是分配一個(gè)請(qǐng)求列表,而不是使用可配置的“并行”因子發(fā)送它們,該因子允許一次只運(yùn)行“N”個(gè)請(qǐng)求。
// This array will contains the list of request
var reqs []requests.Request
// N is the number of request to run in parallel, in order to avoid "TO MANY OPEN FILES. N have to be lower than ulimit threshold"
var N int = 12
// Create the list of request
for i := 0; i < 1000; i++ {
// In this case, we init 1000 request with same URL,METHOD,BODY,HEADERS
req, err := requests.InitRequest("https://127.0.0.1:5000", "GET", nil, nil, true)
if err != nil {
// Request is not compliant, and will not be add to the list
log.Println("Skipping request [", i, "]. Error: ", err)
} else {
// If no error occurs, we can append the request created to the list of request that we need to send
reqs = append(reqs, *req)
}
}
此時(shí),我們有一個(gè)列表,其中包含必須發(fā)送的請(qǐng)求。讓我們并行發(fā)送它們!
// This array will contains the response from the givens request
var response []datastructure.Response
// send the request using N request to send in parallel
response = requests.ParallelRequest(reqs, N)
// Print the response
for i := range response {
// Dump is a method that print every information related to the response
log.Println("Request [", i, "] -> ", response[i].Dump())
// Or use the data present in the response
log.Println("Headers: ", response[i].Headers)
log.Println("Status code: ", response[i].StatusCode)
log.Println("Time elapsed: ", response[i].Time)
log.Println("Error: ", response[i].Error)
log.Println("Body: ", string(response[i].Body))
}
您可以在存儲(chǔ)庫的示例文件夾中找到示例用法。
- 2 回答
- 0 關(guān)注
- 144 瀏覽
添加回答
舉報(bào)