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

Unsure if golang::last_version() caching will be a problem #22

Open
danielparks opened this issue Oct 1, 2022 · 1 comment
Open

Unsure if golang::last_version() caching will be a problem #22

danielparks opened this issue Oct 1, 2022 · 1 comment

Comments

@danielparks
Copy link
Owner

Shortly I’ll be adding golang::last_version() to get the latest stable version of Go from https://go.dev/dl/?mode=json.

I’m concerned about the potential to hit https://go.dev with too many requests (though it seems unlikely to me that this module will ever be used enough to cause a problem). So, I’m considering adding simple memoization to the function:

Puppet::Functions.create_function(:"golang::latest_version") do
  # ...
  def latest_version(url)
    @@golang_latest_version_cache ||= {}
    @@golang_latest_version_cache[url] ||= load_latest_version(url)
  end
  # ...
end

I imagine this will last longer than the catalog compilation, which seems like it might be an issue — if it lives as long as the Puppet server, then it could last quite a long time.

  • I could add expiration to the cache.
  • I’m not sure if there are other downsides I’m not seeing, or better ways to do this.
  • Maybe this isn’t necessary at all?
@danielparks
Copy link
Owner Author

  def latest_version(url)
    time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    @@golang_latest_version_cache ||= {} # rubocop:disable Style/ClassVars

    if !@@golang_latest_version_cache[url] \
        || time - @@golang_latest_version_cache[url][:time] >= 60
      @@golang_latest_version_cache[url] = {
        time: time,
        version: load_latest_version(url),
      }
    end

    @@golang_latest_version_cache[url][:version]
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant