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 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥20 白日门传奇少一个启动区服和启动服务器的快捷键,东西都是全的 , 他们说套一个出来就行了 但我就是弄不好,谁看看,
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计