Skip to content
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
2 changes: 1 addition & 1 deletion benchmark/benchmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (p swappableBufferPool) Put(i *[]byte) {
}

func init() {
internal.SetDefaultBufferPoolForTesting.(func(mem.BufferPool))(swappableBufferPool{mem.DefaultBufferPool()})
internal.SetDefaultBufferPool.(func(mem.BufferPool))(swappableBufferPool{mem.DefaultBufferPool()})
}

var (
Expand Down
12 changes: 12 additions & 0 deletions experimental/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ import (
"google.golang.org/grpc/mem"
)

// SetDefaultBufferPool sets the default buffer pool used by all grpc clients
// and servers that do not have a buffer pool configured via WithBufferPool or
// BufferPool. It also changes the buffer pool used by the proto codec, which
// can't be changed otherwise. The provided buffer pool must be non-nil. The
// default value is mem.DefaultBufferPool.
//
// NOTE: this function must only be called during initialization time (i.e. in
// an init() function), and is not thread-safe. The last caller wins.
func SetDefaultBufferPool(bufferPool mem.BufferPool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of requiring users to set this before making RPCs, we recommend calling these functions within an init() function. This prevents data races that can occur when multiple packages invoke the same method simultaneously. For example, the balancer.Register function includes the following requirement:

NOTE: this function must only be called during initialization time (i.e., in an init() function) and is not thread-safe...

Can you please add a similar note here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, done.

internal.SetDefaultBufferPool.(func(mem.BufferPool))(bufferPool)
}

// WithBufferPool returns a grpc.DialOption that configures the use of bufferPool
// for parsing incoming messages on a grpc.ClientConn, and for temporary buffers
// when marshaling outgoing messages. By default, mem.DefaultBufferPool is used,
Expand Down
3 changes: 3 additions & 0 deletions internal/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
// option to configure a shared buffer pool for a grpc.Server.
BufferPool any // func (grpc.SharedBufferPool) grpc.ServerOption

// SetDefaultBufferPool updates the default buffer pool.
SetDefaultBufferPool any // func(mem.BufferPool)

// AcceptCompressors is implemented by the grpc package and returns
// a call option that restricts the grpc-accept-encoding header for a call.
AcceptCompressors any // func(...string) grpc.CallOption
Expand Down
4 changes: 0 additions & 4 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ var (
// original state. Only called in testing functions.
SnapshotMetricRegistryForTesting func() func()

// SetDefaultBufferPoolForTesting updates the default buffer pool, for
// testing purposes.
SetDefaultBufferPoolForTesting any // func(mem.BufferPool)

// SetBufferPoolingThresholdForTesting updates the buffer pooling threshold, for
// testing purposes.
SetBufferPoolingThresholdForTesting any // func(int)
Expand Down
2 changes: 1 addition & 1 deletion internal/leakcheck/leakcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var failTestsOnLeakedBuffers = false
func init() {
defaultPool := mem.DefaultBufferPool()
globalPool.Store(&defaultPool)
(internal.SetDefaultBufferPoolForTesting.(func(mem.BufferPool)))(&globalPool)
internal.SetDefaultBufferPool.(func(mem.BufferPool))(&globalPool)
}

var globalPool swappableBufferPool
Expand Down
2 changes: 1 addition & 1 deletion mem/buffer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var defaultBufferPool BufferPool
func init() {
defaultBufferPool = NewTieredBufferPool(defaultBufferPoolSizes...)

internal.SetDefaultBufferPoolForTesting = func(pool BufferPool) {
internal.SetDefaultBufferPool = func(pool BufferPool) {
defaultBufferPool = pool
}

Expand Down