Skip to content

cli: use cached index with lib search (#1624) #1789

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

Closed
wants to merge 3 commits into from

Conversation

ardnew
Copy link
Contributor

@ardnew ardnew commented Jul 1, 2022

Please check if the PR fulfills these requirements

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • UPGRADING.md has been updated with a migration guide (for breaking changes)
  • What kind of change does this PR introduce?
    Feature
  • What is the current behavior?
    A new copy of library_index.json is downloaded every time lib search is called (unconditionally).
  • What is the new behavior?
    A new copy of library_index.json is downloaded every time lib search is called and either one of the following conditions exists:
    1. The index has never been downloaded
    2. The time since the last index update is greater than one hour

See how to contribute

Copy link
Member

@cmaglie cmaglie left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.
I see that you have taken the same patch for the core search and that's great!

I've made some comments to simplify this patch, IMHO after we validate these changes we can "backport" some of the suggestions to the core search command too.

Finally, the integration tests are failing, some of the tests expect the Upgrading library_index... message from the CLI, but this is no more happening after the patch. Could you fix the tests too? https://github.com/arduino/arduino-cli/runs/7155610048?check_suite_focus=true#step:10:6806

Comment on lines +230 to +236
// The behavior of now.After(T) is confusing if T < 0 and MTIME in the future,
// and is probably not what the user intended. Disallow negative T and inform
// the user that positive thresholds are expected.
if modTimeThreshold < 0 {
feedback.Error(tr("Timeout must be non-negative: %dns (%s)", modTimeThreshold, duration))
os.Exit(errorcodes.ErrBadArgument)
}
Copy link
Member

Choose a reason for hiding this comment

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

I would not bother too much with validating the timeout since it's a hardcoded constant.

Suggested change
// The behavior of now.After(T) is confusing if T < 0 and MTIME in the future,
// and is probably not what the user intended. Disallow negative T and inform
// the user that positive thresholds are expected.
if modTimeThreshold < 0 {
feedback.Error(tr("Timeout must be non-negative: %dns (%s)", modTimeThreshold, duration))
os.Exit(errorcodes.ErrBadArgument)
}

Comment on lines +208 to +210
// Valid duration units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// Use a duration of 0 to always update the index.
func indexNeedsUpdating(duration string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to change the indexUpdateInterval constant to a time.Duration like:

const indexUpdateInterval = 60 * time.Minute

so the indexNeedsUpdating function may take directly a time.Duration

Suggested change
// Valid duration units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// Use a duration of 0 to always update the index.
func indexNeedsUpdating(duration string) bool {
func indexNeedsUpdating(timeout time.Duration) bool {

feedback.Error(tr("Timeout must be non-negative: %dns (%s)", modTimeThreshold, duration))
os.Exit(errorcodes.ErrBadArgument)
}
return modTimeThreshold == 0 || now.After(info.ModTime().Add(modTimeThreshold))
Copy link
Member

Choose a reason for hiding this comment

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

Also this one can be much more simplified, something like

return time.Since(info.ModTime()) > timeout

should work.

@cmaglie
Copy link
Member

cmaglie commented Jul 21, 2022

I think that just removing these two lines

assert "Downloading index: library_index.json.gz downloaded" in lines
assert "Downloading index signature: library_index.json.sig downloaded" in lines
should be enough to fix the integration tests.

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid excessive libraries index downloads on lib search
3 participants