pools

package module
v0.0.0-...-0fc6be2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2017 License: BSD-3-Clause Imports: 7 Imported by: 2

README

pools

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBuilder

func GetBuilder() *flatbuffers.Builder

func PutBuffer

func PutBuffer(b *Buffer)

func PutBuilder

func PutBuilder(b *flatbuffers.Builder)

Types

type Buffer

type Buffer struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

func GetBuffer

func GetBuffer() *Buffer

func (*Buffer) UnsafeBytes

func (b *Buffer) UnsafeBytes() []byte

UnsafeBytes returns a slice of bytes that will automatically add the Buffer it came from back into the pool when the GC attempts to collect it. This is only useful when the returned byte slice needs to 'outlive' the buffer, but an allocation isn't preferable. For example:

func Marshal(v interface{}) ([]byte, error) {
	b := pools.GetBuffer()
	defer pools.PutBuffer()

	err := json.NewEncoder(b).Encode()
	return b.Bytes(), err
}

func MyHandler(w http.ResponseWriter, r *http.Request) {
	b, err := Marshal("hello, world!")
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}

	// The Buffer created in the 'Marshal' call has already been put back
	// into the pool. Any other goroutine can now collect it from the pool.
	// New writes to the Buffer will overwrite 'b' since 'b' points directly
	// to the Buffer's internal buffer. This can corrupt data and race
	// conditions in Go are undefined behavior.
	_, err := w.Write(b)
	if err != nil {
		...
	}
}

By calling UnsafeBytes instead, the data would be protected.

IMPORTANT: Do not call pools.PutBuffer on the Buffer if you call UnsafeBytes at all. Additionally, do not append to the returned slice. Reallocation will invalidate the Buffer.

func (*Buffer) WriteGroups

func (w *Buffer) WriteGroups(offset, groupLen, groups int, prefix ...int) error

WriteGroups writes the interval [offset, offset+groupLen) to w N times. Each number is prefixed with '$' and suffixed with ', '. The final value in an interval and final interval in a set are not suffixed with ', '. The intervals are wrapped in parenthases. An error is only returned if the arguments are invalid. Arguments are invalid if offset < 0 or groups == 0.

WriteInterval(0, 4, 2) // ($0, $1, $2, $3, $4), ($5, $6, $7, $8, $9)

func (*Buffer) WriteInt

func (w *Buffer) WriteInt(i int)

WriteInt is a wrapper that writes i to w.

func (*Buffer) WriteInt64

func (w *Buffer) WriteInt64(i int64)

WriteInt64 is a wrapper that writes i to w.

func (*Buffer) WriteInterval

func (w *Buffer) WriteInterval(start, end, num int) error

WriteInterval writes the interval [start, end] to w N times. Each number is prefixed with '$' and suffixed with ', '. The final value in an interval and final interval in a set are not suffixed with ', '. The intervals are wrapped in parenthases. An error is only returned if the arguments are invalid. Arguments are invalid if start < 0, start >= end, or num == 0.

WriteInterval(0, 4, 2) // (0, 1, 2, 3, 4), (0, 1, 2, 3, 4)

Jump to

Keyboard shortcuts

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