From 3ff5e7b88f3c2838e93aeb57824c82508e1e1d66 Mon Sep 17 00:00:00 2001 From: ale Date: Tue, 19 Dec 2017 08:35:21 +0000 Subject: Simplify redirectHandler.Handle --- crawler.go | 30 ++++++++++++++++-------------- queue.go | 2 -- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crawler.go b/crawler.go index 9fad2ef..b3c4a7b 100644 --- a/crawler.go +++ b/crawler.go @@ -293,22 +293,24 @@ type redirectHandler struct { } func (wrap *redirectHandler) Handle(c *Crawler, u string, depth int, resp *http.Response, err error) error { - if err == nil { - if resp.StatusCode == 200 { - err = wrap.h.Handle(c, u, depth, resp, err) - } else if resp.StatusCode > 300 && resp.StatusCode < 400 { - location := resp.Header.Get("Location") - if location != "" { - locationURL, err := resp.Request.URL.Parse(location) - if err != nil { - log.Printf("error parsing Location header: %v", err) - } else { - c.Enqueue(Outlink{URL: locationURL, Tag: TagPrimary}, depth+1) - } + if err != nil { + return err + } + + if resp.StatusCode == 200 { + err = wrap.h.Handle(c, u, depth, resp, err) + } else if resp.StatusCode > 300 && resp.StatusCode < 400 { + location := resp.Header.Get("Location") + if location != "" { + locationURL, err := resp.Request.URL.Parse(location) + if err != nil { + log.Printf("error parsing Location header: %v", err) + } else { + c.Enqueue(Outlink{URL: locationURL, Tag: TagPrimary}, depth+1) } - } else { - err = errors.New(resp.Status) } + } else { + err = errors.New(resp.Status) } return err } diff --git a/queue.go b/queue.go index 7621226..da4a7b8 100644 --- a/queue.go +++ b/queue.go @@ -65,10 +65,8 @@ func (q *queue) Add(wb *leveldb.Batch, urlStr string, depth int, when time.Time) func (q *queue) acquire(qp queuePair) { wb := new(leveldb.Batch) - q.db.PutObjBatch(wb, activeQueueKey(qp.key), qp) wb.Delete(qp.key) - q.db.Write(wb, nil) atomic.AddInt32(&q.numActive, 1) -- cgit v1.2.3-54-g00ecf