This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ghacache.go
259 lines (199 loc) · 6.72 KB
/
ghacache.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
package ghacache
import (
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/internal/http_cache/ghacache/uploadable"
"github.com/go-chi/render"
"github.com/puzpuzpuz/xsync/v3"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
)
const (
APIMountPoint = "/_apis/artifactcache"
// JavaScript's Number is limited to 2^53-1[1]
//
// [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
jsNumberMaxSafeInteger = 9007199254740991
)
type GHACache struct {
cacheHost string
mux *http.ServeMux
uploadables *xsync.MapOf[int64, *uploadable.Uploadable]
}
func New(cacheHost string) *GHACache {
cache := &GHACache{
cacheHost: cacheHost,
mux: http.NewServeMux(),
uploadables: xsync.NewMapOf[int64, *uploadable.Uploadable](),
}
cache.mux.HandleFunc("GET /cache", cache.get)
cache.mux.HandleFunc("POST /caches", cache.reserveUploadable)
cache.mux.HandleFunc("PATCH /caches/{id}", cache.updateUploadable)
cache.mux.HandleFunc("POST /caches/{id}", cache.commitUploadable)
return cache
}
func (cache *GHACache) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
cache.mux.ServeHTTP(writer, request)
}
func (cache *GHACache) get(writer http.ResponseWriter, request *http.Request) {
keys := strings.Split(request.URL.Query().Get("keys"), ",")
version := request.URL.Query().Get("version")
for _, key := range keys {
httpCacheURL := cache.httpCacheURL(key, version)
resp, err := http.Head(httpCacheURL)
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"retrieve %q: %v", httpCacheURL, err)
return
}
if resp.StatusCode == http.StatusOK {
jsonResp := struct {
Key string `json:"cacheKey"`
URL string `json:"archiveLocation"`
}{
Key: key,
URL: httpCacheURL,
}
render.JSON(writer, request, &jsonResp)
return
}
}
writer.WriteHeader(http.StatusNoContent)
}
func (cache *GHACache) reserveUploadable(writer http.ResponseWriter, request *http.Request) {
var jsonReq struct {
Key string `json:"key"`
Version string `json:"version"`
}
if err := render.DecodeJSON(request.Body, &jsonReq); err != nil {
fail(writer, request, http.StatusBadRequest, "GHA cache failed to read/decode the "+
"JSON passed to the reserve uploadable endpoint: %v", err)
return
}
jsonResp := struct {
CacheID int64 `json:"cacheId"`
}{
CacheID: rand.Int63n(jsNumberMaxSafeInteger),
}
uploadable, err := uploadable.New(jsonReq.Key, jsonReq.Version)
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed instantiate "+
"an uploadable: %v", err)
return
}
cache.uploadables.Store(jsonResp.CacheID, uploadable)
render.JSON(writer, request, &jsonResp)
}
func (cache *GHACache) updateUploadable(writer http.ResponseWriter, request *http.Request) {
id, ok := getID(request)
if !ok {
fail(writer, request, http.StatusBadRequest, "GHA cache failed to get/decode the "+
"ID passed to the update uploadable endpoint")
return
}
uploadable, ok := cache.uploadables.Load(id)
if !ok {
fail(writer, request, http.StatusNotFound, "GHA cache failed to find an uploadable "+
"with ID %d", id)
return
}
bodyBytes, err := io.ReadAll(request.Body)
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to read a "+
"chunk from the user for the uploadable %d: %v", id, err)
return
}
if err := uploadable.WriteChunk(request.Header.Get("Content-Range"), bodyBytes); err != nil {
fail(writer, request, http.StatusBadRequest, "GHA cache failed to write a chunk to "+
"the uploadable %d: %v", id, err)
return
}
writer.WriteHeader(http.StatusOK)
}
func (cache *GHACache) commitUploadable(writer http.ResponseWriter, request *http.Request) {
id, ok := getID(request)
if !ok {
fail(writer, request, http.StatusBadRequest, "GHA cache failed to get/decode the "+
"ID passed to the commit uploadable endpoint")
return
}
uploadable, ok := cache.uploadables.Load(id)
if !ok {
fail(writer, request, http.StatusNotFound, "GHA cache failed to find an uploadable "+
"with ID %d", id)
return
}
defer cache.uploadables.Delete(id)
var jsonReq struct {
Size int64 `json:"size"`
}
if err := render.DecodeJSON(request.Body, &jsonReq); err != nil {
fail(writer, request, http.StatusBadRequest, "GHA cache failed to read/decode "+
"the JSON passed to the commit uploadable endpoint: %v", err)
return
}
finalizedUploadableReader, finalizedUploadableSize, err := uploadable.Finalize()
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"finalize uploadable %d: %v", id, err)
return
}
if jsonReq.Size != finalizedUploadableSize {
fail(writer, request, http.StatusBadRequest, "GHA cache detected a cache entry "+
"size mismatch for uploadable %d: expected %d bytes, got %d bytes",
id, finalizedUploadableSize, jsonReq.Size)
return
}
req, err := http.NewRequestWithContext(request.Context(), http.MethodPost, cache.httpCacheURL(uploadable.Key,
uploadable.Version), finalizedUploadableReader)
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"upload the uploadable with ID %d: %v", id, err)
return
}
// Explicitly set the Content-Length header,
// otherwise HTTP 411 Length Required from
// the upstream S3-compatible server
req.ContentLength = finalizedUploadableSize
// Set the Content-Type header to indicate
// that we're sending arbitrary binary data
req.Header.Set("Content-Type", "application/octet-stream")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"upload the uploadable with ID %d: %v", id, err)
return
}
if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated) {
fail(writer, request, http.StatusInternalServerError, "GHA cache failed to "+
"upload the uploadable with ID %d: got HTTP %d", id, resp.StatusCode)
return
}
writer.WriteHeader(http.StatusCreated)
}
func (cache *GHACache) httpCacheURL(key string, version string) string {
return fmt.Sprintf("http://%s/%s-%s", cache.cacheHost, url.PathEscape(key), url.PathEscape(version))
}
func getID(request *http.Request) (int64, bool) {
idRaw := request.PathValue("id")
id, err := strconv.ParseInt(idRaw, 10, 64)
if err != nil {
return 0, false
}
return id, true
}
func fail(writer http.ResponseWriter, request *http.Request, status int, format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
log.Println(message)
writer.WriteHeader(status)
jsonResp := struct {
Message string `json:"message"`
}{
Message: message,
}
render.JSON(writer, request, &jsonResp)
}