github.com/abayer/test-infra@v0.0.5/ghproxy/ghcache/ghcache.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package ghcache implements an HTTP cache optimized for caching responses
    18  // from the GitHub API (https://api.github.com).
    19  //
    20  // Specifically, it enforces a cache policy that revalidates every cache hit
    21  // with a conditional request to upstream regardless of cache entry freshness
    22  // because conditional requests for unchanged resources don't cost any API
    23  // tokens!!! See: https://developer.github.com/v3/#conditional-requests
    24  //
    25  // It also provides request coalescing and prometheus instrumentation.
    26  package ghcache
    27  
    28  import (
    29  	"net/http"
    30  	"path"
    31  	"strings"
    32  
    33  	"github.com/gregjones/httpcache"
    34  	"github.com/gregjones/httpcache/diskcache"
    35  	"github.com/peterbourgon/diskv"
    36  	"github.com/prometheus/client_golang/prometheus"
    37  	"github.com/sirupsen/logrus"
    38  )
    39  
    40  // Cache response modes describe how ghcache fulfilled a request.
    41  const (
    42  	ModeError   = "ERROR"    // internal error handling request
    43  	ModeNoStore = "NO-STORE" // response not cacheable
    44  	ModeMiss    = "MISS"     // not in cache, request proxied and response cached.
    45  	ModeChanged = "CHANGED"  // cache value invalid: resource changed, cache updated
    46  	// The modes below are the happy cases in which the request is fulfilled for
    47  	// free (no API tokens used).
    48  	ModeCoalesced   = "COALESCED"   // coalesced request, this is a copied response
    49  	ModeRevalidated = "REVALIDATED" // cached value revalidated and returned
    50  )
    51  
    52  // cacheCounter provides the 'ghcache_responses' counter vec that is indexed
    53  // by the cache response mode.
    54  var cacheCounter = prometheus.NewCounterVec(
    55  	prometheus.CounterOpts{
    56  		Name: "ghcache_responses",
    57  		Help: "How many cache responses of each cache response mode there are.",
    58  	},
    59  	[]string{"mode"},
    60  )
    61  
    62  func init() {
    63  	prometheus.MustRegister(cacheCounter)
    64  }
    65  
    66  func cacheResponseMode(headers http.Header) string {
    67  	if strings.Contains(headers.Get("Cache-Control"), "no-store") {
    68  		return ModeNoStore
    69  	}
    70  	if strings.Contains(headers.Get("Status"), "304 Not Modified") {
    71  		return ModeRevalidated
    72  	}
    73  	if headers.Get("X-Conditional-Request") != "" {
    74  		return ModeChanged
    75  	}
    76  	return ModeMiss
    77  }
    78  
    79  // upstreamTransport changes response headers from upstream before they
    80  // reach the cache layer in order to force the caching policy we require.
    81  //
    82  // By default github responds to PR requests with:
    83  //    Cache-Control: private, max-age=60, s-maxage=60
    84  // Which means the httpcache would not consider anything stale for 60 seconds.
    85  // However, we want to always revalidate cache entries using ETags and last
    86  // modified times so this RoundTripper overrides response headers to:
    87  //    Cache-Control: no-cache
    88  // This instructs the cache to store the response, but always consider it stale.
    89  type upstreamTransport struct {
    90  	delegate http.RoundTripper
    91  }
    92  
    93  func (u upstreamTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    94  	etag := req.Header.Get("if-none-match")
    95  	// Don't modify request, just pass to delegate.
    96  	resp, err := u.delegate.RoundTrip(req)
    97  	if err != nil {
    98  		logrus.WithField("cache-key", req.URL.String()).WithError(err).Error("Error from upstream (GitHub).")
    99  		return nil, err
   100  	}
   101  
   102  	if resp.StatusCode >= 400 {
   103  		// Don't store errors. They can't be revalidated to save API tokens.
   104  		resp.Header.Set("Cache-Control", "no-store")
   105  	} else {
   106  		resp.Header.Set("Cache-Control", "no-cache")
   107  	}
   108  	if etag != "" {
   109  		resp.Header.Set("X-Conditional-Request", etag)
   110  	}
   111  	return resp, nil
   112  }
   113  
   114  // NewDiskCache creates a GitHub cache RoundTripper that is backed by a disk
   115  // cache.
   116  func NewDiskCache(delegate http.RoundTripper, cacheDir string, cacheSizeGB int) http.RoundTripper {
   117  	return NewFromCache(delegate, diskcache.NewWithDiskv(
   118  		diskv.New(diskv.Options{
   119  			BasePath:     path.Join(cacheDir, "data"),
   120  			TempDir:      path.Join(cacheDir, "temp"),
   121  			CacheSizeMax: uint64(cacheSizeGB) * uint64(1000000000), // convert G to B
   122  		}),
   123  	))
   124  }
   125  
   126  // NewMemCache creates a GitHub cache RoundTripper that is backed by a memory
   127  // cache.
   128  func NewMemCache(delegate http.RoundTripper) http.RoundTripper {
   129  	return NewFromCache(delegate, httpcache.NewMemoryCache())
   130  }
   131  
   132  // NewFromCache creates a GitHub cache RoundTripper that is backed by the
   133  // specified httpcache.Cache implementation.
   134  func NewFromCache(delegate http.RoundTripper, cache httpcache.Cache) http.RoundTripper {
   135  	cacheTransport := httpcache.NewTransport(cache)
   136  	cacheTransport.Transport = upstreamTransport{delegate: delegate}
   137  	return &requestCoalescer{
   138  		keys:     make(map[string]*responseWaiter),
   139  		delegate: cacheTransport,
   140  	}
   141  }