Skip to content

feat: add HTTP caching for PAC fetch using httpcache (RFC 9111 compli… - #171

Open
Sneha41sb wants to merge 3 commits into
samuong:masterfrom
Sneha41sb:feat/pac-cache
Open

feat: add HTTP caching for PAC fetch using httpcache (RFC 9111 compli…#171
Sneha41sb wants to merge 3 commits into
samuong:masterfrom
Sneha41sb:feat/pac-cache

Conversation

@Sneha41sb

Copy link
Copy Markdown

Problem:
PAC file requests currently do not use HTTP caching.

Solution:

  • Integrated RFC 9111-compliant HTTP caching using sandrolain/httpcache
  • Preserved proxy bypass logic by wrapping base transport with Proxy: nil
  • Ensured cached PAC is not used as fallback when server is unreachable

Impact:

  • Reduces redundant network requests
  • Improves performance
  • Maintains correct connectivity behavior

@samuong samuong left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ensured cached PAC is not used as fallback when server is unreachable

It's not really clear to me how the httpcache package behaves when the server is unreachable - does it error out or does it return a response from the cache? To make sure this is behaving properly, I think we need to have a test that:

  1. sets up a test server (use a httptest.Server) to serve a pac file
  2. run the pac fetcher to download a file from it (and cache it)
  3. kill the test server
  4. rerun the pac fetcher, and check that it does the right thing

Comment thread pacfetcher.go Outdated
"time"

"github.com/sandrolain/httpcache"
"github.com/sandrolain/httpcache/diskcache"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Using a disk cache opens up a whole can of worms that we could avoid if we just stick to an in-memory cache, let's stick with in-memory?

Also, I see a lot of files in the cache directory, do we need to check them in to git, or are they just a result of this disk cache getting accidentally added?

Comment thread pacfetcher.go
Comment on lines -4 to -13
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I see a lot of comments, including this one, removed - unless you have a reason to remove them, can we keep them?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I still see a lot of comments that have been removed by this PR, that include important information about why things were done a certain way. Are you able to add them back?

Comment thread .gitignore
Comment on lines +7 to +8
cache/
cache/

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

i think we just need this once?

Suggested change
cache/
cache/
cache/

Comment thread pacfetcher.go
Comment on lines -4 to -13
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I still see a lot of comments that have been removed by this PR, that include important information about why things were done a certain way. Are you able to add them back?

Comment thread pacfetcher.go
//modified time.Time
//fetched time.Time
//expiry time.Time
//etag string

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This is good - this commented out code can stay removed, but we should maintain all the other comments that have been deleted.

Comment thread pacfetcher.go
client = &http.Client{
Timeout: 30 * time.Second,
Transport: cacheTransport,
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think it's better to leave the original client initialisation above the if-else statement, so we don't repeat the timeout twice:

client := &http.Client{Timeout: 30 * time.Second}

And then for the else branch, maybe something like this?

Suggested change
}
client.Transport := httpcache.NewTransport(httpcache.NewMemoryCache())
// The DefaultClient in net/http uses the proxy specified in the http(s)_proxy
// environment variable, which could be pointing at this instance of alpaca. When
// fetching the PAC file, we always use a client that goes directly to the server,
// rather than via a proxy.
client.Transport.Transport = &http.Transport{Proxy: nil}

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

Successfully merging this pull request may close these issues.

2 participants