Skip to content

Commit

Permalink
Minor improvements to the service goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
waybackarchiver committed Jul 19, 2022
1 parent 119d639 commit f7c1d75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ const (
MsgWaybackTimeout = "wayback timeout, please try later."
)

type doFunc func(cols []wayback.Collect, rdx reduxer.Reduxer) error

// Wayback in a separate goroutine.
func Wayback(ctx context.Context, urls []*url.URL, do func(cols []wayback.Collect, rdx reduxer.Reduxer) error) error {
func Wayback(ctx context.Context, urls []*url.URL, do doFunc) error {
var done = make(chan error, 1)
var cols []wayback.Collect
var rdx reduxer.Reduxer
Expand All @@ -31,19 +33,23 @@ func Wayback(ctx context.Context, urls []*url.URL, do func(cols []wayback.Collec
done <- errors.Wrap(err, "wayback failed")
return
}
defer rdx.Flush()
// push collects to the Meilisearch
if meili != nil {
meili.push(cols)
}
done <- do(cols, rdx)
done <- nil
}()

select {
case <-ctx.Done():
return ctx.Err()
case err := <-done:
close(done)
return err
if err != nil {
return err
}
defer rdx.Flush()

// push collects to the Meilisearch
if meili != nil {
meili.push(cols)
}
return do(cols, rdx)
}
}
2 changes: 1 addition & 1 deletion service/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestWayback(t *testing.T) {
logger.SetLogLevel(logger.LevelFatal)

u, _ := url.Parse("https://example.com/")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
defer helper.CheckContext(ctx, t)

Expand Down

0 comments on commit f7c1d75

Please sign in to comment.