这是我第一个使用并发性的程序,所以我可能会遗漏一些相当简单的东西。
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)
}
这个程序第一次完美运行。但在此之后,它继续在随机文件名上提供net/http:TLS Handshake timeout
。我不确定是什么原因造成的。什么是可能的解决方案?这个错误究竟是什么?
编辑2:为了解决并发中的赛车问题,我使用了频道,但现在我的节目与以前相比非常慢。我更新的主要功能:
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))
}
请建议我采用与以前相同的速度,但也避免争分夺秒。
编辑1:我添加了完整的程序。没有办法在堆栈溢出上描述行号。我只在main函数中使用了并发。如果您觉得有必要在编写程序方面给我建议,请这样做,我是初学者,我很乐意做正确的事。答案 0 :(得分:1)
当来自客户端的大量http.Get()请求时,会发生net/http
。避免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/")
找到它here