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

fix: prevent buffer reuse in async mode for Put method #16

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fastcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ type Item struct {
ContentType string
Compression string
ETag string
Blob []byte
// If the Blob is used beyond the scope of the request, it should be copied.
// Such as when the cache is written asynchronously.
Blob []byte
}

// Store represents a backend data store where bytes are cached. Individual
Expand Down
8 changes: 8 additions & 0 deletions stores/goredis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ type putReq struct {
// Put sets a value to given session but stored only on commit
func (s *Store) Put(namespace, group, uri string, b fastcache.Item, ttl time.Duration) error {
if s.config.Async {
// In async mode, we need to copy b.Blob to prevent fasthttp from reusing
// the buffer, as we will use the buffer in a separate goroutine beyond
// the scope of the current request.
blobCopy := make([]byte, len(b.Blob))
copy(blobCopy, b.Blob)
b.Blob = blobCopy

// Send the put request to the async buffer channel.
s.putBuf <- putReq{namespace, group, uri, b, ttl}
return nil
}
Expand Down
Loading