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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算