blob: 4e2bc24e774d2418d2da87fcc05636d0847af0e4 [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package discovery
18
19import (
20 "net/http"
21 "path/filepath"
22
23 "github.com/gregjones/httpcache"
24 "github.com/gregjones/httpcache/diskcache"
25 "github.com/peterbourgon/diskv"
26 "k8s.io/klog"
27)
28
29type cacheRoundTripper struct {
30 rt *httpcache.Transport
31}
32
33// newCacheRoundTripper creates a roundtripper that reads the ETag on
34// response headers and send the If-None-Match header on subsequent
35// corresponding requests.
36func newCacheRoundTripper(cacheDir string, rt http.RoundTripper) http.RoundTripper {
37 d := diskv.New(diskv.Options{
38 BasePath: cacheDir,
39 TempDir: filepath.Join(cacheDir, ".diskv-temp"),
40 })
41 t := httpcache.NewTransport(diskcache.NewWithDiskv(d))
42 t.Transport = rt
43
44 return &cacheRoundTripper{rt: t}
45}
46
47func (rt *cacheRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
48 return rt.rt.RoundTrip(req)
49}
50
51func (rt *cacheRoundTripper) CancelRequest(req *http.Request) {
52 type canceler interface {
53 CancelRequest(*http.Request)
54 }
55 if cr, ok := rt.rt.Transport.(canceler); ok {
56 cr.CancelRequest(req)
57 } else {
58 klog.Errorf("CancelRequest not implemented by %T", rt.rt.Transport)
59 }
60}
61
62func (rt *cacheRoundTripper) WrappedRoundTripper() http.RoundTripper { return rt.rt.Transport }