forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathghcache.go
243 lines (213 loc) · 8.65 KB
/
ghcache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
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.
*/
// Package ghcache implements an HTTP cache optimized for caching responses
// from the GitHub API (https://api.github.com).
//
// Specifically, it enforces a cache policy that revalidates every cache hit
// with a conditional request to upstream regardless of cache entry freshness
// because conditional requests for unchanged resources don't cost any API
// tokens!!! See: https://developer.github.com/v3/#conditional-requests
//
// It also provides request coalescing and prometheus instrumentation.
package ghcache
import (
"context"
"crypto/sha256"
"fmt"
"net/http"
"path"
"strconv"
"strings"
"time"
"github.com/gomodule/redigo/redis"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
rediscache "github.com/gregjones/httpcache/redis"
"github.com/peterbourgon/diskv"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
"k8s.io/test-infra/ghproxy/ghmetrics"
)
type CacheResponseMode string
// Cache response modes describe how ghcache fulfilled a request.
const (
CacheModeHeader = "X-Cache-Mode"
ModeError CacheResponseMode = "ERROR" // internal error handling request
ModeNoStore CacheResponseMode = "NO-STORE" // response not cacheable
ModeMiss CacheResponseMode = "MISS" // not in cache, request proxied and response cached.
ModeChanged CacheResponseMode = "CHANGED" // cache value invalid: resource changed, cache updated
// The modes below are the happy cases in which the request is fulfilled for
// free (no API tokens used).
ModeCoalesced CacheResponseMode = "COALESCED" // coalesced request, this is a copied response
ModeRevalidated CacheResponseMode = "REVALIDATED" // cached value revalidated and returned
)
func CacheModeIsFree(mode CacheResponseMode) bool {
switch mode {
case ModeCoalesced:
return true
case ModeRevalidated:
return true
case ModeError:
// In this case we did not successfully communicate with the GH API, so no
// token is used, but we also don't return a response, so ModeError won't
// ever be returned as a value of CacheModeHeader.
return true
}
return false
}
// cacheCounter provides the 'ghcache_responses' counter vec that is indexed
// by the cache response mode.
var cacheCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ghcache_responses",
Help: "How many cache responses of each cache response mode there are.",
},
[]string{"mode"},
)
// outboundConcurrencyGauge provides the 'concurrent_outbound_requests' gauge that
// is global to the proxy.
var outboundConcurrencyGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "concurrent_outbound_requests",
Help: "How many concurrent requests are in flight to GitHub servers.",
})
// pendingOutboundConnectionsGauge provides the 'pending_outbound_requests' gauge that
// is global to the proxy.
var pendingOutboundConnectionsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "pending_outbound_requests",
Help: "How many pending requests are waiting to be sent to GitHub servers.",
})
func init() {
prometheus.MustRegister(cacheCounter)
prometheus.MustRegister(outboundConcurrencyGauge)
prometheus.MustRegister(pendingOutboundConnectionsGauge)
}
func cacheResponseMode(headers http.Header) CacheResponseMode {
if strings.Contains(headers.Get("Cache-Control"), "no-store") {
return ModeNoStore
}
if strings.Contains(headers.Get("Status"), "304 Not Modified") {
return ModeRevalidated
}
if headers.Get("X-Conditional-Request") != "" {
return ModeChanged
}
return ModeMiss
}
func newThrottlingTransport(maxConcurrency int, delegate http.RoundTripper) http.RoundTripper {
return &throttlingTransport{sem: semaphore.NewWeighted(int64(maxConcurrency)), delegate: delegate}
}
// throttlingTransport throttles outbound concurrency from the proxy
type throttlingTransport struct {
sem *semaphore.Weighted
delegate http.RoundTripper
}
func (c *throttlingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
pendingOutboundConnectionsGauge.Inc()
if err := c.sem.Acquire(context.Background(), 1); err != nil {
logrus.WithField("cache-key", req.URL.String()).WithError(err).Error("Internal error acquiring semaphore.")
return nil, err
}
defer c.sem.Release(1)
pendingOutboundConnectionsGauge.Dec()
outboundConcurrencyGauge.Inc()
defer outboundConcurrencyGauge.Dec()
return c.delegate.RoundTrip(req)
}
// upstreamTransport changes response headers from upstream before they
// reach the cache layer in order to force the caching policy we require.
//
// By default github responds to PR requests with:
// Cache-Control: private, max-age=60, s-maxage=60
// Which means the httpcache would not consider anything stale for 60 seconds.
// However, we want to always revalidate cache entries using ETags and last
// modified times so this RoundTripper overrides response headers to:
// Cache-Control: no-cache
// This instructs the cache to store the response, but always consider it stale.
type upstreamTransport struct {
delegate http.RoundTripper
}
func (u upstreamTransport) RoundTrip(req *http.Request) (*http.Response, error) {
etag := req.Header.Get("if-none-match")
// get authorization header to convert to sha256
authHeader := req.Header.Get("Authorization")
if authHeader == "" {
logrus.Warn("Couldn't retrieve 'Authorization' header, adding to unknown bucket")
authHeader = "unknown"
}
hasher := sha256.New()
hasher.Write([]byte(authHeader))
authHeaderHash := fmt.Sprintf("%x", hasher.Sum(nil)) // use %x to make this a utf-8 string for use as a label
reqStartTime := time.Now()
// Don't modify request, just pass to delegate.
resp, err := u.delegate.RoundTrip(req)
if err != nil {
logrus.WithField("cache-key", req.URL.String()).WithError(err).Error("Error from upstream (GitHub).")
return nil, err
}
responseTime := time.Now()
roundTripTime := responseTime.Sub(reqStartTime)
if resp.StatusCode >= 400 {
// Don't store errors. They can't be revalidated to save API tokens.
resp.Header.Set("Cache-Control", "no-store")
} else {
resp.Header.Set("Cache-Control", "no-cache")
}
if etag != "" {
resp.Header.Set("X-Conditional-Request", etag)
}
apiVersion := "v3"
if strings.HasPrefix(req.URL.Path, "graphql") || strings.HasPrefix(req.URL.Path, "/graphql") {
resp.Header.Set("Cache-Control", "no-store")
apiVersion = "v4"
}
ghmetrics.CollectGitHubTokenMetrics(authHeaderHash, apiVersion, resp.Header, reqStartTime, responseTime)
ghmetrics.CollectGitHubRequestMetrics(authHeaderHash, req.URL.Path, strconv.Itoa(resp.StatusCode), roundTripTime.Seconds())
return resp, nil
}
// NewDiskCache creates a GitHub cache RoundTripper that is backed by a disk
// cache.
func NewDiskCache(delegate http.RoundTripper, cacheDir string, cacheSizeGB, maxConcurrency int) http.RoundTripper {
return NewFromCache(delegate, diskcache.NewWithDiskv(
diskv.New(diskv.Options{
BasePath: path.Join(cacheDir, "data"),
TempDir: path.Join(cacheDir, "temp"),
CacheSizeMax: uint64(cacheSizeGB) * uint64(1000000000), // convert G to B
})),
maxConcurrency,
)
}
// NewMemCache creates a GitHub cache RoundTripper that is backed by a memory
// cache.
func NewMemCache(delegate http.RoundTripper, maxConcurrency int) http.RoundTripper {
return NewFromCache(delegate, httpcache.NewMemoryCache(), maxConcurrency)
}
// NewFromCache creates a GitHub cache RoundTripper that is backed by the
// specified httpcache.Cache implementation.
func NewFromCache(delegate http.RoundTripper, cache httpcache.Cache, maxConcurrency int) http.RoundTripper {
cacheTransport := httpcache.NewTransport(cache)
cacheTransport.Transport = newThrottlingTransport(maxConcurrency, upstreamTransport{delegate: delegate})
return &requestCoalescer{
keys: make(map[string]*responseWaiter),
delegate: cacheTransport,
}
}
// NewRedisCache creates a GitHub cache RoundTripper that is backed by a Redis
// cache.
func NewRedisCache(delegate http.RoundTripper, redisAddress string, maxConcurrency int) http.RoundTripper {
conn, err := redis.Dial("tcp", redisAddress)
if err != nil {
logrus.WithError(err).Fatal("Error connecting to Redis")
}
return NewFromCache(delegate, rediscache.NewWithClient(conn), maxConcurrency)
}