Skip to content

Commit

Permalink
fix: prevent buffer reuse in async mode for Put method
Browse files Browse the repository at this point in the history
If the mode is async, we need to copy request Blob to prevent
fasthttp from reusing the buffer since we are going to
use the buffer in a separate goroutine beyond the scope
of the current request.
  • Loading branch information
rhnvrm committed Nov 6, 2024
1 parent a434c18 commit 3a9f6c0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion stores/goredis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +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 {
s.putBuf <- putReq{namespace, group, uri, b, ttl}
// If the mode is async, we need to copy b.Blob to prevent
// fasthttp from reusing the buffer since we are going to
// use the buffer in a separate goroutine beyond the scope
// of the current request.
copiedItem := b
copiedItem.Blob = append([]byte(nil), b.Blob...)

s.putBuf <- putReq{namespace, group, uri, copiedItem, ttl}
return nil
}

Expand Down

0 comments on commit 3a9f6c0

Please sign in to comment.