Skip to content

Commit

Permalink
chore(pkg/find/handlers.go): improve log and documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Massimiliano Giovagnoli <[email protected]>
  • Loading branch information
maxgio92 committed Aug 23, 2023
1 parent b7259bf commit 925bd3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions cmd/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ func (o *Command) Run(_ *cobra.Command, args []string) error {
return err
}

// Network client dialer.
dialer := network.NewDialer(
network.WithTimeout(o.ConnectionTimeout),
network.WithKeepAlive(o.KeepAliveInterval),
)

// HTTP client transport.
transport := network.NewTransport(
network.WithDialer(dialer),
network.WithIdleConnsTimeout(o.IdleConnTimeout),
Expand All @@ -122,6 +124,7 @@ func (o *Command) Run(_ *cobra.Command, args []string) error {
network.WithMaxIdleConnsPerHost(o.ConnPoolPerHostSize),
)

// Wfind finder.
finder := find.NewFind(
find.WithSeedURLs(o.SeedURLs),
find.WithFilenameRegexp(o.FilenameRegexp),
Expand Down
17 changes: 12 additions & 5 deletions pkg/find/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ import (
// It accepts a colly.Response and the error.
func (o *Options) handleError(response *colly.Response, err error) {
switch {
// Context timed out.
case errors.Is(err, context.DeadlineExceeded):
log.Println(err, "context deadline has exceeded")
// Request has timed out.
case os.IsTimeout(err):
log.Println(err, "connection has timed out")
if o.TimeoutRetryBackOff != nil {
log.Println(err, "Will backoff...")
retryWithExponentialBackoff(response.Request.Retry, o.TimeoutRetryBackOff)
}
// Connection has been reset (RST) by the peer.
case errors.Is(err, unix.ECONNRESET):
log.Println(err, "connection has been reset by peer")
if o.ConnResetRetryBackOff != nil {
log.Println(err, "Will backoff...")
retryWithExponentialBackoff(response.Request.Retry, o.ConnResetRetryBackOff)
}
// Other failures.
default:
log.Printf("error: %v\n", err)
}
}

// retryWithExtponentialBackoff retries with an exponential backoff a function.
// Exponential backoff can be tuned with options accepted as arguments to the function.
func retryWithExponentialBackoff(retryF func() error, opts *ExponentialBackOffOptions) {
ticker := backoff.NewTicker(
utils.NewExponentialBackOff(
Expand All @@ -52,20 +58,21 @@ func retryWithExponentialBackoff(retryF func() error, opts *ExponentialBackOffOp
// so operations that take a while to fail could run in quick succession.
for range ticker.C {
if err = retryF(); err != nil {
log.Println(err, "will backoff...")
// Retry.
log.Println(err, "will retry...")
continue
}

ticker.Stop()
log.Println("OK")
log.Println("retried with success")
break
}

if err != nil {
// Operation has failed.
log.Println(err, "Failed")
// Retry has failed.
log.Println(err, "retry limit exhausted")
return
}

// Operation is successful.
// Retry is successful.
}

0 comments on commit 925bd3f

Please sign in to comment.