bytesbuffers

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2022 License: BSD-3-Clause Imports: 2 Imported by: 3

Documentation

Overview

Package bytesbuffers provides multiple implementations of a "byte buffer pool" allowing for reuse of preallocated memory in the form of a *bytes.Buffer.

Example Usage: Marshal a JSON request body to a buffer, then put it back in the pool after the request.

pool := bytesbuffers.NewSyncPool(4)
var obj MyInput{}

buffer := pool.Get()
defer pool.Put(buffer)
_ = json.NewEncoder(buffer).Encode(obj)
_, _ = http.Post("http://localhost:1234", "application/json", buffer)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool interface {
	// Get reset bytes buffer from the pool.
	Get() *bytes.Buffer
	// Put adds buf to the pool.
	Put(buf *bytes.Buffer)
}

Pool is a type-safe interface for a pool of bytes buffers.

func NewSizedPool

func NewSizedPool(poolCapacity, defaultBufferCapacity int) Pool

NewSizedPool stores (up to) a fixed size of released bytes buffers in the pool.

Stored bytes buffers are not garbage collected until the reference to the pool is lost.

Use this for steady high-volume workloads.

func NewSyncPool

func NewSyncPool(defaultBufferCapacity int64) Pool

NewSyncPool uses the standard library's sync.Pool to store reusable bytes buffers until the next GC.

This self-sizes based on the highest concurrent usage per GC period.

Use this for spiky workloads.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL