dongying9756 2016-06-02 14:40
浏览 74
已采纳

HTTP定时请求Golang

I'm new to Go and trying to generate multiple requests to several http/ https servers to check the response time and the status of each web server.

I stored the URLs in a text file, afterwards I decided to add a ticker to my code which will keep generating these requests on each URL after a certain amount of time (the amount of time is in seconds typed next to each URL and spaced with a tab).

When I started scanning the time from the file everything got complicated and I can't manage to find my mistake. Here is my Go code:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "strconv"
    "strings"
    "sync"
    "time"
)

func get_resp_time(url string) { //Get time for each URL

    time_start := time.Now()
    fmt.Println("Start time", time_start, " URL ", url)
    resp, err := http.Get(url)
    //fmt.Printf("resp : %#v 
", resp)

    if err != nil {
        log.Printf("Error fetching: %v", err)
    }
    defer resp.Body.Close()
    fmt.Println(time.Since(time_start), url, " Status: ", resp.Status)
}

func main() {
    content, _ := ioutil.ReadFile("url_list.txt")
    lines := strings.Split(string(content), "\t")
    //fields := strings.Split(string(content), "\t")
    //fmt.Println(lines[1])
    //fmt.Println(strconv.Atoi(lines[0]))

    const workers = 25
    var nb int

    wg := new(sync.WaitGroup)
    in := make(chan string, 2*workers)

    if _, err := strconv.Atoi(lines[1]); err == nil {
        nb, err = strconv.Atoi(lines[1])
    }

    ticker := time.NewTicker(time.Second * time.Duration(nb))

    for t := range ticker.C {
        fmt.Println("Time of origin: ", time.Now())
        for i := 0; i < len(lines)-1; i++ {
            wg.Add(1)
            go func() {
                defer wg.Done()
                //for j := 0; j < len(in); j++ {
                if _, err := strconv.Atoi(lines[i]); err == nil {
                    nb, err = strconv.Atoi(lines[i])
                    //get_resp_time(url)
                } else {
                    get_resp_time(lines[i])
                }

                //}
            }()
        }
        for _, url := range lines {
            if url != "" {
                in <- url
            }
        }
        fmt.Println("Tick at ", t)
    }
    close(in)
    wg.Wait()
}

And the text file:

http://google.com   5   
http://nike.com     10  

This is the error I got:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2752]

goroutine 9 [running]:
panic(0x313180, 0xc82000a0d0)
    /usr/local/go/src/runtime/panic.go:464 +0x3e6
main.get_resp_time(0xc82006e195, 0x14)
    /Users/Elliott/Desktop/GoTutorial/url-time-response.go:26 +0x712
main.main.func1(0xc820070dc0, 0xc82006e1e0, 0xc8200a0090, 0x9, 0x9, 0xc820070da8)
    /Users/Elliott/Desktop/GoTutorial/url-time-response.go:61 +0x13f
created by main.main
    /Users/Elliott/Desktop/GoTutorial/url-time-response.go:65 +0x4a6
exit status 2

EDIT: Alright, I just changed the file to having only one duration because apparently it's pretty complicated to have multiple duration(according to my project advisor). Thanks for the help!

  • 写回答

1条回答 默认 最新

  • dougan4884 2016-06-02 15:37
    关注

    You don't appear to be parsing your file correctly. You should first split on , THEN split on \t. Also, verify your text file actually has \t and not spaces.

    You should probably iterate through your parsed values with fmt.Println to verify your parsed results are as expected.

    It should look something like this.

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

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义