Skip to content

Commit

Permalink
Merge pull request #76 from 2manymws/skip-recorder
Browse files Browse the repository at this point in the history
If `rc.ErrShouldNotUseCache`, skip all caching processes.
  • Loading branch information
k1LoW authored Jun 13, 2024
2 parents c46cabf + 1f7dfdb commit ae091a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
5 changes: 4 additions & 1 deletion rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var defaultHeaderNamesToMask = []string{
type Cacher interface {
// Load loads the request/response cache.
// If the cache is not found, it returns ErrCacheNotFound.
// If not caching, it returns ErrNoCache.
// If the cache is expired, it returns ErrCacheExpired.
// If not caching, it returns ErrShouldNotUseCache.
Load(req *http.Request) (cachedReq *http.Request, cachedRes *http.Response, err error)
// Store stores the response cache.
Store(req *http.Request, res *http.Response, expires time.Time) error
Expand Down Expand Up @@ -107,6 +107,9 @@ func (m *cacheMw) Handler(next http.Handler) http.Handler {
m.logger.Debug("cache expired", slog.String("host", preq.Host), slog.String("method", preq.Method), slog.String("url", preq.URL.String()), slog.Any("headers", m.maskHeader(preq.Header)))
case errors.Is(err, ErrShouldNotUseCache):
m.logger.Debug("should not use cache", slog.String("host", preq.Host), slog.String("method", preq.Method), slog.String("url", preq.URL.String()), slog.Any("headers", m.maskHeader(preq.Header)))
// Skip caching
next.ServeHTTP(w, req)
return
default:
m.logger.Error("failed to load cache", slog.String("error", err.Error()), slog.String("host", preq.Host), slog.String("method", preq.Method), slog.String("url", preq.URL.String()), slog.Any("headers", m.maskHeader(preq.Header)))
}
Expand Down
18 changes: 5 additions & 13 deletions testutil/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ package testutil
import (
"crypto/sha1"
"encoding/hex"
"errors"
"io"
"net/http"
"strings"
"sync"
"testing"
"time"

"github.com/2manymws/rc"
)

var (
_ Cacher = &AllCache{}
_ Cacher = &GetOnlyCache{}
)

// errCacheNotFound is returned when the cache is not found
var errCacheNotFound error = errors.New("cache not found")

// errNoCache is returned if not caching
var errNoCache error = errors.New("no cache")

type Cacher interface {
Load(req *http.Request) (cachedReq *http.Request, cachedRes *http.Response, err error)
Store(req *http.Request, res *http.Response, expires time.Time) error
Expand Down Expand Up @@ -61,7 +56,7 @@ func (c *AllCache) Load(req *http.Request) (*http.Request, *http.Response, error
cc, ok := c.m[key]
c.mu.Unlock()
if !ok {
return nil, nil, errCacheNotFound
return nil, nil, rc.ErrCacheNotFound
}
cachedReq, cachedRes, err := decodeReqRes(cc)
if err != nil {
Expand Down Expand Up @@ -101,14 +96,14 @@ func NewGetOnlyCache(t testing.TB) *GetOnlyCache {
func (c *GetOnlyCache) Load(req *http.Request) (*http.Request, *http.Response, error) {
c.t.Helper()
if req.Method != http.MethodGet {
return nil, nil, errNoCache
return nil, nil, rc.ErrShouldNotUseCache
}
key := reqToKey(req)
c.mu.Lock()
cc, ok := c.m[key]
c.mu.Unlock()
if !ok {
return nil, nil, errCacheNotFound
return nil, nil, rc.ErrCacheNotFound
}
cachedReq, cachedRes, err := decodeReqRes(cc)
if err != nil {
Expand All @@ -121,9 +116,6 @@ func (c *GetOnlyCache) Load(req *http.Request) (*http.Request, *http.Response, e

func (c *GetOnlyCache) Store(req *http.Request, res *http.Response, expires time.Time) error {
c.t.Helper()
if req.Method != http.MethodGet {
return errNoCache
}
key := reqToKey(req)
cc, err := encodeReqRes(req, res)
if err != nil {
Expand Down

0 comments on commit ae091a6

Please sign in to comment.