Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retries to link checking #40

Merged
merged 3 commits into from
Jun 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion pkg/mdformatter/linktransformer/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/bwplotka/mdox/pkg/mdformatter"
"github.com/efficientgo/tools/core/pkg/merrors"
Expand All @@ -33,6 +35,7 @@ var (

const (
originalURLKey = "originalURLKey"
retryKey = "retryKey"
)

type chain struct {
Expand Down Expand Up @@ -175,7 +178,30 @@ func NewValidator(logger log.Logger, linksValidateConfig []byte, anchorDir strin
v.c.OnError(func(response *colly.Response, err error) {
v.rMu.Lock()
defer v.rMu.Unlock()
v.remoteLinks[response.Ctx.Get(originalURLKey)] = errors.Wrapf(err, "%q not accessible; status code %v", response.Request.URL.String(), response.StatusCode)
retriesStr := response.Ctx.Get(retryKey)
retries, _ := strconv.Atoi(retriesStr)
switch response.StatusCode {
case 429:
if retries <= 2 {
response.Ctx.Put(retryKey, strconv.Itoa(retries+1))
retryAfter, convErr := strconv.Atoi(response.Headers.Get("Retry-After"))
if convErr == nil {
time.Sleep(time.Duration(retryAfter) * time.Second)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmmm I think we need to have global retry. You see that will not work if others for same host will try in the same time 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I didn't think of this before! Will try to implement global retry!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this and tested it a bit. I think we never make requests to the same host again or at the same time due to our present caching layer and retries can only occur after request! So this should work. 🙂

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I challenge this statement (:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine for now, let's iterate (global retry will be hard to implement), but I think it's needed, paths are cached not hosts

}

retryErr := response.Request.Retry()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might think we need default back of maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in default?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well default if no retry-after is set? maybe something like 1s

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay! Will try to implement this!

v.remoteLinks[response.Ctx.Get(originalURLKey)] = errors.Wrapf(retryErr, "%q rate limited even after retry; status code %v", response.Request.URL.String(), response.StatusCode)
}
case 301, 307, 503:
if retries <= 2 {
response.Ctx.Put(retryKey, strconv.Itoa(retries+1))

retryErr := response.Request.Retry()
v.remoteLinks[response.Ctx.Get(originalURLKey)] = errors.Wrapf(retryErr, "%q not accessible even after retry; status code %v", response.Request.URL.String(), response.StatusCode)
}
default:
v.remoteLinks[response.Ctx.Get(originalURLKey)] = errors.Wrapf(err, "%q not accessible; status code %v", response.Request.URL.String(), response.StatusCode)
}
})
return v, nil
}
Expand Down