Skip to content
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

Merged
merged 10 commits into from
Jun 24, 2024

Conversation

k1LoW
Copy link
Member

@k1LoW k1LoW commented Jun 14, 2024

In particular, avoid io.ReadAll

@k1LoW k1LoW added the enhancement New feature or request label Jun 14, 2024
@k1LoW k1LoW self-assigned this Jun 14, 2024

This comment has been minimized.

@k1LoW k1LoW added the minor label Jun 14, 2024

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 {
Copy link
Member Author

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
Comment on lines 193 to 211
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
}
}

Copy link
Member Author

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.

Comment on lines +194 to +206
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))
}()
Copy link
Member Author

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.

Copy link
Contributor

Code Metrics Report

main (1439b7d) #81 (90bdb04) +/-
Coverage 61.2% 61.9% +0.7%
Code to Test Ratio 1:2.2 1:2.2 -0.0
Test Execution Time 40s 40s 0s
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%)

Files Coverage +/-
copybuf.go 80.0% +80.0%
rc.go 77.8% +1.6%

Reported by octocov

@k1LoW k1LoW requested a review from pyama86 June 14, 2024 10:34
@k1LoW k1LoW merged commit 3781590 into main Jun 24, 2024
3 checks passed
@k1LoW k1LoW deleted the refactor-io branch June 24, 2024 22:07
@github-actions github-actions bot mentioned this pull request Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant