duanping5306 2017-05-08 03:03
浏览 55
已采纳

在执行HTTP请求时出错

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)   
}
  • 写回答

1条回答 默认 最新

  • dpps0715 2017-05-08 03:12
    关注
    resp, err := client.Do(req)
    //defer resp.Body.Close()
    if (err != nil) {
        fmt.Println(err)
    }
    resp.Body.Close()
    

    In this fragment you're checking the err and continue execution.

    So if something happened and the err is not nil, but resp is (or the resp.Body is) - you print the error then dereference a nil pointer.

    What you should have done: if an error occurs - cleanup and return from the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿