From 5e41802ce4aa44f047502648141adcfc06738968 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Thu, 19 Sep 2019 11:25:53 -0500 Subject: [PATCH] Improve timeout logic. --- go.mod | 2 ++ sites/fa.go | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 56f9604..692ef75 100644 --- a/go.mod +++ b/go.mod @@ -42,3 +42,5 @@ require ( huefox.com/syfaro/go-e621 v1.0.0 mvdan.cc/xurls v1.1.1-0.20180901190342-70405f5eab51 ) + +go 1.13 diff --git a/sites/fa.go b/sites/fa.go index 0316ff9..f7bb53e 100644 --- a/sites/fa.go +++ b/sites/fa.go @@ -8,10 +8,11 @@ import ( "os/exec" "strconv" "strings" + "time" "github.com/PuerkitoBio/goquery" "github.com/Syfaro/finch" - "github.com/go-telegram-bot-api/telegram-bot-api/v5" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "huefox.com/syfaro/telegram-furryimgbot/logger" ) @@ -27,6 +28,7 @@ type imageURLResult struct { type fa struct { cookies []*http.Cookie fautilKey string + client *http.Client } func (fa) Name() string { @@ -56,12 +58,24 @@ type cfscrape struct { UserAgent string `json:"user_agent"` } +func (f *fa) initClient() { + if f.client != nil { + return + } + + timeout := time.Duration(4 * time.Second) + client := &http.Client{ + Timeout: timeout, + } + f.client = client +} + func (f fa) handleDirectLink(postURL string) ([]PostInfo, error) { logger.Log.Debug("Attempting to load FA direct link") post := strings.TrimPrefix(postURL, "https://") post = strings.TrimPrefix(post, "http://") - post = "%" + post + post = "https://" + post logger.Log.Debugf("Converted post link is %s", post) @@ -74,16 +88,20 @@ func (f fa) handleDirectLink(postURL string) ([]PostInfo, error) { req, _ := http.NewRequest("GET", u.String(), nil) req.Header.Set("X-Api-Key", f.fautilKey) - resp, err := http.DefaultClient.Do(req) + f.initClient() + + resp, err := f.client.Do(req) if err != nil { - return nil, nil + logger.Log.Warnf("Error loading, falling back to direct loading") + d := direct{} + return d.GetImageURLs(postURL, tgbotapi.User{}) } defer resp.Body.Close() var results []imageURLResult d := json.NewDecoder(resp.Body) if err = d.Decode(&results); err != nil { - logger.Log.Debug("Unable to decode results") + logger.Log.Warnf("Unable to decode results") return nil, err }