github.com/blend/go-sdk@v1.20220411.3/bufferutil/put_on_close.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package bufferutil
     9  
    10  import "bytes"
    11  
    12  // PutOnClose wraps a buffer with a close function that will return
    13  // the buffer to the pool.
    14  func PutOnClose(buffer *bytes.Buffer, pool *Pool) *PutOnCloser {
    15  	return &PutOnCloser{Buffer: buffer, Pool: pool}
    16  }
    17  
    18  // PutOnCloser is a helper wrapper that will return a buffer to a given pool.
    19  type PutOnCloser struct {
    20  	*bytes.Buffer
    21  	Pool *Pool
    22  }
    23  
    24  // Close returns the buffer to the pool.
    25  func (poc PutOnCloser) Close() error {
    26  	poc.Pool.Put(poc.Buffer)
    27  	return nil
    28  }