-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.go
165 lines (124 loc) · 3.67 KB
/
frontend.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
package httpcache
import (
"context"
"errors"
"fmt"
"time"
"flamingo.me/flamingo/v3/framework/flamingo"
"go.opencensus.io/trace"
"golang.org/x/sync/singleflight"
)
var ErrInvalidEntry = errors.New("cache returned invalid entry type")
var ErrNoCacheBackend = errors.New("no backend defined")
type (
// Frontend caches and delivers HTTP responses
Frontend struct {
singleflight.Group
backend Backend
logger flamingo.Logger
}
)
// Inject dependencies
func (f *Frontend) Inject(
logger flamingo.Logger,
) *Frontend {
f.logger = logger
return f
}
// SetBackend for usage
func (f *Frontend) SetBackend(b Backend) *Frontend {
f.backend = b
return f
}
func (f *Frontend) Purge(ctx context.Context, key string) error {
if f.backend == nil {
return ErrNoCacheBackend
}
_, span := trace.StartSpan(ctx, "flamingo/httpcache/purge")
span.Annotate(nil, key)
defer span.End()
err := f.backend.Purge(key)
if err != nil {
return fmt.Errorf("failed to purge with key: %s: %w", key, err)
}
return nil
}
// Get the cached response if possible or perform a call to loader
// The result of loader will be returned and cached
func (f *Frontend) Get(ctx context.Context, key string, loader HTTPLoader) (Entry, error) {
if f.backend == nil {
return Entry{}, ErrNoCacheBackend
}
ctx, span := trace.StartSpan(ctx, "flamingo/httpcache/get")
span.Annotate(nil, key)
defer span.End()
if entry, ok := f.backend.Get(key); ok {
if entry.Meta.LifeTime.After(time.Now()) {
f.logger.WithContext(ctx).
WithField(flamingo.LogKeyCategory, "httpcache").
Debug("Serving from cache: ", key)
return entry, nil
}
if entry.Meta.GraceTime.After(time.Now()) {
// Try to load the actual value in background
go func() {
_, _ = f.load(ctx, key, loader)
}()
f.logger.WithContext(ctx).
WithField(flamingo.LogKeyCategory, "httpcache").
Debug("Gracetime! Serving from cache: ", key)
return entry, nil
}
}
f.logger.WithContext(ctx).
WithField(flamingo.LogKeyCategory, "httpcache").
Debug("No cache entry for: ", key)
return f.load(ctx, key, loader)
}
func (f *Frontend) load(ctx context.Context, key string, loader HTTPLoader) (Entry, error) { //nolint:contextcheck // this is fine
oldSpan := trace.FromContext(ctx)
newContext := trace.NewContext(context.Background(), oldSpan)
deadline, hasDeadline := ctx.Deadline()
if hasDeadline {
var cancel context.CancelFunc
newContext, cancel = context.WithDeadline(newContext, deadline)
defer cancel()
}
newContextWithSpan, span := trace.StartSpan(newContext, "flamingo/httpcache/load")
span.Annotate(nil, key)
defer span.End()
data, err, _ := f.Do(key, func() (res interface{}, resultErr error) {
ctx, fetchRoutineSpan := trace.StartSpan(newContextWithSpan, "flamingo/httpcache/fetchRoutine")
fetchRoutineSpan.Annotate(nil, key)
defer fetchRoutineSpan.End()
defer func() {
// catch potential panics
if err := recover(); err != nil {
if typedError, ok := err.(error); ok {
resultErr = typedError
return
}
resultErr = fmt.Errorf("HTTPCache Frontend.load exception: %#v", err) //nolint:err113 // no dedicated error needed here
}
}()
entry, err := loader(ctx)
if err != nil {
return nil, err
}
ctx, setSpan := trace.StartSpan(ctx, "flamingo/httpcache/set")
setSpan.Annotate(nil, key)
defer setSpan.End()
f.logger.WithContext(ctx).WithField(flamingo.LogKeyCategory, "httpcache").
Debugf("Store entry in Cache for key: %s", key)
_ = f.backend.Set(key, entry)
return entry, err
})
if err != nil {
return Entry{}, fmt.Errorf("http loader error: %w", err)
}
entry, ok := data.(Entry)
if !ok {
return Entry{}, ErrInvalidEntry
}
return entry, nil
}