亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

50000 個并行請求的點擊 URL

50000 個并行請求的點擊 URL

Go
天涯盡頭無女友 2023-08-07 18:52:52
我正在嘗試訪問任何示例 url 來并行 50000 個請求。但我收到各種錯誤。有人可以讓我知道我做錯了什么嗎?或者應該采取什么策略?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 err != nil {        fmt.Println(err)    }    ch <- resp.StatusCode    wg.Done()}func main() {    var waitGroup sync.WaitGroup     count := 0    ch := make(chan int)    url := "http://some-dummy-url/ping"    totalHits, _ := strconv.Atoi("50000")    waitGroup.Add(totalHits)    for i := 0; i < totalHits; i++ {        go MakeRequests(url, ch, &waitGroup)    }    for i := 0; i < totalHits; i++ {        if <-ch == 200 {            count++        }    }    waitGroup.Wait()    fmt.Printf("Total number of successfull request :" + strconv.Itoa(count))}錯誤Get http://some-dummy-url/ping: dial tcp 10.120.0.45:80: connect: can't assign requested addresspanic: runtime error: invalid memory address or nil pointer dereference[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x123da32]goroutine 13425 [running]:main.MakeRequests(0x7ffeefbff868, 0x22, 0xc0000880c0, 0xc0000ae290)        /Users/kshitij/go/src/github.com/pingService/healthCheck.go:19 +0xe2created by main.main        /Users/kshitij/go/src/github.com/pingService/healthCheck.go:33 +0x136exit status 2
查看完整描述

2 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

你必須resp.Body靠近MakeRequests。


導致錯誤的另一個問題是您由于錯誤而嘗試訪問 StatusCodewhen is nil。resp


這是固定的代碼。


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()

}


查看完整回答
反對 回復 2023-08-07
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

您可以使用以下庫:


Requests:一個 Go 庫,用于減少發出 HTTP 請求時的麻煩(20k/s 請求)


https://github.com/alessiosavi/Requests


這個想法是分配一個請求列表,然后使用可配置的“并行”因子發送它們,該因子允許一次僅運行“N”個請求。


// 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)

    }

}

此時,我們有一個包含必須發送的請求的列表。讓我們并行發送它們吧!


// 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))

}

您可以在存儲庫的示例文件夾中找到示例用法。


查看完整回答
反對 回復 2023-08-07
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號