implement a short timeout for some get operations
Ted Unangst tedu@tedunangst.com
Thu, 11 Jul 2019 01:20:53 -0400
1 files changed,
19 insertions(+),
2 deletions(-)
jump to
M
activity.go
→
activity.go
@@ -18,6 +18,7 @@
import ( "bytes" "compress/gzip" + "context" "crypto/rsa" "database/sql" "fmt"@@ -100,6 +101,14 @@ return gz.r.Close()
} func GetJunk(url string) (junk.Junk, error) { + return GetJunkTimeout(url, 0) +} + +func GetJunkFast(url string) (junk.Junk, error) { + return GetJunkTimeout(url, 5*time.Second) +} + +func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) { client := http.DefaultClient req, err := http.NewRequest("GET", url, nil) if err != nil {@@ -112,9 +121,17 @@ }
req.Header.Set("Accept", at) req.Header.Set("Accept-Encoding", "gzip") req.Header.Set("User-Agent", "honksnonk/5.0") + if timeout > 0 { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + req = req.WithContext(ctx) + } resp, err := client.Do(req) if err != nil { log.Printf("first get failed: %s", err) + if timeout > 0 { + return nil, err + } resp, err = client.Do(req) if err != nil { return nil, err@@ -950,7 +967,7 @@ handlock.Unlock()
return href } log.Printf("fishing for %s", name) - j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name)) + j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name)) if err != nil { log.Printf("failed to go fish %s: %s", name, err) handlock.Lock()@@ -1002,7 +1019,7 @@ }
if name == "" { return "" } - obj, err := GetJunk(name) + obj, err := GetJunkFast(name) if err != nil { log.Printf("error investigating honker: %s", err) return ""