-
Notifications
You must be signed in to change notification settings - Fork 5
/
caching_handler.go
233 lines (178 loc) · 6.08 KB
/
caching_handler.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
package gbox
import (
"bytes"
"errors"
"fmt"
"mime"
"net/http"
"strings"
"time"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/jensneuse/graphql-go-tools/pkg/operationreport"
"go.uber.org/zap"
)
var ErrHandleUnknownOperationTypeError = errors.New("unknown operation type")
// HandleRequest caching GraphQL query result by configured rules and varies.
func (c *Caching) HandleRequest(w http.ResponseWriter, r *cachingRequest, h caddyhttp.HandlerFunc) error {
// Remove `accept-encoding` header to prevent response body encoded when forward request to upstream
// encode directive had read this header, safe to delete it.
r.httpRequest.Header.Del("accept-encoding")
operationType, _ := r.gqlRequest.OperationType()
// nolint:exhaustive
switch operationType {
case graphql.OperationTypeQuery:
return c.handleQueryRequest(w, r, h)
case graphql.OperationTypeMutation:
return c.handleMutationRequest(w, r, h)
}
return ErrHandleUnknownOperationTypeError
}
func (c *Caching) handleQueryRequest(w http.ResponseWriter, r *cachingRequest, h caddyhttp.HandlerFunc) (err error) {
var plan *cachingPlan
report := &operationreport.Report{}
plan, err = c.getCachingPlan(r)
if err != nil {
report.AddInternalError(err)
return report
}
status, result := c.resolvePlan(r, plan)
defer c.addMetricsCaching(r.gqlRequest, status)
switch status {
case CachingStatusMiss:
bodyBuff := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(bodyBuff)
bodyBuff.Reset()
crw := newCachingResponseWriter(bodyBuff)
if err = h(crw, r.httpRequest); err != nil {
return err
}
defer func() {
c.addCachingResponseHeaders(status, result, plan, w.Header())
err = crw.WriteResponse(w)
}()
shouldCache := false
mt, _, _ := mime.ParseMediaType(crw.header.Get("content-type"))
if crw.Status() == http.StatusOK && mt == "application/json" {
// respect no-store directive
// https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.1.5
shouldCache = r.cacheControl == nil || !r.cacheControl.NoStore
}
if !shouldCache {
return err
}
err = c.cachingQueryResult(r.httpRequest.Context(), r, plan, crw.buffer.Bytes(), crw.Header().Clone())
if err == nil {
c.logger.Info("caching query result successful", zap.String("cache_key", plan.queryResultCacheKey))
}
case CachingStatusHit:
for header, values := range result.Header {
w.Header()[header] = values
}
c.addCachingResponseHeaders(status, result, plan, w.Header())
w.WriteHeader(http.StatusOK)
_, err = w.Write(result.Body)
if err != nil || result.Status() != CachingQueryResultStale {
return err
}
r.httpRequest = prepareSwrHTTPRequest(c.ctxBackground, r.httpRequest, w)
go func() {
if err := c.swrQueryResult(c.ctxBackground, result, r, h); err != nil {
c.logger.Error("swr failed, can not update query result", zap.String("cache_key", plan.queryResultCacheKey), zap.Error(err))
} else {
c.logger.Info("swr query result successful", zap.String("cache_key", plan.queryResultCacheKey))
}
}()
case CachingStatusPass:
c.addCachingResponseHeaders(status, result, plan, w.Header())
err = h(w, r.httpRequest)
}
return err
}
func (c *Caching) resolvePlan(r *cachingRequest, p *cachingPlan) (CachingStatus, *cachingQueryResult) {
if p.Passthrough {
return CachingStatusPass, nil
}
result, _ := c.getCachingQueryResult(r.httpRequest.Context(), p)
if result != nil && (r.cacheControl == nil || result.ValidFor(r.cacheControl)) {
err := c.increaseQueryResultHitTimes(r.httpRequest.Context(), result)
if err != nil {
c.logger.Error("increase query result hit times failed", zap.String("cache_key", p.queryResultCacheKey), zap.Error(err))
}
return CachingStatusHit, result
}
return CachingStatusMiss, nil
}
func (c *Caching) addCachingResponseHeaders(s CachingStatus, r *cachingQueryResult, p *cachingPlan, h http.Header) {
h.Set("x-cache", string(s))
if s == CachingStatusPass {
return
}
for _, name := range p.VaryNames {
for _, v := range c.Varies[name].Headers {
h.Add("vary", v)
}
for _, v := range c.Varies[name].Cookies {
h.Add("vary", fmt.Sprintf("cookie:%s", v))
}
}
if s == CachingStatusHit {
age := int64(r.Age().Seconds())
maxAge := int64(time.Duration(r.MaxAge).Seconds())
cacheControl := []string{"public", fmt.Sprintf("s-maxage=%d", maxAge)}
if r.Swr > 0 {
swr := int64(time.Duration(r.Swr).Seconds())
cacheControl = append(cacheControl, fmt.Sprintf("stale-while-revalidate=%d", swr))
}
h.Set("age", fmt.Sprintf("%d", age))
h.Set("cache-control", strings.Join(cacheControl, "; "))
h.Set("x-cache-hits", fmt.Sprintf("%d", r.HitTime))
}
if c.DebugHeaders {
h.Set("x-debug-result-cache-key", p.queryResultCacheKey)
if r == nil {
return
}
if len(r.Tags.TypeKeys()) == 0 {
h.Set("x-debug-result-missing-type-keys", "")
}
h.Set("x-debug-result-tags", strings.Join(r.Tags.ToSlice(), ", "))
}
}
func (c *Caching) handleMutationRequest(w http.ResponseWriter, r *cachingRequest, h caddyhttp.HandlerFunc) (err error) {
if !c.AutoInvalidate {
return h(w, r.httpRequest)
}
bodyBuff := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(bodyBuff)
bodyBuff.Reset()
crw := newCachingResponseWriter(bodyBuff)
err = h(crw, r.httpRequest)
if err != nil {
return err
}
defer func() {
err = crw.WriteResponse(w)
}()
mt, _, _ := mime.ParseMediaType(crw.Header().Get("content-type"))
if crw.Status() != http.StatusOK || mt != "application/json" {
return err
}
foundTags := make(cachingTags)
tagAnalyzer := newCachingTagAnalyzer(r, c.TypeKeys)
if aErr := tagAnalyzer.AnalyzeResult(crw.buffer.Bytes(), nil, foundTags); aErr != nil {
c.logger.Info("fail to analyze result tags", zap.Error(aErr))
return err
}
purgeTags := foundTags.TypeKeys().ToSlice()
if len(purgeTags) == 0 {
return err
}
if c.DebugHeaders {
w.Header().Set("x-debug-purged-tags", strings.Join(purgeTags, "; "))
}
if err = c.purgeQueryResultByTags(c.ctxBackground, purgeTags); err != nil {
c.logger.Error("fail to purge query result by tags", zap.Error(err))
}
return err
}