Skip to content

Commit a880a8c

Browse files
committed
refactor: optimize slice capacity handling
1 parent 31abc27 commit a880a8c

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

internal/log.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func TraceIDFromContext(ctx context.Context) (string, bool) {
4242

4343
type (
4444
LogEntry struct {
45-
CacheStatus string
4645
URLKey string
4746
MiscProvider MiscProvider
4847
Error error
@@ -75,6 +74,9 @@ func (m Misc) LogValue() slog.Value {
7574
if m.RefIndex >= 0 && m.RefIndex < len(m.Refs) {
7675
attrs = append(attrs, slog.Any("ref", m.Refs[m.RefIndex]))
7776
}
77+
if cap(attrs) > len(attrs) {
78+
attrs = slices.Clip(attrs)
79+
}
7880
return slog.GroupValue(attrs...)
7981
}
8082

@@ -143,7 +145,6 @@ func (l *Logger) LogCacheHit(req *http.Request, urlKey string, mp MiscProvider)
143145
"Hit; served from cache.",
144146
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
145147
return CacheStatusHit, req, LogEntry{
146-
CacheStatus: CacheStatusHit.Value,
147148
URLKey: urlKey,
148149
MiscProvider: mp,
149150
Error: nil,
@@ -159,7 +160,6 @@ func (l *Logger) LogCacheMiss(req *http.Request, urlKey string, mp MiscProvider)
159160
"Miss; served from origin.",
160161
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
161162
return CacheStatusMiss, req, LogEntry{
162-
CacheStatus: CacheStatusMiss.Value,
163163
URLKey: urlKey,
164164
MiscProvider: mp,
165165
Error: nil,
@@ -175,7 +175,6 @@ func (l *Logger) LogCacheStale(req *http.Request, urlKey string, mp MiscProvider
175175
"Stale; served from cache.",
176176
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
177177
return CacheStatusStale, req, LogEntry{
178-
CacheStatus: CacheStatusStale.Value,
179178
URLKey: urlKey,
180179
MiscProvider: mp,
181180
Error: nil,
@@ -191,7 +190,6 @@ func (l *Logger) LogCacheStaleIfError(req *http.Request, urlKey string, mp MiscP
191190
"Stale; served from cache; stale-if-error policy applied.",
192191
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
193192
return CacheStatusStale, req, LogEntry{
194-
CacheStatus: CacheStatusStale.Value,
195193
URLKey: urlKey,
196194
MiscProvider: mp,
197195
Error: nil,
@@ -207,7 +205,6 @@ func (l *Logger) LogCacheStaleRevalidate(req *http.Request, urlKey string, mp Mi
207205
"Stale; served from cache; revalidating.",
208206
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
209207
return CacheStatusStale, req, LogEntry{
210-
CacheStatus: CacheStatusStale.Value,
211208
URLKey: urlKey,
212209
MiscProvider: mp,
213210
Error: nil,
@@ -223,7 +220,6 @@ func (l *Logger) LogCacheRevalidated(req *http.Request, urlKey string, mp MiscPr
223220
"Revalidated; served cached response.",
224221
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
225222
return CacheStatusRevalidated, req, LogEntry{
226-
CacheStatus: CacheStatusRevalidated.Value,
227223
URLKey: urlKey,
228224
MiscProvider: mp,
229225
Error: nil,
@@ -239,7 +235,6 @@ func (l *Logger) LogCacheBypass(msg string, req *http.Request, urlKey string, mp
239235
msg,
240236
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
241237
return CacheStatusBypass, req, LogEntry{
242-
CacheStatus: CacheStatusBypass.Value,
243238
URLKey: urlKey,
244239
MiscProvider: mp,
245240
Error: nil,
@@ -261,7 +256,6 @@ func (l *Logger) LogCacheError(
261256
msg,
262257
LogFunc(func() (CacheStatus, *http.Request, LogEntry) {
263258
return CacheStatus{Value: "error"}, req, LogEntry{
264-
CacheStatus: "error",
265259
URLKey: urlKey,
266260
MiscProvider: mp,
267261
Error: err,
@@ -293,7 +287,7 @@ func (l *Logger) logCache(ctx context.Context, level slog.Level, msg string, lp
293287
slog.String("host", req.Host),
294288
),
295289
groupAttrs("cache",
296-
slog.String("status", cl.CacheStatus),
290+
slog.Any("status", event),
297291
slog.String("url_key", cl.URLKey),
298292
),
299293
)

internal/normalization.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ outer:
192192
}
193193
slices.Sort(params)
194194
}
195+
if cap(params) > len(params) {
196+
params = slices.Clip(params)
197+
}
195198
qualityParts = append(qualityParts, qualityValue{
196199
main: main,
197200
q: q,

roundtripper.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ func (r *transport) handleUnrecognizedMethod(
218218
return nil, err
219219
}
220220
internal.CacheStatusBypass.ApplyTo(resp.Header)
221+
r.logger.LogCacheBypass(
222+
"Bypass; unrecognized (safe) method, served from upstream.",
223+
req,
224+
urlKey,
225+
nil,
226+
)
221227
return resp, nil
222228
}
223229
resp, err := r.upstream.RoundTrip(req)
@@ -229,7 +235,12 @@ func (r *transport) handleUnrecognizedMethod(
229235
r.ci.InvalidateCache(req.URL, resp.Header, refs, urlKey)
230236
}
231237
internal.CacheStatusBypass.ApplyTo(resp.Header)
232-
r.logger.LogCacheBypass("Bypass; unrecognized method, served from upstream.", req, urlKey, nil)
238+
r.logger.LogCacheBypass(
239+
"Bypass; unrecognized (unsafe) method, served from upstream.",
240+
req,
241+
urlKey,
242+
nil,
243+
)
233244
return resp, nil
234245
}
235246

@@ -241,6 +252,17 @@ func (r *transport) handleCacheMiss(
241252
) (*http.Response, error) {
242253
ccReq := internal.ParseCCRequestDirectives(req.Header)
243254
if ccReq.OnlyIfCached() {
255+
r.logger.LogCacheMiss(
256+
req,
257+
urlKey,
258+
internal.MiscFunc(func() internal.Misc {
259+
return internal.Misc{
260+
CCReq: ccReq,
261+
Refs: refs,
262+
RefIndex: refIndex,
263+
}
264+
}),
265+
)
244266
return make504Response(req)
245267
}
246268
resp, start, end, err := r.roundTripTimed(req)

store/fscache/fscache.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"net/url"
5858
"os"
5959
"path/filepath"
60+
"slices"
6061
"strings"
6162
"time"
6263

@@ -174,11 +175,11 @@ func WithBaseDir(base string) Option {
174175
}
175176

176177
func fromURL(u *url.URL) (*fsCache, error) {
177-
opts := make([]Option, 0, 4)
178-
var appname string
179-
if appname = u.Query().Get("appname"); appname == "" {
178+
appname := u.Query().Get("appname")
179+
if appname == "" {
180180
return nil, ErrMissingAppName
181181
}
182+
opts := make([]Option, 0, 4)
182183
if u.Path != "" && u.Path != "/" {
183184
opts = append(opts, WithBaseDir(u.Path))
184185
}
@@ -192,6 +193,9 @@ func fromURL(u *url.URL) (*fsCache, error) {
192193
key := cmp.Or(u.Query().Get("encrypt_key"), os.Getenv("FSCACHE_ENCRYPT_KEY"))
193194
opts = append(opts, WithEncryption(key))
194195
}
196+
if cap(opts) > len(opts) {
197+
opts = slices.Clip(opts)
198+
}
195199
return Open(appname, opts...)
196200
}
197201

@@ -452,5 +456,8 @@ func (c *fsCache) keys(prefix string) ([]string, error) {
452456
if err != nil {
453457
return nil, err
454458
}
459+
if cap(keys) > len(keys) {
460+
keys = slices.Clip(keys)
461+
}
455462
return keys, nil
456463
}

0 commit comments

Comments
 (0)