doujingao6210 2017-01-18 12:45
浏览 73
已采纳

从api同时请求数据时TLS握手超时

This is my first program in which I have used concurrency so I might be missing something fairly simple.

package main

import(
    "net/http"
    "net/url"
    "log"
    "flag"
    "io/ioutil"
    "fmt"
    "golang.org/x/net/html"
    "strings"
    "encoding/json"
    "os"
    "html/template"
)

type fileInfo struct{
    Title string `json:"Title"`;
    Year string `json:"Year"`;
    Runtime string `json:"Runtime"`
    Genre string `json:"Genre"` 
    Rating string `json:"imdbRating"`;
    Description string `json:"Plot"`;
    Image string `json:"Poster"`;
    Awards string `json:"Awards"`;
}

var movie struct{
    Name string;
    Year string;
}
var Movies []fileInfo
func main() {
    flag.Parse()
    files, _ := ioutil.ReadDir(flag.Args()[0])
    var queryNames []string
    for _, f := range files {
        go func(){
            queryNames= append(queryNames,url.QueryEscape(f.Name()))
        }()
    }
    //fmt.Println(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/template/index.tpl")
    fmt.Println("Preparing data")


    for _, f := range queryNames {
        go GetTitleAndYear("https://opensubtitles.co/search?q=" + f)
    }




    fmt.Println("Done")
    http.HandleFunc("/",ShowRatings)
    http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/static"))))

    log.Fatal(http.ListenAndServe(":8080",nil))
}

func ShowRatings(w http.ResponseWriter,r *http.Request){
    t,err := template.ParseFiles(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/template/index.tpl")
    if(err!=nil){
        log.Fatal(err)
    }
    t.Execute(w,Movies)
}

func GetTitleAndYear(url string){
    resp,err := http.Get(url)
    if err!=nil{
        log.Fatal(err)
    }
    var movieData string
    if resp.StatusCode != 200 {
        fmt.Println("statuscode",err)
    }
    z := html.NewTokenizer(resp.Body)
    for{
        tt := z.Next()

        if tt == html.ErrorToken{
            return
        }else if tt==html.StartTagToken{
            t:= z.Token()
            if t.Data=="h4"{
                tt = z.Next()
                tt = z.Next()
                tt = z.Next()
                t = z.Token()
                movieData = strings.TrimSpace(t.Data)               
                break
            }
        }
    }

    movie.Name = movieData[:len(movieData)-6]
    movie.Year = movieData[len(movieData)-5:len(movieData)-1]
    movie.Name = strings.Replace(movie.Name, " ", "+", -1)
    url = "http://www.omdbapi.com/?t=" + movie.Name + "&y=" + movie.Year + "&plot=short&r=json"  
    req,err := http.Get(url)

    if err!=nil{
        log.Fatal(err)
    }
    var x fileInfo
    jsonParser := json.NewDecoder(req.Body)
    if err := jsonParser.Decode(&x); err != nil {
        log.Fatal("parsing config file", err)
    }
    Movies = append(Movies,x)
    fmt.Println(x.Title,x.Year)     
}

This program ran perfectly for the first time. But after that it keeps on giving net/http:TLS Handshake timeout on random filenames. I am not sure what is causing this. What might be a possible solution? Also what exactly is this error?

EDIT 2: To solve the racing problem in concurrency, I used channels but now my program is very very slow compared to before. My updated main function :

func main() {
    flag.Parse()
    files, _ := ioutil.ReadDir(flag.Args()[0])
    var queryNames []string
    for _, f := range files {
        queryNames= append(queryNames,url.QueryEscape(f.Name()))
    }
    //fmt.Println(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/template/index.tpl")
    fmt.Println("Preparing data")
    ch := make(chan string)
    done := make(chan bool)
    go func(){
        for{
            name,more := <-ch
            if more{
                GetTitleAndYear("https://opensubtitles.co/search?q=" + name)
            }else{
                done <-true
            }
        }
    }()

    for i:=0;i<len(queryNames);i++{
        ch <- queryNames[i] 
    }
    <- done
    fmt.Println("Preparation DONE")
    http.HandleFunc("/",ShowRatings)
    http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir(os.Getenv("GOPATH") + "/src/github.com/krashcan/review/static"))))

    log.Fatal(http.ListenAndServe(":8080",nil))
}

Please suggest me a way to have the same speed as before but also avoid racing around problems.

EDIT 1: I have added my complete program. There is no way to depict line no.s on stack overflow , I have used concurrency only in the main function. If you feel the need to advice me on my way of writing go programs, please do so, I am a beginner and I would love to do things right.

  • 写回答

1条回答 默认 最新

  • doujiang7258 2017-01-31 11:21
    关注

    The net/http occurs when there is a large amount of http.Get() requests from client side. To avoid the TLS handshake timeout error,

    t := &http.Transport{
                Dial: (&net.Dialer{
                        Timeout: 60 * time.Second,
                        KeepAlive: 30 * time.Second,
                }).Dial,
                // We use ABSURDLY large keys, and should probably not.
                TLSHandshakeTimeout: 60 * time.Second,
        }
        c := &http.Client{
                Transport: t,
        }
        resp, err := c.Get("https://internal.lan/")
    

    Found it here

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化