common

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GlobalPoolMigrationManager = NewPoolMigrationManager()
	GlobalSmartStringPool      = NewSmartStringBuilderPool()
	GlobalBufferAdapter        = NewBufferPoolAdapter()
)

全局实例,便于系统集成

View Source
var GlobalTieredPools = NewTieredPools()

全局分级池实例

Functions

func AnyValueToString

func AnyValueToString(v slog.Value) string

func AppendAttrsToGroup

func AppendAttrsToGroup(groups []string, actualAttrs []slog.Attr, newAttrs ...slog.Attr) []slog.Attr

func AppendRecordAttrsToAttrs

func AppendRecordAttrsToAttrs(attrs []slog.Attr, groups []string, record *slog.Record) []slog.Attr

func AttrToValue

func AttrToValue(attr slog.Attr) (string, any)

func AttrsToMap

func AttrsToMap(attrs ...slog.Attr) map[string]any

func AttrsToString

func AttrsToString(attrs ...slog.Attr) map[string]string

func Contains

func Contains[T comparable](collection []T, element T) bool

Contains returns true if an element is present in a collection.

func ContextExtractor

func ContextExtractor(ctx context.Context, fns []func(ctx context.Context) []slog.Attr) []slog.Attr

func ExtractError

func ExtractError(attrs []slog.Attr, errorKeys ...string) ([]slog.Attr, error)

func ExtractFromContext

func ExtractFromContext(keys ...any) func(ctx context.Context) []slog.Attr

func FilterMap

func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R

FilterMap returns a slice which obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/-AuYXfy7opz

func FindAttrByGroupAndKey

func FindAttrByGroupAndKey(attrs []slog.Attr, groups []string, key string) (slog.Attr, bool)

func FindAttrByKey

func FindAttrByKey(attrs []slog.Attr, key string) (slog.Attr, bool)

func FindAttribute

func FindAttribute(attrs []slog.Attr, groups []string, key string) (slog.Attr, bool)

func FormatError

func FormatError(err error) map[string]any

func FormatErrorKey

func FormatErrorKey(values map[string]any, errorKeys ...string) map[string]any

func FormatRequest

func FormatRequest(req *http.Request, ignoreHeaders bool) map[string]any

func GetLargeStringBuilder

func GetLargeStringBuilder() *strings.Builder

GetLargeStringBuilder 获取大容量字符串构建器

func GetMediumStringBuilder

func GetMediumStringBuilder() *strings.Builder

GetMediumStringBuilder 获取中容量字符串构建器

func GetSmallStringBuilder

func GetSmallStringBuilder() *strings.Builder

GetSmallStringBuilder 获取小容量字符串构建器

func GetSmartStringBuilder

func GetSmartStringBuilder() *strings.Builder

GetSmartStringBuilder 获取智能字符串构建器

func Map

func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R

Map manipulates a slice and transforms it to a slice of another type. Play: https://go.dev/play/p/OkPcYAhBo0D

func MapEntries

func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2

MapEntries manipulates a map entries and transforms it to a map of another type. Play: https://go.dev/play/p/VuvNQzxKimT

func MapToSlice

func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []R

MapToSlice transforms a map into a slice based on specific iteratee Play: https://go.dev/play/p/ZuiCZpDt6LD

func PutBuffer

func PutBuffer(buffer *TieredBuffer)

PutBuffer 放回buffer到全局池

func PutSmartStringBuilder

func PutSmartStringBuilder(builder *strings.Builder)

PutSmartStringBuilder 放回智能字符串构建器

func PutStringBuilder

func PutStringBuilder(builder *strings.Builder)

PutStringBuilder 放回字符串构建器到全局池

func ReleaseBuffer

func ReleaseBuffer(buffer *TieredBuffer)

ReleaseBuffer 释放适配的buffer

func RemoveEmptyAttrs

func RemoveEmptyAttrs(attrs []slog.Attr) []slog.Attr

func ReplaceAttrs

func ReplaceAttrs(fn ReplaceAttrFn, groups []string, attrs ...slog.Attr) []slog.Attr

func ReplaceError

func ReplaceError(attrs []slog.Attr, errorKeys ...string) []slog.Attr

func Source

func Source(sourceKey string, r *slog.Record) slog.Attr

func StringSource

func StringSource(sourceKey string, r *slog.Record) slog.Attr

func ToAnySlice

func ToAnySlice[T any](collection []T) []any

ToAnySlice returns a slice with all elements mapped to `any` type

func UniqAttrs

func UniqAttrs(attrs []slog.Attr) []slog.Attr

@TODO: should be recursive

func ValueToString

func ValueToString(v slog.Value) string

Types

type BufferPool

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

BufferPool 单级buffer池

func NewBufferPool

func NewBufferPool(initSize, maxSize int, stats *PoolStats) *BufferPool

NewBufferPool 创建buffer池

type BufferPoolAdapter

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

BufferPoolAdapter 将现有buffer池适配到分级池

func NewBufferPoolAdapter

func NewBufferPoolAdapter() *BufferPoolAdapter

NewBufferPoolAdapter 创建buffer池适配器

func (*BufferPoolAdapter) AdaptExistingBuffer

func (bpa *BufferPoolAdapter) AdaptExistingBuffer(data []byte) *TieredBuffer

AdaptExistingBuffer 适配现有的buffer到分级池系统

func (*BufferPoolAdapter) GetAdapterStats

func (bpa *BufferPoolAdapter) GetAdapterStats() (gets, puts int64)

GetAdapterStats 获取适配器统计

func (*BufferPoolAdapter) ReleaseAdaptedBuffer

func (bpa *BufferPoolAdapter) ReleaseAdaptedBuffer(buffer *TieredBuffer)

ReleaseAdaptedBuffer 释放适配的buffer

type BufferPoolStats

type BufferPoolStats struct {
	Gets     int64   `json:"gets"`      // 获取次数
	Puts     int64   `json:"puts"`      // 放回次数
	News     int64   `json:"news"`      // 新建次数
	Discards int64   `json:"discards"`  // 丢弃次数
	PoolSize int     `json:"pool_size"` // 当前池大小
	HitRate  float64 `json:"hit_rate"`  // 命中率
}

BufferPoolStats 对象池统计信息

type BufferSize

type BufferSize int

BufferSize 定义buffer大小级别

const (
	SmallBuffer  BufferSize = iota // 小buffer: 256B - 2KB
	MediumBuffer                   // 中buffer: 2KB - 8KB
	LargeBuffer                    // 大buffer: 8KB - 32KB
)

type CacheEntry

type CacheEntry struct {
	Key   interface{} // 缓存键
	Value interface{} // 缓存值
}

CacheEntry 缓存条目

type LRUCache

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

LRUCache LRU (Least Recently Used) 缓存实现 提供线程安全的LRU缓存,支持自动淘汰最久未使用的条目

func NewLRUCache

func NewLRUCache(capacity int) *LRUCache

NewLRUCache 创建新的LRU缓存

func NewLRUCacheWithOptions

func NewLRUCacheWithOptions(options ...LRUCacheOption) *LRUCache

NewLRUCacheWithOptions 使用选项创建LRU缓存

func (*LRUCache) Capacity

func (lru *LRUCache) Capacity() int

Capacity 获取缓存容量

func (*LRUCache) Clear

func (lru *LRUCache) Clear()

Clear 清空所有缓存

func (*LRUCache) Contains

func (lru *LRUCache) Contains(key interface{}) bool

Contains 检查是否包含指定键

func (*LRUCache) Get

func (lru *LRUCache) Get(key interface{}) (interface{}, bool)

Get 获取缓存值

func (*LRUCache) GetKeys

func (lru *LRUCache) GetKeys() []interface{}

GetKeys 获取所有键(按访问顺序,从最新到最旧)

func (*LRUCache) GetMostRecentKey

func (lru *LRUCache) GetMostRecentKey() interface{}

GetMostRecentKey 获取最近使用的键(用于调试)

func (*LRUCache) GetOldestKey

func (lru *LRUCache) GetOldestKey() interface{}

GetOldestKey 获取最久未使用的键(用于调试)

func (*LRUCache) GetStats

func (lru *LRUCache) GetStats() LRUCacheStats

GetStats 获取缓存统计信息

func (*LRUCache) Peek

func (lru *LRUCache) Peek(key interface{}) (interface{}, bool)

Peek 查看缓存值但不更新访问顺序

func (*LRUCache) Put

func (lru *LRUCache) Put(key interface{}, value interface{})

Put 设置缓存值

func (*LRUCache) Remove

func (lru *LRUCache) Remove(key interface{}) bool

Remove 移除指定键的缓存

func (*LRUCache) SetCapacity

func (lru *LRUCache) SetCapacity(capacity int)

SetCapacity 设置缓存容量

func (*LRUCache) Size

func (lru *LRUCache) Size() int

Size 获取当前缓存大小

type LRUCacheOption

type LRUCacheOption func(*LRUCache)

LRUCacheOption LRU缓存配置选项

func WithCapacity

func WithCapacity(capacity int) LRUCacheOption

WithCapacity 设置缓存容量

type LRUCacheStats

type LRUCacheStats struct {
	Size     int     `json:"size"`     // 当前大小
	Capacity int     `json:"capacity"` // 最大容量
	Hits     int64   `json:"hits"`     // 命中次数
	Misses   int64   `json:"misses"`   // 未命中次数
	HitRate  float64 `json:"hit_rate"` // 命中率
}

LRUCacheStats LRU缓存统计信息

type LRUStringCache

type LRUStringCache struct {
	*LRUCache
}

LRUStringCache 字符串特化的LRU缓存 针对字符串键值进行优化的LRU缓存实现

func NewLRUStringCache

func NewLRUStringCache(capacity int) *LRUStringCache

NewLRUStringCache 创建字符串LRU缓存

func (*LRUStringCache) GetString

func (lsc *LRUStringCache) GetString(key string) (string, bool)

GetString 获取字符串值

func (*LRUStringCache) GetStringKeys

func (lsc *LRUStringCache) GetStringKeys() []string

GetStringKeys 获取所有字符串键

func (*LRUStringCache) PutString

func (lsc *LRUStringCache) PutString(key string, value string)

PutString 设置字符串值

type PoolMigrationManager

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

PoolMigrationManager 对象池迁移管理器 帮助逐步从传统sync.Pool迁移到分级对象池

func NewPoolMigrationManager

func NewPoolMigrationManager() *PoolMigrationManager

NewPoolMigrationManager 创建池迁移管理器

func (*PoolMigrationManager) MigrateStringBuilderPool

func (pm *PoolMigrationManager) MigrateStringBuilderPool(oldPool *sync.Pool) *TieredStringBuilderPool

MigrateStringBuilderPool 将字符串构建器池迁移到分级池 这个函数可以替代现有的sync.Pool使用

type PoolStats

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

PoolStats 池统计信息

func (*PoolStats) Discards

func (ps *PoolStats) Discards() int64

Discards 获取"丢弃"操作次数

func (*PoolStats) Gets

func (ps *PoolStats) Gets() int64

Gets 获取"获取"操作次数

func (*PoolStats) News

func (ps *PoolStats) News() int64

News 获取"新建"操作次数

func (*PoolStats) Puts

func (ps *PoolStats) Puts() int64

Puts 获取"放回"操作次数

type ReplaceAttrFn

type ReplaceAttrFn = func(groups []string, a slog.Attr) slog.Attr

type SmartStringBuilderPool

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

SmartStringBuilderPool 智能字符串构建器池 自动根据使用模式选择最佳的池大小

func NewSmartStringBuilderPool

func NewSmartStringBuilderPool() *SmartStringBuilderPool

NewSmartStringBuilderPool 创建智能字符串构建器池

func (*SmartStringBuilderPool) GetOptimal

func (ssp *SmartStringBuilderPool) GetOptimal() *strings.Builder

GetOptimal 根据使用历史获取最优大小的字符串构建器

func (*SmartStringBuilderPool) GetUsageStats

func (ssp *SmartStringBuilderPool) GetUsageStats() (small, medium, large int64)

GetUsageStats 获取使用统计

func (*SmartStringBuilderPool) PutWithUsageTracking

func (ssp *SmartStringBuilderPool) PutWithUsageTracking(builder *strings.Builder)

PutWithUsageTracking 放回字符串构建器并记录使用模式

type StringBuilderPool

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

StringBuilderPool 字符串构建器池

func NewStringBuilderPool

func NewStringBuilderPool(capacity int, stats *PoolStats) *StringBuilderPool

NewStringBuilderPool 创建字符串构建器池

type TieredBuffer

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

TieredBuffer 分级buffer

func AdaptBuffer

func AdaptBuffer(data []byte) *TieredBuffer

AdaptBuffer 适配现有buffer数据

func GetLargeBuffer

func GetLargeBuffer() *TieredBuffer

GetLargeBuffer 获取大buffer

func GetMediumBuffer

func GetMediumBuffer() *TieredBuffer

GetMediumBuffer 获取中buffer

func GetSmallBuffer

func GetSmallBuffer() *TieredBuffer

GetSmallBuffer 获取小buffer

func (*TieredBuffer) Bytes

func (tb *TieredBuffer) Bytes() []byte

Bytes 返回字节内容

func (*TieredBuffer) Cap

func (tb *TieredBuffer) Cap() int

Cap 返回容量

func (*TieredBuffer) Len

func (tb *TieredBuffer) Len() int

Len 返回当前长度

func (*TieredBuffer) Reset

func (tb *TieredBuffer) Reset()

Reset 重置buffer

func (*TieredBuffer) Size

func (tb *TieredBuffer) Size() BufferSize

Size 返回buffer大小级别

func (*TieredBuffer) String

func (tb *TieredBuffer) String() string

String 返回字符串内容

func (*TieredBuffer) Write

func (tb *TieredBuffer) Write(p []byte) (n int, err error)

Write 实现io.Writer接口

func (*TieredBuffer) WriteString

func (tb *TieredBuffer) WriteString(s string) (n int, err error)

WriteString 写入字符串

type TieredPools

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

TieredPools 分级对象池管理器

func NewTieredPools

func NewTieredPools() *TieredPools

NewTieredPools 创建分级对象池管理器

func (*TieredPools) GetBuffer

func (tp *TieredPools) GetBuffer(expectedSize int) *TieredBuffer

GetBuffer 根据期望大小获取最适合的buffer

func (*TieredPools) GetStats

func (tp *TieredPools) GetStats() map[BufferSize]BufferPoolStats

GetStats 获取所有池的统计信息

func (*TieredPools) GetStringBuilder

func (tp *TieredPools) GetStringBuilder(expectedCapacity int) *strings.Builder

GetStringBuilder 根据期望容量获取字符串构建器

func (*TieredPools) PutBuffer

func (tp *TieredPools) PutBuffer(buffer *TieredBuffer)

PutBuffer 将buffer放回对应的池

func (*TieredPools) PutStringBuilder

func (tp *TieredPools) PutStringBuilder(builder *strings.Builder, expectedCapacity int)

PutStringBuilder 将字符串构建器放回对应的池

type TieredStringBuilderPool

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

TieredStringBuilderPool 分级字符串构建器池包装器 提供与sync.Pool兼容的接口,但内部使用分级池

func MigrateStringPool

func MigrateStringPool(oldPool *sync.Pool) *TieredStringBuilderPool

MigrateStringPool 迁移字符串池的便捷函数

func (*TieredStringBuilderPool) Get

func (tsp *TieredStringBuilderPool) Get() interface{}

Get 获取字符串构建器(兼容sync.Pool接口)

func (*TieredStringBuilderPool) GetWithCapacity

func (tsp *TieredStringBuilderPool) GetWithCapacity(expectedCapacity int) *strings.Builder

GetWithCapacity 根据期望容量获取字符串构建器

func (*TieredStringBuilderPool) Put

func (tsp *TieredStringBuilderPool) Put(x interface{})

Put 放回字符串构建器(兼容sync.Pool接口)

func (*TieredStringBuilderPool) PutWithCapacity

func (tsp *TieredStringBuilderPool) PutWithCapacity(builder *strings.Builder, expectedCapacity int)

PutWithCapacity 根据期望容量放回字符串构建器

Jump to

Keyboard shortcuts

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