-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid getting request body as much as possible in the handler. #81
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
ww := w.(io.Writer) | ||
buf := getCopyBuf() | ||
defer putCopyBuf(buf) | ||
if _, err := io.CopyBuffer(ww, res.Body, buf); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use io.CopyBuffer instead of io.ReadAll
rc.go
Outdated
ok, expires := m.cacher.Storable(reqc, res, now) | ||
if !ok { | ||
m.logger.Debug("cache not storable", slog.String("host", reqc.Host), slog.String("method", reqc.Method), slog.String("url", reqc.URL.String()), slog.Any("headers", m.maskHeader(reqc.Header)), slog.Int("status", res.StatusCode), slog.Any("response_headers", m.maskHeader(res.Header))) | ||
return res, nil | ||
} | ||
|
||
res2 := rec.Result() | ||
go func() { | ||
// Store response as cache | ||
if err := m.cacher.Store(reqc, res2, expires); err != nil { | ||
m.logger.Error("failed to store cache", slog.String("error", err.Error()), slog.String("host", reqc.Host), slog.String("method", reqc.Method), slog.String("url", reqc.URL.String()), slog.Any("headers", m.maskHeader(reqc.Header)), slog.Int("status", res.StatusCode)) | ||
} | ||
m.logger.Debug("cache stored", slog.String("host", reqc.Host), slog.String("method", reqc.Method), slog.String("url", reqc.URL.String()), slog.Any("headers", m.maskHeader(reqc.Header)), slog.Int("status", res.StatusCode)) | ||
}() | ||
|
||
return res, nil | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Determines storable at the time the response is received from upstream.
go func() { | ||
ok, expires := m.cacher.Storable(reqc, resc, now) | ||
if !ok { | ||
m.logger.Debug("cache not storable", slog.String("host", reqc.Host), slog.String("method", reqc.Method), slog.String("url", reqc.URL.String()), slog.Any("headers", m.maskHeader(reqc.Header)), slog.Int("status", res.StatusCode), slog.Any("response_headers", m.maskHeader(resc.Header))) | ||
return | ||
} | ||
|
||
// Store response as cache | ||
if err := m.cacher.Store(reqc, resc, expires); err != nil { | ||
m.logger.Error("failed to store cache", slog.String("error", err.Error()), slog.String("host", reqc.Host), slog.String("method", reqc.Method), slog.String("url", reqc.URL.String()), slog.Any("headers", m.maskHeader(reqc.Header)), slog.Int("status", resc.StatusCode)) | ||
} | ||
m.logger.Debug("cache stored", slog.String("host", reqc.Host), slog.String("method", reqc.Method), slog.String("url", reqc.URL.String()), slog.Any("headers", m.maskHeader(reqc.Header)), slog.Int("status", resc.StatusCode)) | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use goroutine to avoid blocking due to the storing process.
Code Metrics Report
Details | | main (1439b7d) | #81 (90bdb04) | +/- |
|---------------------|----------------|---------------|-------|
+ | Coverage | 61.2% | 61.9% | +0.7% |
| Files | 7 | 8 | +1 |
| Lines | 446 | 454 | +8 |
+ | Covered | 273 | 281 | +8 |
- | Code to Test Ratio | 1:2.2 | 1:2.2 | -0.0 |
| Code | 758 | 775 | +17 |
| Test | 1685 | 1685 | 0 |
| Test Execution Time | 40s | 40s | 0s | Code coverage of files in pull request scope (76.2% → 77.9%)
Reported by octocov |
In particular, avoid
io.ReadAll