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

Cache Swift Evolution JSON file locally on disk #5

Open
ole opened this issue May 14, 2024 · 4 comments
Open

Cache Swift Evolution JSON file locally on disk #5

ole opened this issue May 14, 2024 · 4 comments
Assignees

Comments

@ole
Copy link
Collaborator

ole commented May 14, 2024

Currently, we download the JSON file on every invocation of the workflow, which is really wasteful and slow.

@dempseyatgithub
Copy link

You may already be thinking along these lines but if you use the GitHub API endpoint at:
https://api.github.com/repos/apple/swift-evolution/branches/main

It will return the commit of the main branch. You can check it against the commit value in the cached evolution.json file and only download a new version if they do not match.

The GitHub API docs are here:
https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#get-a-branch

And something like this should work for decoding the result (excluding a lot of additional branch information) from the API:

struct GHBranch: Codable {
    let name: String
    let commit: Commit
    struct Commit: Codable {
        let sha: String
    }
}

@ole
Copy link
Collaborator Author

ole commented May 15, 2024

@dempseyatgithub That's a good idea, thanks!

@ole ole self-assigned this May 21, 2024
@ole
Copy link
Collaborator Author

ole commented May 22, 2024

I've been running a few informal tests over the past few days to check the HTTP caching behavior of the server download.switf.org that hosts the evolution.json file. It looks like the server correctly reports via the HTTP response headers whether the file has changed. Example:

$ curl --head "https://download.swift.org/swift-evolution/v1/evolution.json"
HTTP/1.1 200 OK
…
ETag: "D876AE18FBE60E698072F81210C48EBB"
Cache-Control: max-age=3600, public
Last-Modified: Wed, 22 May 2024 13:38:28 GMT
…

Specifically, it looks like we can perform a HEAD request and rely on the ETag field to determine whether we need to redownload the full file.

This strikes me as the better approach than asking a different source of truth (the GitHub API). It means we have to store the HTTP response headers separately, but that's not a big deal.

@dempseyatgithub
Copy link

Thanks for doing that investigation @ole. That seems better than what I had suggested, I will remember this technique for my own projects.

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

2 participants