orbyte

package module
v0.0.0-...-3bb3583 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: AGPL-3.0 Imports: 6 Imported by: 3

README

orbyte

Lightweight & Safe (buffer-writer | general object) pool.

Quick Start

package main

import (
	"crypto/rand"
	"io"

	"github.com/fumiama/orbyte/pbuf"
)

func main() {
	buf := pbuf.NewBuffer(nil) // Allocate Buffer from pool.
	buf.P(func(buf *pbuf.Buffer) {
		io.CopyN(buf, rand.Reader, 4096) // Do sth.
	})
	// After that, buf will be auto-reused on GC.

	b := pbuf.NewBytes(1024) // Allocate Bytes from pool.
	b.V(func(b []byte) {
		rand.Read(b) // Do sth.
	})
	// After that, b will be auto-reused on GC.

}

Documentation

Overview

Package orbyte is a lightweight & safe (buffer-writer | general object) pool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[T any] struct {
	// contains filtered or unexported fields
}

Item represents a thread-safe user-defined value.

Only the item that has ownership can be a pointer. Do not copy neither Item nor *Item by yourself. You must always use the given methods.

func (*Item[T]) Copy

func (b *Item[T]) Copy() (cb *Item[T])

Copy data completely with separated ownership.

func (*Item[T]) HasInvolved

func (b *Item[T]) HasInvolved() bool

HasInvolved whether this item is buffered and will be Reset on putting back.

func (*Item[T]) Ignore

func (b *Item[T]) Ignore() *Item[T]

Ignore marks Item to be independent and will not be put back.

func (*Item[T]) ManualDestroy

func (b *Item[T]) ManualDestroy()

ManualDestroy item and put it back to pool.

Calling this method only when you're sure that no one will use it, or it will cause a panic.

func (*Item[T]) P

func (b *Item[T]) P(f func(*T)) *Item[T]

P use pointer value of the item.

This operation is safe in function f.

func (*Item[T]) Trans

func (b *Item[T]) Trans() T

Trans disable inner val being reset by destroy and return a safe copy of val.

This method is not thread-safe. Only call once on one item. The item will be destroyed after calling Trans().

Use it to drop your ownership before passing val (not its pointer) to another function that is not controlled by you.

func (*Item[T]) V

func (b *Item[T]) V(f func(T)) *Item[T]

V use value of the item.

This operation is safe in function f.

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool lightweight general pool.

func NewPool

func NewPool[T any](pooler Pooler[T]) *Pool[T]

NewPool make a new pool from custom pooler.

func (*Pool[T]) CountItems

func (pool *Pool[T]) CountItems() (outside, inside int32)

CountItems returns total item count outside and inside.

func (*Pool[T]) Involve

func (pool *Pool[T]) Involve(config, obj any) *Item[T]

InvolveItem[T any] involve external object into pool.

After that, you must only use the object through Item.

func (*Pool[T]) LimitInput

func (pool *Pool[T]) LimitInput(n int32)

LimitInputwill automatically set new item no-autodestroy if countout > inlim.

func (*Pool[T]) LimitOutput

func (pool *Pool[T]) LimitOutput(n int32)

LimitOutput will automatically set new item no-autodestroy if countout > outlim.

func (*Pool[T]) New

func (pool *Pool[T]) New(config any) *Item[T]

New call this to generate an item.

func (*Pool[T]) Parse

func (pool *Pool[T]) Parse(config, obj any) *Item[T]

ParseItem[T any] safely convert obj into pool item without copy.

You can still use the original object elsewhere.

func (*Pool[T]) SetNoPutBack

func (pool *Pool[T]) SetNoPutBack(on bool)

SetNoPutBack make it panic on every use-after-destroy.

Enable this to detect coding errors.

func (*Pool[T]) SetSyncItem

func (pool *Pool[T]) SetSyncItem(on bool)

SetSyncItem make it panic on every read-write conflict.

Enable this to detect coding errors.

type Pooler

type Pooler[T any] interface {
	New(config any, pooled T) T
	Parse(obj any, pooled T) T
	Reset(item *T)
	Copy(dst, src *T)
}

Pooler connects to a user-defined struct.

Directories

Path Synopsis
Package pbuf is a lightweight pooled buffer.
Package pbuf is a lightweight pooled buffer.

Jump to

Keyboard shortcuts

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