-
Notifications
You must be signed in to change notification settings - Fork 2.5k
add configurable buffer sizes for Redis connections #3453
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
Merged
+298
−5
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b0bb6ce
add configurable buffer sizes for Redis connections
ofekshenawa c4cc80b
add MiB to wordlist
ofekshenawa c2fd8dc
Add description for buffer size parameter
ofekshenawa 49d2a55
Merge branch 'master' of https://github.com/redis/go-redis into add-c…
ofekshenawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,4 +73,5 @@ OAuth | |
Azure | ||
StreamingCredentialsProvider | ||
oauth | ||
entraid | ||
entraid | ||
MiB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package pool_test | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"net" | ||
"unsafe" | ||
|
||
. "github.com/bsm/ginkgo/v2" | ||
. "github.com/bsm/gomega" | ||
|
||
"github.com/redis/go-redis/v9/internal/pool" | ||
"github.com/redis/go-redis/v9/internal/proto" | ||
) | ||
|
||
var _ = Describe("Buffer Size Configuration", func() { | ||
var connPool *pool.ConnPool | ||
ctx := context.Background() | ||
|
||
AfterEach(func() { | ||
if connPool != nil { | ||
connPool.Close() | ||
} | ||
}) | ||
|
||
It("should use default buffer sizes when not specified", func() { | ||
connPool = pool.NewConnPool(&pool.Options{ | ||
Dialer: dummyDialer, | ||
PoolSize: 1, | ||
PoolTimeout: 1000, | ||
}) | ||
|
||
cn, err := connPool.NewConn(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer connPool.CloseConn(cn) | ||
|
||
// Check that default buffer sizes are used (0.5MiB) | ||
writerBufSize := getWriterBufSizeUnsafe(cn) | ||
readerBufSize := getReaderBufSizeUnsafe(cn) | ||
|
||
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
}) | ||
|
||
It("should use custom buffer sizes when specified", func() { | ||
customReadSize := 32 * 1024 // 32KB | ||
customWriteSize := 64 * 1024 // 64KB | ||
|
||
connPool = pool.NewConnPool(&pool.Options{ | ||
Dialer: dummyDialer, | ||
PoolSize: 1, | ||
PoolTimeout: 1000, | ||
ReadBufferSize: customReadSize, | ||
WriteBufferSize: customWriteSize, | ||
}) | ||
|
||
cn, err := connPool.NewConn(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer connPool.CloseConn(cn) | ||
|
||
// Check that custom buffer sizes are used | ||
writerBufSize := getWriterBufSizeUnsafe(cn) | ||
readerBufSize := getReaderBufSizeUnsafe(cn) | ||
|
||
Expect(writerBufSize).To(Equal(customWriteSize)) | ||
Expect(readerBufSize).To(Equal(customReadSize)) | ||
}) | ||
|
||
It("should handle zero buffer sizes by using defaults", func() { | ||
connPool = pool.NewConnPool(&pool.Options{ | ||
Dialer: dummyDialer, | ||
PoolSize: 1, | ||
PoolTimeout: 1000, | ||
ReadBufferSize: 0, // Should use default | ||
WriteBufferSize: 0, // Should use default | ||
}) | ||
|
||
cn, err := connPool.NewConn(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer connPool.CloseConn(cn) | ||
|
||
// Check that default buffer sizes are used (0.5MiB) | ||
writerBufSize := getWriterBufSizeUnsafe(cn) | ||
readerBufSize := getReaderBufSizeUnsafe(cn) | ||
|
||
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
}) | ||
|
||
It("should use 0.5MiB default buffer sizes for standalone NewConn", func() { | ||
// Test that NewConn (without pool) also uses 0.5MiB defaults | ||
netConn := newDummyConn() | ||
cn := pool.NewConn(netConn) | ||
defer cn.Close() | ||
|
||
writerBufSize := getWriterBufSizeUnsafe(cn) | ||
readerBufSize := getReaderBufSizeUnsafe(cn) | ||
|
||
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
}) | ||
|
||
It("should use 0.5MiB defaults even when pool is created directly without buffer sizes", func() { | ||
// Test the scenario where someone creates a pool directly (like in tests) | ||
// without setting ReadBufferSize and WriteBufferSize | ||
connPool = pool.NewConnPool(&pool.Options{ | ||
Dialer: dummyDialer, | ||
PoolSize: 1, | ||
PoolTimeout: 1000, | ||
// ReadBufferSize and WriteBufferSize are not set (will be 0) | ||
}) | ||
|
||
cn, err := connPool.NewConn(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer connPool.CloseConn(cn) | ||
|
||
// Should still get 0.5MiB defaults because NewConnPool sets them | ||
writerBufSize := getWriterBufSizeUnsafe(cn) | ||
readerBufSize := getReaderBufSizeUnsafe(cn) | ||
|
||
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 0.5MiB buffer size | ||
}) | ||
}) | ||
|
||
// Helper functions to extract buffer sizes using unsafe pointers | ||
func getWriterBufSizeUnsafe(cn *pool.Conn) int { | ||
cnPtr := (*struct { | ||
usedAt int64 | ||
netConn net.Conn | ||
rd *proto.Reader | ||
bw *bufio.Writer | ||
wr *proto.Writer | ||
// ... other fields | ||
})(unsafe.Pointer(cn)) | ||
|
||
if cnPtr.bw == nil { | ||
return -1 | ||
} | ||
|
||
bwPtr := (*struct { | ||
err error | ||
buf []byte | ||
n int | ||
wr interface{} | ||
})(unsafe.Pointer(cnPtr.bw)) | ||
|
||
return len(bwPtr.buf) | ||
} | ||
|
||
func getReaderBufSizeUnsafe(cn *pool.Conn) int { | ||
cnPtr := (*struct { | ||
usedAt int64 | ||
netConn net.Conn | ||
rd *proto.Reader | ||
bw *bufio.Writer | ||
wr *proto.Writer | ||
// ... other fields | ||
})(unsafe.Pointer(cn)) | ||
|
||
if cnPtr.rd == nil { | ||
return -1 | ||
} | ||
|
||
rdPtr := (*struct { | ||
rd *bufio.Reader | ||
})(unsafe.Pointer(cnPtr.rd)) | ||
|
||
if rdPtr.rd == nil { | ||
return -1 | ||
} | ||
|
||
bufReaderPtr := (*struct { | ||
buf []byte | ||
rd interface{} | ||
r, w int | ||
err error | ||
lastByte int | ||
lastRuneSize int | ||
})(unsafe.Pointer(rdPtr.rd)) | ||
|
||
return len(bufReaderPtr.buf) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.