diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-22 04:14:25 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-22 04:14:25 +0700 |
| commit | a559b47dc217793ca7f80121b3ea86f03e47afd3 (patch) | |
| tree | 90035fd53d46b092d3e05ac821b1cbad5b4822e1 | |
| parent | 2a4376d5ddeee82d4ef38f4953453abb43e85220 (diff) | |
| download | jarink-a559b47dc217793ca7f80121b3ea86f03e47afd3.tar.xz | |
brokenlinks: improve fetch logging and decrease timeout to 10 seconds
When fetching, print log after the fetch completed.
If success, print the URL along with HTTP status code.
If fail, print the URL along with the error.
The timeout now reduce to 10 seconds to prevent long delay when working
with broken website.
| -rw-r--r-- | brokenlinks/worker.go | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/brokenlinks/worker.go b/brokenlinks/worker.go index a286362..4a9a1d9 100644 --- a/brokenlinks/worker.go +++ b/brokenlinks/worker.go @@ -57,7 +57,7 @@ type worker struct { func newWorker(opts Options) (wrk *worker, err error) { var logp = `newWorker` var netDial = &net.Dialer{ - Timeout: 30 * time.Second, + Timeout: 10 * time.Second, KeepAlive: 30 * time.Second, } var tlsConfig = &tls.Config{ @@ -327,28 +327,31 @@ func (wrk *worker) scan(linkq jarink.Link) (resultq map[string]jarink.Link) { func (wrk *worker) fetch(linkq jarink.Link) (httpResp *http.Response, err error) { const maxRetry = 5 var retry int + var method string for retry < 5 { if linkq.Kind == int(atom.Img) { - wrk.log.Printf("fetch: HEAD %s", linkq.Url) + method = `HEAD` httpResp, err = wrk.httpc.Head(linkq.Url) } else { - wrk.log.Printf("fetch: GET %s", linkq.Url) + method = `GET` httpResp, err = wrk.httpc.Get(linkq.Url) } if err == nil { + wrk.log.Printf(`fetch: %s %s: %d`, method, linkq.Url, httpResp.StatusCode) return httpResp, nil } var errDNS *net.DNSError if !errors.As(err, &errDNS) { - return nil, err + break } if errDNS.Timeout() { retry++ - wrk.log.Printf(`fetch %s: %s (%d/%d)`, linkq.Url, err, retry, maxRetry) + wrk.log.Printf(`[ERROR] fetch %s: %s (retry %d/%d)`, linkq.Url, err, retry, maxRetry) continue } break } + wrk.log.Printf(`[ERROR] fetch: %s %s: %s`, method, linkq.Url, err) return nil, err } |
