When running the following http client to stress test a web server, got strange error, wonder what was the cause. The version of go is go1.8.1 linux/amd64
running on Ubuntu 14.04 with 16GB of ram.
$ go run te2.go
563.904492ms
Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address
Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address
Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x5e3b27]
goroutine 140284 [running]:
main.httpGet(0x0)
/home/jon/learn/go/te2.go:27 +0x107
created by main.main
/home/jon/learn/go/te2.go:45 +0xd3
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x5e3b27]
goroutine 140375 [running]:
main.httpGet(0x0)
/home/jon/learn/go/te2.go:27 +0x107
created by main.main
/home/jon/learn/go/te2.go:45 +0xd3
exit status 2
any ideas?
Here is the code snippet:
package main
import "fmt"
import "bufio"
import "os"
import "time"
import "net/http"
var req = []byte("GET /small HTTP/1.1
" +
"Host: localhost
" +
"Content-Length: 0
");
var buf = make([]byte, 1024)
var total = 0;
var t0 = time.Now()
var c = make(chan int)
func httpGet () int {
req, err := http.NewRequest("GET", "http://10.3.0.6/small", nil)
//req.Header.Add("User-Agent", `MYCLIENT`)
//req.Header.Add("Cookie", `sessid=12345`)
client := &http.Client{}
resp, err := client.Do(req)
//defer resp.Body.Close()
if (err != nil) {
fmt.Println(err)
}
resp.Body.Close()
total ++
if (total == 10000) {
fmt.Println(time.Now().Sub(t0))
}
c <- 1
return 0;
}
func main() {
i := 1
t0 = time.Now()
for (i < 1000) {
go httpGet()
i += 1
}
for (1 < 2) {
<-c
go httpGet()
}
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('
')
fmt.Println(text)
}