-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor.go
224 lines (184 loc) · 5.55 KB
/
interceptor.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
// Package gcache provides caching interceptors for gRPC clients.
package gcache
import (
"context"
"crypto/sha1" //nolint: gosec // we use sha1 for hashing
"fmt"
"io"
"log/slog"
"reflect"
"regexp"
"slices"
"strings"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// Interceptor is a cache interceptor.
// It looks over the ETag header and caches the response ONLY if the ETag is present.
type Interceptor struct {
store Store
logger *slog.Logger
codec encoding.Codec
filter *regexp.Regexp
}
// NewInterceptor makes a new Interceptor.
func NewInterceptor(opts ...Option) *Interceptor {
c := &Interceptor{
codec: RawBytesCodec{},
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
filter: regexp.MustCompile(`.*`),
}
for _, opt := range opts {
opt(c)
}
if c.store == nil { // lazy init for LRU
l, _ := lru.New[string, Entry](1024)
c.store = NewLRU(l)
}
return c
}
// UnaryServerInterceptor returns a new unary server interceptor that caches the response.
// It doesn't use ETag header, but Cache-Control header.
func (c *Interceptor) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (resp any, err error) {
if !c.filter.MatchString(info.FullMethod) {
return handler(ctx, req)
}
if inMD, ok := metadata.FromIncomingContext(ctx); ok {
if h := inMD.Get("Cache-Control"); len(h) > 0 && h[0] == "no-cache" {
return handler(ctx, req)
}
}
key, err := c.key(info.FullMethod, req)
if err != nil {
c.logger.WarnContext(ctx, "gcache: failed to produce a key, skipping cache",
slog.Any(ErrKey, err))
return handler(ctx, req)
}
if e, ok := c.store.Get(ctx, key); ok {
if resp, err = c.buildResponse(info, e); err == nil {
return resp, nil
}
c.logger.WarnContext(ctx, "gcache: failed to unmarshal response, retrieving from handler",
slog.Any(ErrKey, err))
}
if resp, err = handler(ctx, req); err != nil {
return nil, err
}
bts, err := c.codec.Marshal(resp)
if err != nil {
c.logger.WarnContext(ctx, "gcache: failed to marshal response, value won't be cached",
slog.Any(ErrKey, err))
return resp, nil
}
c.store.Set(ctx, key, Entry{Value: bts})
return resp, nil
}
}
// UnaryClientInterceptor returns a new unary client interceptor that caches the response.
func (c *Interceptor) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
if !c.filter.MatchString(method) {
return invoker(ctx, method, req, reply, cc, opts...)
}
key, err := c.key(method, req)
if err != nil {
c.logger.WarnContext(ctx, "gcache: failed to produce a key, skipping cache",
slog.Any(ErrKey, err))
return invoker(ctx, method, req, reply, cc, opts...)
}
cachedValue, ok := c.store.Get(ctx, key)
if ok {
outMD, ok := metadata.FromOutgoingContext(ctx)
if !ok {
outMD = metadata.MD{}
}
outMD.Set("If-None-Match", cachedValue.ETag)
ctx = metadata.NewOutgoingContext(ctx, outMD)
}
inMD := &metadata.MD{}
opts = append(opts, grpc.Header(inMD), grpc.ForceCodec(c.codec))
var raw []byte
switch err = invoker(ctx, method, req, &raw, cc, opts...); {
case notChanged(ctx, err, inMD):
if err = c.codec.Unmarshal(cachedValue.Value, reply); err != nil {
return fmt.Errorf("unmarshal cached response: %w", err)
}
case err != nil:
return fmt.Errorf("call invoker: %w", err)
}
if err = c.codec.Unmarshal(raw, reply); err != nil {
return fmt.Errorf("unmarshal response: %w", err)
}
if etag := inMD.Get("ETag"); len(etag) != 0 {
c.store.Set(ctx, key, Entry{Value: raw, ETag: etag[0]})
} else {
c.store.Remove(ctx, key)
}
return nil
}
}
var responseCache sync.Map
func (c *Interceptor) responseType(fullMethodName string, srv any) (any, error) {
if v, ok := responseCache.Load(fullMethodName); ok {
return reflect.New(v.(reflect.Type)).Interface(), nil
}
parts := strings.Split(fullMethodName, "/")
method := parts[len(parts)-1]
typ := reflect.TypeOf(srv)
m, ok := typ.MethodByName(method)
if !ok {
return nil, fmt.Errorf("method %s not found in type %s", method, typ.Name())
}
respTyp := m.Type.Out(0).Elem()
responseCache.Store(fullMethodName, respTyp)
return reflect.New(respTyp).Interface(), nil
}
func (c *Interceptor) buildResponse(info *grpc.UnaryServerInfo, e Entry) (any, error) {
out, err := c.responseType(info.FullMethod, info.Server)
if err != nil {
return nil, fmt.Errorf("get response type: %w", err)
}
if err = c.codec.Unmarshal(e.Value, out); err != nil {
return nil, fmt.Errorf("unmarshal response: %w", err)
}
return out, nil
}
func (c *Interceptor) key(method string, req interface{}) (string, error) {
bts, err := c.codec.Marshal(req)
if err != nil {
return "", fmt.Errorf("marshal request: %w", err)
}
return fmt.Sprintf("%s{%x}", method, hash(bts)), nil
}
func notChanged(ctx context.Context, err error, inMD *metadata.MD) bool {
if err == nil {
return false
}
outMD, _ := metadata.FromOutgoingContext(ctx)
if status.Code(err) != codes.Aborted {
return false
}
if !slices.Equal(outMD.Get("if-none-match"), inMD.Get("etag")) {
return false
}
return true
}
func hash(bts []byte) []byte { h := sha1.Sum(bts); return h[:] } //nolint: gosec // we use sha1 for hashing