我正在尝试访问任何示例 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 address
panic: 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 +0xe2
created by main.main
/Users/kshitij/go/src/github.com/pingService/healthCheck.go:33 +0x136
exit status 2
犯罪嫌疑人X
倚天杖
相关分类