util

package
v0.0.0-...-2b29d23 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: GPL-3.0 Imports: 42 Imported by: 67

Documentation

Overview

Package util provides tiny featured funcs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDaemonAlreadyStarted = NewIDError("daemon already started")
	ErrDaemonAlreadyStopped = NewIDError("daemon already stopped")
)
View Source
var (
	ErrFound          = NewIDError("found")
	ErrNotFound       = NewIDError("not found")
	ErrDuplicated     = NewIDError("duplicated")
	ErrNotImplemented = NewIDError("not implemented")
	ErrInternal       = NewIDError("internal error")
)
View Source
var (
	ErrLockedSetIgnore = NewIDError("ignore to set locked value")
	ErrLockedMapClosed = NewIDError("locked map closed")
)
View Source
var EmptyCancelFunc = func() error { return nil }
View Source
var ErrInvalid = NewIDError("invalid")
View Source
var ErrJobWorkerDone = NewIDError("job worker: no more new job")
View Source
var MinVersionLength = 2
View Source
var (
	ULIDLen = len([]byte(ULID().String()))
)

Functions

func AssertInterfaceValue

func AssertInterfaceValue[T any](v interface{}) (t T, _ error)

func AwareContext

func AwareContext(ctx context.Context, f func(context.Context) error) error

func AwareContextValue

func AwareContextValue[T any](ctx context.Context, f func(context.Context) (T, error)) (T, error)

func BatchWork

func BatchWork(
	ctx context.Context,
	size, limit int64,
	pref func(_ context.Context, last uint64) error,
	f func(_ context.Context, i, last uint64) error,
) error

BatchWork runs f by limit size in worker. For example, size=5 limit=2 are given, 1. Run worker(0,1) 2. Run worker(2,3) 3. Run worker(4), done.

func BigBytesToInt64

func BigBytesToInt64(b []byte) (int64, error)

func BoolToBytes

func BoolToBytes(b bool) []byte

func BytesToInt64

func BytesToInt64(b []byte) (int64, error)

func BytesToUint64

func BytesToUint64(b []byte) (uint64, error)

func CheckIsValiderSlice

func CheckIsValiderSlice[T IsValider](b []byte, allowNil bool, vs []T) error

func CheckIsValiders

func CheckIsValiders(b []byte, allowNil bool, vs ...IsValider) error

func CleanDirectory

func CleanDirectory(root string, filter func(path string) bool) error

func CompactAppendSlice

func CompactAppendSlice[T any](size int) (
	add func(T) bool,
	done func() []T,
)

func ConcatByterSlice

func ConcatByterSlice[T Byter](bs []T) []byte

func ConcatByters

func ConcatByters(bs ...Byter) []byte

func ConcatBytesSlice

func ConcatBytesSlice(sl ...[]byte) []byte

func ContextWithValues

func ContextWithValues(ctx context.Context, v map[ContextKey]interface{}) context.Context

func CountFilteredSlice

func CountFilteredSlice[T any](s []T, f func(T) bool) (n int)

func DecodeHash

func DecodeHash(s string) ([]byte, error)

func DefaultDecompressReaderFunc

func DefaultDecompressReaderFunc(format string) (func(io.Reader) (io.Reader, error), error)

func DelmSplitStrings

func DelmSplitStrings(s, delm string, n uint) string

func EncodeHash

func EncodeHash(b []byte) string

func EnsureRead

func EnsureRead(ctx context.Context, r io.Reader, b []byte) (n uint64, _ error)

func EnsureWrite

func EnsureWrite(w io.Writer, b []byte) (int, error)

func Filter2Slices

func Filter2Slices[T any, Y any](a []T, b []Y, f func(T, Y) bool) []T

func FilterMap

func FilterMap[T any, Y comparable](s map[Y]T, f func(Y, T) bool) []T

func FilterSlice

func FilterSlice[T any](s []T, f func(T) bool) []T

func Float64ToBytes

func Float64ToBytes(i float64) []byte

func FuncCaller

func FuncCaller(skip int) errors.Frame

func Int64ToBigBytes

func Int64ToBigBytes(i int64) []byte

func Int64ToBytes

func Int64ToBytes(i int64) []byte

func IsDuplicatedSlice

func IsDuplicatedSlice[T any](s []T, keyf func(T) (bool, string)) bool

func IsDuplicatedSliceWithMap

func IsDuplicatedSliceWithMap[T any](s []T, keyf func(T) (bool, string)) (map[string]T, bool)

func IsEqualHashByter

func IsEqualHashByter(a, b HashByter) bool

func IsNilJSON

func IsNilJSON(b []byte) bool

func IsValidURL

func IsValidURL(u *url.URL) error

func JoinErrors

func JoinErrors(errs ...error) error

func LoadFromContext

func LoadFromContext(ctx context.Context, a ...interface{}) error

func LoadFromContextOK

func LoadFromContextOK(ctx context.Context, a ...interface{}) error

func MarshalJSON

func MarshalJSON(v interface{}) ([]byte, error)

func MarshalJSONIndent

func MarshalJSONIndent(i interface{}) ([]byte, error)

func MustMarshalJSON

func MustMarshalJSON(i interface{}) []byte

func MustMarshalJSONIndent

func MustMarshalJSONIndent(i interface{}) []byte

func MustMarshalJSONIndentString

func MustMarshalJSONIndentString(i interface{}) string

func MustMarshalJSONString

func MustMarshalJSONString(i interface{}) string

func NewLengthedBytesSlice

func NewLengthedBytesSlice(m [][]byte) ([]byte, error)

func NormalizeTime

func NormalizeTime(t time.Time) time.Time

NormalizeTime clear the nanoseconds part from Time and make time to UTC. "2009-11-10T23:00:00.00101010Z" -> "2009-11-10T23:00:00.001Z",

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

func ParseRFC3339

func ParseRFC3339(s string) (time.Time, error)

ParseRFC3339 parses RFC3339 string.

func PipeReadWrite

func PipeReadWrite(
	ctx context.Context,
	read func(context.Context, io.Reader) error,
	write func(context.Context, io.Writer) error,
) error

func RFC3339

func RFC3339(t time.Time) string

RFC3339 formats time.Time to RFC3339Nano string.

func RandomChoiceSlice

func RandomChoiceSlice[T any](a []T, size int64) ([]T, error)

RandomChoiceSlice creates new n-sized slice randomly.

func ReadLength

func ReadLength(r io.Reader) (read uint64, length uint64, _ error)

func ReadLengthBytes

func ReadLengthBytes(b []byte) (uint64, error)

func ReadLengthed

func ReadLengthed(r io.Reader) (read uint64, _ []byte, _ error)

func ReadLengthedBytes

func ReadLengthedBytes(b []byte) (_ []byte, left []byte, _ error)

func ReadLengthedBytesSlice

func ReadLengthedBytesSlice(b []byte) (m [][]byte, left []byte, _ error)

func ReadLengthedSlice

func ReadLengthedSlice(r io.Reader) (read uint64, hs [][]byte, _ error)

func ReflectPtrSetInterfaceValue

func ReflectPtrSetInterfaceValue[T any](v interface{}, t *T) error

func ReflectSetInterfaceValue

func ReflectSetInterfaceValue(v, target interface{}) error

func RemoveDuplicatedSlice

func RemoveDuplicatedSlice[T any](s []T, f func(T) (string, error)) ([]T, error)

func Retry

func Retry(ctx context.Context, f func() (bool, error), limit int, interval time.Duration) error

func RunErrCallbackJobWorker

func RunErrCallbackJobWorker(
	ctx context.Context,
	workersize, size int64,
	errf func(error),
	f func(ctx context.Context, i, jobid uint64) error,
) error

func RunJobWorker

func RunJobWorker(
	ctx context.Context,
	workersize, size int64,
	f func(ctx context.Context, i, jobid uint64) error,
) error

func RunJobWorkerByJobs

func RunJobWorkerByJobs(ctx context.Context, jobs ...ContextWorkerCallback) error

func SHA256Checksum

func SHA256Checksum(b []byte) string

func SetInterfaceValue

func SetInterfaceValue[T any](v interface{}, t *T) error

func SingleflightDo

func SingleflightDo[T any](
	sg *singleflight.Group, key string, f func() (T, error),
) (t T, _ error, _ bool)

func SortCompactAppendSlice

func SortCompactAppendSlice[T any](size int, less func(T, T) bool) (
	add func(T) bool,
	done func() []T,
)

func StringError

func StringError(format string, args ...interface{}) *baseError

func TimeEqual

func TimeEqual(a, b time.Time) bool

func TimeString

func TimeString(t time.Time) string

func TraverseSlice

func TraverseSlice[T any](s []T, f func(int, T) error) error

func ULID

func ULID() ulid.ULID

func UUID

func UUID() uuid.UUID

func Uint64ToBigBytes

func Uint64ToBigBytes(i uint64) []byte

func Uint64ToBytes

func Uint64ToBytes(i uint64) []byte

func Uint8ToBytes

func Uint8ToBytes(i uint8) []byte

func UintToBytes

func UintToBytes(i uint) []byte

func UniqCompactAppendSlice

func UniqCompactAppendSlice[T cmp.Ordered](size int) (
	add func(T),
	done func() []T,
)

func UnmarshalJSON

func UnmarshalJSON(b []byte, v interface{}) error

func UnmarshalWithYAMLOrderedMap

func UnmarshalWithYAMLOrderedMap(b []byte) (interface{}, error)

func UnmarshalYAMLNodeWithYAMLOrderedMap

func UnmarshalYAMLNodeWithYAMLOrderedMap(y *yaml.Node) (interface{}, error)

func WriteLength

func WriteLength(w io.Writer, i uint64) error

func WriteLengthed

func WriteLengthed(w io.Writer, b []byte) error

func WriteLengthedSlice

func WriteLengthedSlice(w io.Writer, m [][]byte) error

func ZerologMarshalStack

func ZerologMarshalStack(err error) interface{}

Types

type BaseGCache

type BaseGCache[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func NewLFUGCache

func NewLFUGCache[K cmp.Ordered, V any](size int) *BaseGCache[K, V]

func NewLRUGCache

func NewLRUGCache[K cmp.Ordered, V any](size int) *BaseGCache[K, V]

func (*BaseGCache[K, V]) Close

func (c *BaseGCache[K, V]) Close()

func (*BaseGCache[K, V]) Exists

func (c *BaseGCache[K, V]) Exists(key K) bool

func (*BaseGCache[K, V]) Get

func (c *BaseGCache[K, V]) Get(key K) (v V, found bool)

func (*BaseGCache[K, V]) Purge

func (c *BaseGCache[K, V]) Purge()

func (*BaseGCache[K, V]) Remove

func (c *BaseGCache[K, V]) Remove(key K) bool

func (*BaseGCache[K, V]) Set

func (c *BaseGCache[K, V]) Set(key K, v V, expire time.Duration)

func (*BaseGCache[K, V]) Traverse

func (c *BaseGCache[K, V]) Traverse(f func(K, V) bool)

type BaseJobWorker

type BaseJobWorker struct {
	NewJobFunc func(jobCount uint64, _ ContextWorkerCallback) error
	// contains filtered or unexported fields
}

func NewBaseJobWorker

func NewBaseJobWorker(ctx context.Context, semSize int64) (*BaseJobWorker, error)

func NewErrCallbackJobWorker

func NewErrCallbackJobWorker(ctx context.Context, semSize int64, errf func(error)) (wk *BaseJobWorker, _ error)

NewErrCallbackJobWorker ignores job error.

func (*BaseJobWorker) Cancel

func (wk *BaseJobWorker) Cancel()

func (*BaseJobWorker) Close

func (wk *BaseJobWorker) Close()

func (*BaseJobWorker) Done

func (wk *BaseJobWorker) Done()

func (*BaseJobWorker) LazyWait

func (wk *BaseJobWorker) LazyWait() error

LazyWait don't wait until all job finished.

func (*BaseJobWorker) NewJob

func (*BaseJobWorker) Wait

func (wk *BaseJobWorker) Wait() error

Wait waits until all job finished.

type BaseParams

type BaseParams struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewBaseParams

func NewBaseParams() *BaseParams

func (*BaseParams) ID

func (p *BaseParams) ID() string

func (*BaseParams) IsValid

func (p *BaseParams) IsValid([]byte) error

func (*BaseParams) Set

func (p *BaseParams) Set(f func() (bool, error)) error

func (*BaseParams) SetDuration

func (p *BaseParams) SetDuration(d time.Duration, f func(time.Duration) (bool, error)) error

func (*BaseParams) SetInt

func (p *BaseParams) SetInt(d int, f func(int) (bool, error)) error

func (*BaseParams) SetOverZeroInt

func (p *BaseParams) SetOverZeroInt(d int, f func(int) (bool, error)) error

func (*BaseParams) SetUint64

func (p *BaseParams) SetUint64(d uint64, f func(uint64) (bool, error)) error

type BufferedResetReader

type BufferedResetReader struct {
	io.Reader

	sync.RWMutex
	// contains filtered or unexported fields
}

func NewBufferedResetReader

func NewBufferedResetReader(r io.Reader) *BufferedResetReader

func (*BufferedResetReader) Close

func (s *BufferedResetReader) Close() error

func (*BufferedResetReader) Read

func (s *BufferedResetReader) Read(p []byte) (int, error)

func (*BufferedResetReader) Reset

func (s *BufferedResetReader) Reset()

type BufferedWriter

type BufferedWriter struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

func NewBufferedWriter

func NewBufferedWriter(w io.Writer, size int) *BufferedWriter

func (*BufferedWriter) Close

func (w *BufferedWriter) Close() error

type BuildInfo

type BuildInfo struct {
	Branch    string
	Commit    string
	BuildTime time.Time
	Version   Version
}

func ParseBuildInfo

func ParseBuildInfo(version, branch, commit, buildTime string) (BuildInfo, error)

func (BuildInfo) String

func (bi BuildInfo) String() string

type Byter

type Byter interface {
	Bytes() []byte
}

type BytesFrameReader

type BytesFrameReader struct {
	// contains filtered or unexported fields
}

func NewBufferBytesFrameReader

func NewBufferBytesFrameReader(b []byte) (*BytesFrameReader, *bytes.Buffer, error)

func NewBufferBytesNoHeadersFrameReader

func NewBufferBytesNoHeadersFrameReader(b []byte) (*BytesFrameReader, *bytes.Buffer, error)

func NewBytesFrameReader

func NewBytesFrameReader(r io.Reader) (*BytesFrameReader, error)

func (*BytesFrameReader) Body

func (f *BytesFrameReader) Body() ([]byte, error)

func (*BytesFrameReader) BodyReader

func (f *BytesFrameReader) BodyReader() (io.Reader, error)

func (*BytesFrameReader) Header

func (f *BytesFrameReader) Header() ([][]byte, error)

func (*BytesFrameReader) Lengthed

func (f *BytesFrameReader) Lengthed(read func([]byte) error) error

func (*BytesFrameReader) Reader

func (f *BytesFrameReader) Reader() io.Reader

func (*BytesFrameReader) Version

func (f *BytesFrameReader) Version() [2]byte

type BytesFrameWriter

type BytesFrameWriter struct {
	// contains filtered or unexported fields
}

func NewBufferBytesFrameWriter

func NewBufferBytesFrameWriter() (*BytesFrameWriter, *bytes.Buffer)

func NewBytesFrameWriter

func NewBytesFrameWriter(w io.Writer) (*BytesFrameWriter, error)

func (*BytesFrameWriter) Header

func (f *BytesFrameWriter) Header(bs ...[]byte) error

func (*BytesFrameWriter) Lengthed

func (f *BytesFrameWriter) Lengthed(b []byte) error

Lengthed writes bytes body with length. Lengthed can be used multiple times.

func (*BytesFrameWriter) Writer

func (f *BytesFrameWriter) Writer() io.Writer

Writer returns io.Writer. Writer is for not lengthed body.

type BytesToByter

type BytesToByter []byte

func (BytesToByter) Bytes

func (b BytesToByter) Bytes() []byte

type ChecksumReader

type ChecksumReader interface {
	io.Reader
	Checksum() string
	Close() error
}

type ChecksumWriter

type ChecksumWriter interface {
	io.WriteCloser
	Name() string
	Checksum() string
}

type CompressedReader

type CompressedReader struct {
	io.Reader

	Format string // NOTE compress format, usually file extension without '.' prefix

	sync.RWMutex
	// contains filtered or unexported fields
}

func NewCompressedReader

func NewCompressedReader(
	r io.Reader,
	format string,
	decompressReader DecompressReaderFunc,
) (*CompressedReader, error)

func (*CompressedReader) Close

func (r *CompressedReader) Close() error

Close only closes decompress Reader.

func (*CompressedReader) Decompress

func (r *CompressedReader) Decompress() (io.Reader, error)

func (*CompressedReader) Exaust

func (r *CompressedReader) Exaust() error

Exaust read all unread bytes from Decompress().

func (*CompressedReader) Tee

func (r *CompressedReader) Tee(tee, decompressed io.Writer) (io.Reader, error)

type ContextDaemon

type ContextDaemon struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewContextDaemon

func NewContextDaemon(startfunc func(context.Context) error) *ContextDaemon

func (*ContextDaemon) IsStarted

func (dm *ContextDaemon) IsStarted() bool

func (*ContextDaemon) Start

func (dm *ContextDaemon) Start(ctx context.Context) error

func (*ContextDaemon) Stop

func (dm *ContextDaemon) Stop() error

func (*ContextDaemon) Wait

func (dm *ContextDaemon) Wait(ctx context.Context) <-chan error

type ContextKey

type ContextKey string

type ContextWorkerCallback

type ContextWorkerCallback func(ctx context.Context, jobid uint64) error

type Daemon

type Daemon interface {
	Start(context.Context) error
	Stop() error
}

type DecompressReaderFunc

type DecompressReaderFunc func(format string) (func(io.Reader) (io.Reader, error), error)

type DefaultExtensibleJSON

type DefaultExtensibleJSON struct {
	// contains filtered or unexported fields
}

func (DefaultExtensibleJSON) MarshaledJSON

func (e DefaultExtensibleJSON) MarshaledJSON() ([]byte, bool)

func (*DefaultExtensibleJSON) SetMarshaledJSON

func (e *DefaultExtensibleJSON) SetMarshaledJSON(b []byte)

type DummyByter

type DummyByter func() []byte

func (DummyByter) Bytes

func (d DummyByter) Bytes() []byte

type DummyChecksumWriter

type DummyChecksumWriter struct {
	io.WriteCloser
	// contains filtered or unexported fields
}

func NewDummyChecksumWriter

func NewDummyChecksumWriter(f io.WriteCloser, cw ChecksumWriter) *DummyChecksumWriter

func (*DummyChecksumWriter) Checksum

func (w *DummyChecksumWriter) Checksum() string

func (*DummyChecksumWriter) Name

func (w *DummyChecksumWriter) Name() string

type DummyIsValider

type DummyIsValider func([]byte) error

func (DummyIsValider) IsValid

func (iv DummyIsValider) IsValid(b []byte) error

type ExtensibleJSON

type ExtensibleJSON interface {
	MarshaledJSON() ([]byte, bool)
}

type ExtensibleJSONSetter

type ExtensibleJSONSetter interface {
	SetMarshaledJSON([]byte)
}

type FuncChain

type FuncChain struct {
	// contains filtered or unexported fields
}

func NewFuncChain

func NewFuncChain() *FuncChain

func (*FuncChain) Add

func (c *FuncChain) Add(f func() (bool, error)) *FuncChain

func (*FuncChain) Run

func (c *FuncChain) Run() error

type GCache

type GCache[K cmp.Ordered, V any] interface {
	Exists(K) bool
	Get(K) (_ V, found bool)
	Set(_ K, _ V, expire time.Duration)
	Remove(K) bool
	Close()
	Purge()
	Traverse(func(K, V) bool)
}

type GzipWriter

type GzipWriter struct {
	io.Writer

	sync.Mutex
	// contains filtered or unexported fields
}

GzipWriter closes the underlying reader too.

func NewGzipWriter

func NewGzipWriter(f io.Writer, level int) (*GzipWriter, error)

func (*GzipWriter) Close

func (w *GzipWriter) Close() error

func (*GzipWriter) Write

func (w *GzipWriter) Write(p []byte) (int, error)

type Hash

type Hash interface {
	Equal(Hash) bool
	fmt.Stringer // NOTE usually String() value is the hex encoded of Bytes()
	Byter
	IsValider
}

type HashByter

type HashByter interface {
	// HashBytes is uses to generate hash
	HashBytes() []byte
}

type HashChecksumReader

type HashChecksumReader struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewHashChecksumReader

func NewHashChecksumReader(r io.Reader, h hash.Hash) *HashChecksumReader

func (*HashChecksumReader) Checksum

func (r *HashChecksumReader) Checksum() string

func (*HashChecksumReader) Close

func (r *HashChecksumReader) Close() error

func (*HashChecksumReader) Read

func (r *HashChecksumReader) Read(b []byte) (int, error)

type HashChecksumWriter

type HashChecksumWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewHashChecksumWriter

func NewHashChecksumWriter(h hash.Hash) *HashChecksumWriter

func NewHashChecksumWriterWithWriter

func NewHashChecksumWriterWithWriter(fname string, w io.Writer, h hash.Hash) *HashChecksumWriter

func (*HashChecksumWriter) Checksum

func (w *HashChecksumWriter) Checksum() string

func (*HashChecksumWriter) Close

func (w *HashChecksumWriter) Close() error

func (*HashChecksumWriter) Name

func (w *HashChecksumWriter) Name() string

func (*HashChecksumWriter) Write

func (w *HashChecksumWriter) Write(b []byte) (int, error)

type Hasher

type Hasher interface {
	Hash() Hash
}

type IDError

type IDError struct {
	// contains filtered or unexported fields
}

func NewBaseIDErrorWithID

func NewBaseIDErrorWithID(id, format string, args ...interface{}) *IDError

func NewIDError

func NewIDError(format string, args ...interface{}) *IDError

func NewIDErrorWithID

func NewIDErrorWithID(id, format string, args ...interface{}) *IDError

func (IDError) Error

func (er IDError) Error() string

func (*IDError) Errorf

func (er *IDError) Errorf(format string, args ...interface{}) *IDError

Errorf formats strings. It does not support `%w` error formatting.

func (IDError) Format

func (er IDError) Format(st fmt.State, verb rune)

func (*IDError) Is

func (er *IDError) Is(err error) bool

func (IDError) StackTrace

func (er IDError) StackTrace() errors.StackTrace

func (IDError) Unwrap

func (er IDError) Unwrap() error

func (*IDError) WithMessage

func (er *IDError) WithMessage(err error, format string, args ...interface{}) error

WithMessage formats strings with error.

func (*IDError) WithStack

func (er *IDError) WithStack() *IDError

func (*IDError) Wrap

func (er *IDError) Wrap(err error) error

type IsValider

type IsValider interface {
	IsValid([]byte) error
}

type JobWorker

type JobWorker interface {
	NewJob(ContextWorkerCallback) error
	Done()
	Wait() error
	Close()
}

type Locked

type Locked[T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func EmptyLocked

func EmptyLocked[T any]() *Locked[T]

func NewLocked

func NewLocked[T any](v T) *Locked[T]

func (*Locked[T]) Empty

func (l *Locked[T]) Empty(f func(_ T, isempty bool) error) error

func (*Locked[T]) EmptyValue

func (l *Locked[T]) EmptyValue() *Locked[T]

func (*Locked[T]) Get

func (l *Locked[T]) Get(f func(T, bool) error) error

func (*Locked[T]) GetOrCreate

func (l *Locked[T]) GetOrCreate(
	f func(_ T, created bool) error,
	create func() (T, error),
) error

func (*Locked[T]) MustValue

func (l *Locked[T]) MustValue() (v T)

func (*Locked[T]) Set

func (l *Locked[T]) Set(f func(_ T, isempty bool) (T, error)) (v T, _ error)

func (*Locked[T]) SetValue

func (l *Locked[T]) SetValue(v T) *Locked[T]

func (*Locked[T]) Value

func (l *Locked[T]) Value() (v T, isempty bool)

type LockedMap

type LockedMap[K cmp.Ordered, V any] interface {
	Exists(key K) (found bool)
	Value(key K) (value V, found bool)
	SetValue(key K, value V) (added bool)
	RemoveValue(key K) (removed bool)
	Get(key K, f func(value V, found bool) error) error
	GetOrCreate(key K, f func(value V, created bool) error, create func() (value V, _ error)) error
	Set(key K, f func(_ V, found bool) (value V, _ error)) (value V, created bool, _ error)
	Remove(key K, f func(value V, found bool) error) (removed bool, _ error)
	SetOrRemove(key K, f func(_ V, found bool) (value V, remove bool, _ error)) (_ V, created, removed bool, _ error)
	Traverse(f func(key K, value V) (keep bool)) bool
	Len() int
	Empty()
	Close()
	Map() map[K]V
}

func NewLockedMap

func NewLockedMap[K cmp.Ordered, V any](
	size uint64,
	newMap func() LockedMap[K, V],
) (LockedMap[K, V], error)

type PurgeFuncGCache

type PurgeFuncGCache[K cmp.Ordered, V any] struct {
	GCache[K, V]
	// contains filtered or unexported fields
}

func NewPurgeFuncGCache

func NewPurgeFuncGCache[K cmp.Ordered, V any](
	b GCache[K, V],
	purgef func() bool,
) *PurgeFuncGCache[K, V]

func (*PurgeFuncGCache[K, V]) Purge

func (c *PurgeFuncGCache[K, V]) Purge()

type ReadableDuration

type ReadableDuration time.Duration

func (ReadableDuration) MarshalText

func (d ReadableDuration) MarshalText() ([]byte, error)

func (*ReadableDuration) UnmarshalJSON

func (d *ReadableDuration) UnmarshalJSON(b []byte) error

func (*ReadableDuration) UnmarshalYAML

func (d *ReadableDuration) UnmarshalYAML(y *yaml.Node) error

type SafeGzipReadCloser

type SafeGzipReadCloser struct {
	io.Reader
	// contains filtered or unexported fields
}

SafeGzipReadCloser closes the underlying reader too.

func NewSafeGzipReadCloser

func NewSafeGzipReadCloser(f io.Reader) (*SafeGzipReadCloser, error)

func (*SafeGzipReadCloser) Close

func (r *SafeGzipReadCloser) Close() error

func (*SafeGzipReadCloser) Read

func (r *SafeGzipReadCloser) Read(p []byte) (int, error)

type ShardedMap

type ShardedMap[K cmp.Ordered, V any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewDeepShardedMap

func NewDeepShardedMap[K cmp.Ordered, V any](
	sizes []uint64,
	newMap func() LockedMap[K, V],
) (*ShardedMap[K, V], error)

func NewShardedMap

func NewShardedMap[K cmp.Ordered, V any](
	size uint64,
	newMap func() LockedMap[K, V],
) (*ShardedMap[K, V], error)

func NewShardedMapWithSeed

func NewShardedMapWithSeed[K cmp.Ordered, V any](
	seed uint32,
	size uint64,
	hashf func(interface{}, uint64) (uint64, interface{}),
	newMap func() LockedMap[K, V],
) (*ShardedMap[K, V], error)

func (*ShardedMap[K, V]) Close

func (l *ShardedMap[K, V]) Close()

func (*ShardedMap[K, V]) Empty

func (l *ShardedMap[K, V]) Empty()

func (*ShardedMap[K, V]) Exists

func (l *ShardedMap[K, V]) Exists(k K) bool

func (*ShardedMap[K, V]) Get

func (l *ShardedMap[K, V]) Get(key K, f func(value V, found bool) error) error

func (*ShardedMap[K, V]) GetOrCreate

func (l *ShardedMap[K, V]) GetOrCreate(
	k K,
	f func(_ V, created bool) error,
	create func() (V, error),
) error

func (*ShardedMap[K, V]) Len

func (l *ShardedMap[K, V]) Len() int

func (*ShardedMap[K, V]) Map

func (l *ShardedMap[K, V]) Map() (m map[K]V)

func (*ShardedMap[K, V]) Remove

func (l *ShardedMap[K, V]) Remove(k K, f func(V, bool) error) (bool, error)

func (*ShardedMap[K, V]) RemoveValue

func (l *ShardedMap[K, V]) RemoveValue(k K) bool

func (*ShardedMap[K, V]) Set

func (l *ShardedMap[K, V]) Set(k K, f func(V, bool) (V, error)) (v V, created bool, _ error)

func (*ShardedMap[K, V]) SetOrRemove

func (l *ShardedMap[K, V]) SetOrRemove(k K, f func(V, bool) (V, bool, error)) (v V, _ bool, _ bool, _ error)

func (*ShardedMap[K, V]) SetValue

func (l *ShardedMap[K, V]) SetValue(k K, v V) (added bool)

func (*ShardedMap[K, V]) Traverse

func (l *ShardedMap[K, V]) Traverse(f func(K, V) bool) bool

func (*ShardedMap[K, V]) TraverseMap

func (l *ShardedMap[K, V]) TraverseMap(f func(LockedMap[K, V]) bool) bool

func (*ShardedMap[K, V]) Value

func (l *ShardedMap[K, V]) Value(k K) (v V, found bool)

type SimpleTimer

type SimpleTimer struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewSimpleTimer

func NewSimpleTimer(
	id TimerID,
	intervalFunc func(uint64) time.Duration,
	callback func(context.Context, uint64) (bool, error),
	whenRemoved func(),
) *SimpleTimer

type SimpleTimers

type SimpleTimers struct {
	*ContextDaemon
	// contains filtered or unexported fields
}

SimpleTimers handles the multiple schdduled jobs without many goroutines.

func NewSimpleTimers

func NewSimpleTimers(size uint64, resolution time.Duration) (*SimpleTimers, error)

func NewSimpleTimersFixedIDs

func NewSimpleTimersFixedIDs(size uint64, interval time.Duration, ids []TimerID) (*SimpleTimers, error)

func (*SimpleTimers) New

func (ts *SimpleTimers) New(
	id TimerID,
	intervalFunc func(uint64) time.Duration,
	callback func(context.Context, uint64) (bool, error),
) (bool, error)

func (*SimpleTimers) NewTimer

func (ts *SimpleTimers) NewTimer(timer *SimpleTimer) (bool, error)

func (*SimpleTimers) Stop

func (ts *SimpleTimers) Stop() error

func (*SimpleTimers) StopAllTimers

func (ts *SimpleTimers) StopAllTimers() error

func (*SimpleTimers) StopOthers

func (ts *SimpleTimers) StopOthers(exclude []TimerID) error

StopOthers stops timers except exclude timers.

func (*SimpleTimers) StopTimers

func (ts *SimpleTimers) StopTimers(ids []TimerID) error

type SingleLockedMap

type SingleLockedMap[K cmp.Ordered, V any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewSingleLockedMap

func NewSingleLockedMap[K cmp.Ordered, V any]() *SingleLockedMap[K, V]

func (*SingleLockedMap[K, V]) Close

func (l *SingleLockedMap[K, V]) Close()

func (*SingleLockedMap[K, V]) Empty

func (l *SingleLockedMap[K, V]) Empty()

func (*SingleLockedMap[K, V]) Exists

func (l *SingleLockedMap[K, V]) Exists(k K) bool

func (*SingleLockedMap[K, V]) Get

func (l *SingleLockedMap[K, V]) Get(key K, f func(value V, found bool) error) error

func (*SingleLockedMap[K, V]) GetOrCreate

func (l *SingleLockedMap[K, V]) GetOrCreate(
	k K,
	f func(_ V, created bool) error,
	create func() (V, error),
) error

func (*SingleLockedMap[K, V]) Len

func (l *SingleLockedMap[K, V]) Len() int

func (*SingleLockedMap[K, V]) Map

func (l *SingleLockedMap[K, V]) Map() (m map[K]V)

func (*SingleLockedMap[K, V]) Remove

func (l *SingleLockedMap[K, V]) Remove(k K, f func(V, bool) error) (bool, error)

func (*SingleLockedMap[K, V]) RemoveValue

func (l *SingleLockedMap[K, V]) RemoveValue(k K) bool

func (*SingleLockedMap[K, V]) Set

func (l *SingleLockedMap[K, V]) Set(k K, f func(_ V, found bool) (V, error)) (v V, created bool, _ error)

func (*SingleLockedMap[K, V]) SetOrRemove

func (l *SingleLockedMap[K, V]) SetOrRemove(
	k K,
	f func(V, bool) (v V, _ bool, _ error),
) (v V, _ bool, _ bool, _ error)

func (*SingleLockedMap[K, V]) SetValue

func (l *SingleLockedMap[K, V]) SetValue(k K, v V) (added bool)

func (*SingleLockedMap[K, V]) Traverse

func (l *SingleLockedMap[K, V]) Traverse(f func(K, V) bool) bool

func (*SingleLockedMap[K, V]) Value

func (l *SingleLockedMap[K, V]) Value(k K) (v V, found bool)

type StreamDecoder

type StreamDecoder interface {
	Decode(interface{}) error
}

func NewJSONStreamDecoder

func NewJSONStreamDecoder(r io.Reader) StreamDecoder

type StreamEncoder

type StreamEncoder interface {
	Encode(interface{}) error
}

func NewJSONStreamEncoder

func NewJSONStreamEncoder(w io.Writer) StreamEncoder

type Stringer

type Stringer func() string

func (Stringer) String

func (s Stringer) String() string

type TimerID

type TimerID string

func (TimerID) String

func (ti TimerID) String() string

type ULIDPool

type ULIDPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewULIDPool

func NewULIDPool() *ULIDPool

func (*ULIDPool) New

func (u *ULIDPool) New() ulid.ULID

type Version

type Version struct {
	// contains filtered or unexported fields
}

func EnsureParseVersion

func EnsureParseVersion(s string) Version

EnsureParseVersion tries to parse version string, but skips to check IsValid().

func MustNewVersion

func MustNewVersion(s string) Version

func ParseVersion

func ParseVersion(s string) (Version, error)

ParseVersion tries to parse version string and also checks IsValid().

func (Version) Compare

func (v Version) Compare(b Version) int

func (Version) IsCompatible

func (v Version) IsCompatible(b Version) bool

IsCompatible checks if the given version is compatible to the target. The compatible conditions are major matches.

func (Version) IsEmpty

func (v Version) IsEmpty() bool

func (Version) IsValid

func (v Version) IsValid([]byte) error

func (Version) Major

func (v Version) Major() uint64

func (Version) MarshalText

func (v Version) MarshalText() ([]byte, error)

func (Version) Minor

func (v Version) Minor() uint64

func (Version) Patch

func (v Version) Patch() uint64

func (Version) Prerelease

func (v Version) Prerelease() string

func (Version) String

func (v Version) String() string

func (*Version) UnmarshalText

func (v *Version) UnmarshalText(b []byte) error

type YAMLOrderedMap

type YAMLOrderedMap struct {
	// contains filtered or unexported fields
}

func NewYAMLOrderedMap

func NewYAMLOrderedMap() *YAMLOrderedMap

func (*YAMLOrderedMap) Clear

func (m *YAMLOrderedMap) Clear()

func (*YAMLOrderedMap) Delete

func (m *YAMLOrderedMap) Delete(k string) (removed bool)

func (*YAMLOrderedMap) Get

func (m *YAMLOrderedMap) Get(k string) (_ interface{}, found bool)

func (*YAMLOrderedMap) Len

func (m *YAMLOrderedMap) Len() int

func (*YAMLOrderedMap) MarshalYAML

func (m *YAMLOrderedMap) MarshalYAML() (interface{}, error)

func (*YAMLOrderedMap) Set

func (m *YAMLOrderedMap) Set(k string, v interface{}) (isnew bool)

func (*YAMLOrderedMap) Traverse

func (m *YAMLOrderedMap) Traverse(f func(k string, v interface{}) bool)

func (*YAMLOrderedMap) UnmarshalYAML

func (m *YAMLOrderedMap) UnmarshalYAML(y *yaml.Node) error

Directories

Path Synopsis
Package encoder supports to encode and decode structs in multiple formats.
Package encoder supports to encode and decode structs in multiple formats.
json
Package jsonenc support encoding and decoding by json.
Package jsonenc support encoding and decoding by json.
Package fixedtree provides tree structures.
Package fixedtree provides tree structures.
Package hint provides to handle structs and data with type and it's version.
Package hint provides to handle structs and data with type and it's version.
Package localtime provides time related funcs.
Package localtime provides time related funcs.
Package logging supports the consistent logging for modules.
Package logging supports the consistent logging for modules.
Package ps is processes
Package ps is processes
Package valuehash provides valuehashes.
Package valuehash provides valuehashes.

Jump to

Keyboard shortcuts

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