summaryrefslogtreecommitdiff
path: root/lib/go/thrift/deserializer.go
diff options
context:
space:
mode:
authorYuxuan 'fishy' Wang <yuxuan.wang@reddit.com>2020-09-22 15:03:57 -0700
committerYuxuan 'fishy' Wang <fishywang@gmail.com>2020-09-23 09:16:42 -0700
commit6dcd64ee5c0886697b38278956335ae4e945341b (patch)
tree38986b372d8a2d806d11ec6c0f96593c8e3f0e8c /lib/go/thrift/deserializer.go
parenta2c44665b416522477cffa6752c2f323768d0507 (diff)
downloadthrift-6dcd64ee5c0886697b38278956335ae4e945341b.tar.gz
THRIFT-5279: Go serializer/deserializer cleanups
Client: go Cleanup the default NewTSerializer and NewTDeserializer implementations to save an unnecessary allocation, and provide NewTSerializerPoolSizeFactory and NewTDeserializerPoolSizeFactory for easier non-default pool usages.
Diffstat (limited to 'lib/go/thrift/deserializer.go')
-rw-r--r--lib/go/thrift/deserializer.go32
1 files changed, 27 insertions, 5 deletions
diff --git a/lib/go/thrift/deserializer.go b/lib/go/thrift/deserializer.go
index e1203a868..cefc7ecda 100644
--- a/lib/go/thrift/deserializer.go
+++ b/lib/go/thrift/deserializer.go
@@ -31,12 +31,12 @@ type TDeserializer struct {
func NewTDeserializer() *TDeserializer {
transport := NewTMemoryBufferLen(1024)
-
- protocol := NewTBinaryProtocolFactoryDefault().GetProtocol(transport)
+ protocol := NewTBinaryProtocolTransport(transport)
return &TDeserializer{
- transport,
- protocol}
+ Transport: transport,
+ Protocol: protocol,
+ }
}
func (t *TDeserializer) ReadString(ctx context.Context, msg TStruct, s string) (err error) {
@@ -68,7 +68,8 @@ func (t *TDeserializer) Read(ctx context.Context, msg TStruct, b []byte) (err er
// TDeserializerPool is the thread-safe version of TDeserializer,
// it uses resource pool of TDeserializer under the hood.
//
-// It must be initialized with NewTDeserializerPool.
+// It must be initialized with either NewTDeserializerPool or
+// NewTDeserializerPoolSizeFactory.
type TDeserializerPool struct {
pool sync.Pool
}
@@ -86,6 +87,27 @@ func NewTDeserializerPool(f func() *TDeserializer) *TDeserializerPool {
}
}
+// NewTDeserializerPoolSizeFactory creates a new TDeserializerPool with
+// the given size and protocol factory.
+//
+// Note that the size is not the limit. The TMemoryBuffer underneath can grow
+// larger than that. It just dictates the initial size.
+func NewTDeserializerPoolSizeFactory(size int, factory TProtocolFactory) *TDeserializerPool {
+ return &TDeserializerPool{
+ pool: sync.Pool{
+ New: func() interface{} {
+ transport := NewTMemoryBufferLen(size)
+ protocol := factory.GetProtocol(transport)
+
+ return &TDeserializer{
+ Transport: transport,
+ Protocol: protocol,
+ }
+ },
+ },
+ }
+}
+
func (t *TDeserializerPool) ReadString(ctx context.Context, msg TStruct, s string) error {
d := t.pool.Get().(*TDeserializer)
defer t.pool.Put(d)