forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghcache.go
289 lines (255 loc) · 10.6 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
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"
"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
// cacheEntryCreationDateHeader contains the creation date of the cache entry
cacheEntryCreationDateHeader = "X-PROW-REQUEST-DATE"
// TokenBudgetIdentifierHeader is used to identify the token budget for
// which metrics should be recorded if set. If unset, the sha256sum of
// the Authorization header will be used.
TokenBudgetIdentifierHeader = "X-PROW-GHCACHE-TOKEN-BUDGET-IDENTIFIER"
)
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
}
// 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.",
})
var cachePartitionsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ghcache_cache_parititions",
Help: "Which cache partitions exist",
},
[]string{"token_hash"},
)
func init() {
prometheus.MustRegister(outboundConcurrencyGauge)
prometheus.MustRegister(pendingOutboundConnectionsGauge)
prometheus.MustRegister(cachePartitionsCounter)
}
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
hasher ghmetrics.Hasher
}
func (u upstreamTransport) RoundTrip(req *http.Request) (*http.Response, error) {
etag := req.Header.Get("if-none-match")
var tokenBudgetName string
if val := req.Header.Get(TokenBudgetIdentifierHeader); val != "" {
tokenBudgetName = val
} else {
tokenBudgetName = u.hasher.Hash(req)
}
reqStartTime := time.Now()
// Don't modify request, just pass to delegate.
resp, err := u.delegate.RoundTrip(req)
if err != nil {
ghmetrics.CollectRequestTimeoutMetrics(tokenBudgetName, req.URL.Path, req.Header.Get("User-Agent"), reqStartTime, time.Now())
logrus.WithField("cache-key", req.URL.String()).WithError(err).Warn("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 resp.StatusCode != http.StatusNotModified {
// Used for metrics about the age of cached requests
resp.Header.Set(cacheEntryCreationDateHeader, strconv.Itoa(int(time.Now().Unix())))
}
}
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(tokenBudgetName, apiVersion, resp.Header, reqStartTime, responseTime)
ghmetrics.CollectGitHubRequestMetrics(tokenBudgetName, req.URL.Path, strconv.Itoa(resp.StatusCode), req.Header.Get("User-Agent"), roundTripTime.Seconds())
return resp, nil
}
const LogMessageWithDiskPartitionFields = "Not using a partitioned cache because legacyDisablePartitioningByAuthHeader is true"
// NewDiskCache creates a GitHub cache RoundTripper that is backed by a disk
// cache.
// It supports a partitioned cache.
func NewDiskCache(delegate http.RoundTripper, cacheDir string, cacheSizeGB, maxConcurrency int, legacyDisablePartitioningByAuthHeader bool) http.RoundTripper {
if legacyDisablePartitioningByAuthHeader {
diskCache := 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
}))
return NewFromCache(delegate,
func(partitionKey string) httpcache.Cache {
logrus.WithField("cache-base-path", path.Join(cacheDir, "data", partitionKey)).
WithField("cache-temp-path", path.Join(cacheDir, "temp", partitionKey)).
Warning(LogMessageWithDiskPartitionFields)
return diskCache
},
maxConcurrency,
)
}
return NewFromCache(delegate,
func(partitionKey string) httpcache.Cache {
return diskcache.NewWithDiskv(
diskv.New(diskv.Options{
BasePath: path.Join(cacheDir, "data", partitionKey),
TempDir: path.Join(cacheDir, "temp", partitionKey),
CacheSizeMax: uint64(cacheSizeGB) * uint64(1000000000), // convert G to B
}))
},
maxConcurrency,
)
}
// NewMemCache creates a GitHub cache RoundTripper that is backed by a memory
// cache.
// It supports a partitioned cache.
func NewMemCache(delegate http.RoundTripper, maxConcurrency int) http.RoundTripper {
return NewFromCache(delegate,
func(_ string) httpcache.Cache { return httpcache.NewMemoryCache() },
maxConcurrency)
}
// CachePartitionCreator creates a new cache partition using the given key
type CachePartitionCreator func(partitionKey string) httpcache.Cache
// NewFromCache creates a GitHub cache RoundTripper that is backed by the
// specified httpcache.Cache implementation.
func NewFromCache(delegate http.RoundTripper, cache CachePartitionCreator, maxConcurrency int) http.RoundTripper {
hasher := ghmetrics.NewCachingHasher()
return newPartitioningRoundTripper(func(partitionKey string) http.RoundTripper {
cacheTransport := httpcache.NewTransport(cache(partitionKey))
cacheTransport.Transport = newThrottlingTransport(maxConcurrency, upstreamTransport{delegate: delegate, hasher: hasher})
return &requestCoalescer{
keys: make(map[string]*responseWaiter),
delegate: cacheTransport,
hasher: hasher,
}
})
}
// NewRedisCache creates a GitHub cache RoundTripper that is backed by a Redis
// cache.
// Important note: The redis implementation does not support partitioning the cache
// which means that requests to the same path from different tokens will invalidate
// each other.
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")
}
redisCache := rediscache.NewWithClient(conn)
return NewFromCache(delegate,
func(_ string) httpcache.Cache { return redisCache },
maxConcurrency)
}