Documentation
¶
Index ¶
- Variables
- func AnyValueToString(v slog.Value) string
- func AppendAttrsToGroup(groups []string, actualAttrs []slog.Attr, newAttrs ...slog.Attr) []slog.Attr
- func AppendRecordAttrsToAttrs(attrs []slog.Attr, groups []string, record *slog.Record) []slog.Attr
- func AttrToValue(attr slog.Attr) (string, any)
- func AttrsToMap(attrs ...slog.Attr) map[string]any
- func AttrsToString(attrs ...slog.Attr) map[string]string
- func Contains[T comparable](collection []T, element T) bool
- func ContextExtractor(ctx context.Context, fns []func(ctx context.Context) []slog.Attr) []slog.Attr
- func ExtractError(attrs []slog.Attr, errorKeys ...string) ([]slog.Attr, error)
- func ExtractFromContext(keys ...any) func(ctx context.Context) []slog.Attr
- func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R
- func FindAttrByGroupAndKey(attrs []slog.Attr, groups []string, key string) (slog.Attr, bool)
- func FindAttrByKey(attrs []slog.Attr, key string) (slog.Attr, bool)
- func FindAttribute(attrs []slog.Attr, groups []string, key string) (slog.Attr, bool)
- func FormatError(err error) map[string]any
- func FormatErrorKey(values map[string]any, errorKeys ...string) map[string]any
- func FormatRequest(req *http.Request, ignoreHeaders bool) map[string]any
- func GetLargeStringBuilder() *strings.Builder
- func GetMediumStringBuilder() *strings.Builder
- func GetSmallStringBuilder() *strings.Builder
- func GetSmartStringBuilder() *strings.Builder
- func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R
- 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
- func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []R
- func PutBuffer(buffer *TieredBuffer)
- func PutSmartStringBuilder(builder *strings.Builder)
- func PutStringBuilder(builder *strings.Builder)
- func ReleaseBuffer(buffer *TieredBuffer)
- func RemoveEmptyAttrs(attrs []slog.Attr) []slog.Attr
- func ReplaceAttrs(fn ReplaceAttrFn, groups []string, attrs ...slog.Attr) []slog.Attr
- func ReplaceError(attrs []slog.Attr, errorKeys ...string) []slog.Attr
- func Source(sourceKey string, r *slog.Record) slog.Attr
- func StringSource(sourceKey string, r *slog.Record) slog.Attr
- func ToAnySlice[T any](collection []T) []any
- func UniqAttrs(attrs []slog.Attr) []slog.Attr
- func ValueToString(v slog.Value) string
- type BufferPool
- type BufferPoolAdapter
- type BufferPoolStats
- type BufferSize
- type CacheEntry
- type LRUCache
- func (lru *LRUCache) Capacity() int
- func (lru *LRUCache) Clear()
- func (lru *LRUCache) Contains(key interface{}) bool
- func (lru *LRUCache) Get(key interface{}) (interface{}, bool)
- func (lru *LRUCache) GetKeys() []interface{}
- func (lru *LRUCache) GetMostRecentKey() interface{}
- func (lru *LRUCache) GetOldestKey() interface{}
- func (lru *LRUCache) GetStats() LRUCacheStats
- func (lru *LRUCache) Peek(key interface{}) (interface{}, bool)
- func (lru *LRUCache) Put(key interface{}, value interface{})
- func (lru *LRUCache) Remove(key interface{}) bool
- func (lru *LRUCache) SetCapacity(capacity int)
- func (lru *LRUCache) Size() int
- type LRUCacheOption
- type LRUCacheStats
- type LRUStringCache
- type PoolMigrationManager
- type PoolStats
- type ReplaceAttrFn
- type SmartStringBuilderPool
- type StringBuilderPool
- type TieredBuffer
- func (tb *TieredBuffer) Bytes() []byte
- func (tb *TieredBuffer) Cap() int
- func (tb *TieredBuffer) Len() int
- func (tb *TieredBuffer) Reset()
- func (tb *TieredBuffer) Size() BufferSize
- func (tb *TieredBuffer) String() string
- func (tb *TieredBuffer) Write(p []byte) (n int, err error)
- func (tb *TieredBuffer) WriteString(s string) (n int, err error)
- type TieredPools
- func (tp *TieredPools) GetBuffer(expectedSize int) *TieredBuffer
- func (tp *TieredPools) GetStats() map[BufferSize]BufferPoolStats
- func (tp *TieredPools) GetStringBuilder(expectedCapacity int) *strings.Builder
- func (tp *TieredPools) PutBuffer(buffer *TieredBuffer)
- func (tp *TieredPools) PutStringBuilder(builder *strings.Builder, expectedCapacity int)
- type TieredStringBuilderPool
Constants ¶
This section is empty.
Variables ¶
var ( GlobalPoolMigrationManager = NewPoolMigrationManager() GlobalSmartStringPool = NewSmartStringBuilderPool() GlobalBufferAdapter = NewBufferPoolAdapter() )
全局实例,便于系统集成
var GlobalTieredPools = NewTieredPools()
全局分级池实例
Functions ¶
func AnyValueToString ¶
func AppendAttrsToGroup ¶
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 ExtractFromContext ¶
func FilterMap ¶
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.
func FindAttrByGroupAndKey ¶
func FindAttribute ¶
func FormatError ¶
func FormatErrorKey ¶
func GetLargeStringBuilder ¶
GetLargeStringBuilder 获取大容量字符串构建器
func GetMediumStringBuilder ¶
GetMediumStringBuilder 获取中容量字符串构建器
func GetSmallStringBuilder ¶
GetSmallStringBuilder 获取小容量字符串构建器
func GetSmartStringBuilder ¶
GetSmartStringBuilder 获取智能字符串构建器
func Map ¶
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 PutSmartStringBuilder ¶
PutSmartStringBuilder 放回智能字符串构建器
func PutStringBuilder ¶
PutStringBuilder 放回字符串构建器到全局池
func ReplaceAttrs ¶
func ToAnySlice ¶
ToAnySlice returns a slice with all elements mapped to `any` type
func ValueToString ¶
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 NewLRUCacheWithOptions ¶
func NewLRUCacheWithOptions(options ...LRUCacheOption) *LRUCache
NewLRUCacheWithOptions 使用选项创建LRU缓存
func (*LRUCache) GetMostRecentKey ¶
func (lru *LRUCache) GetMostRecentKey() interface{}
GetMostRecentKey 获取最近使用的键(用于调试)
func (*LRUCache) GetOldestKey ¶
func (lru *LRUCache) GetOldestKey() interface{}
GetOldestKey 获取最久未使用的键(用于调试)
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 池统计信息
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 (*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 (*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 根据期望容量放回字符串构建器