syncx

package
v0.11.32 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2025 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-05 15:08:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-09 15:07:15 * @FilePath: \go-toolbox\pkg\syncx\atomic.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-05 15:27:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-05 15:27:15 * @FilePath: \go-toolbox\pkg\syncx\clone.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-12-13 13:05:03 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-09 13:23:29 * @FilePath: \go-toolbox\pkg\syncx\lock.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2023-07-28 00:50:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-15 11:55:06 * @FilePath: \go-toolbox\pkg\syncx\map.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-10-24 11:25:16 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-17 13:03:15 * @FilePath: \go-toolbox\pkg\syncx\pool.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-09 01:15:05 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 13:08:58 * @FilePath: \go-toolbox\pkg\syncx\set.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-08 13:55:22 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-09 18:25:16 * @FilePath: \go-toolbox\pkg\syncx\wg.go * @Description: 自定义的 WaitGroup 结构体,封装了 sync.WaitGroup,用于管理并发操作的等待 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyMeta

func CopyMeta(src, dst map[string]string)

CopyMeta 复制 src 中的所有键值对到 dst 中。 如果 dst 为 nil,则不进行任何操作。

func DeepCopy added in v0.11.27

func DeepCopy(dst, src interface{}) error

DeepCopy 复制源值到目标值

@params dst: 目标值的指针,表示要将源值复制到的位置。必须是一个指向某种类型的指针。 @params src: 源值的指针,表示要复制的原始数据。也必须是一个指向某种类型的指针。

@return:

如果成功,返回 nil;如果源值为 nil,返回一个错误。

func MetaMapToString

func MetaMapToString(meta map[string]string) string

MetaMapToString 将 map 转换为 meta 字符串

func MetaStringToMap

func MetaStringToMap(meta string) map[string]string

MetaStringToMap 将 meta 字符串转换为键值对的 map

func WithLock added in v0.11.20

func WithLock(lock Locker, operation func())

WithLock 是一个通用的函数,用于在给定的锁上执行操作

func WithLockReturn added in v0.11.23

func WithLockReturn[T any](lock Locker, operation func() (T, error)) (T, error)

WithLockReturn 是一个支持返回值的函数,用于在给定的锁上执行操作

func WithLockReturnValue added in v0.11.29

func WithLockReturnValue[T any](lock Locker, operation func() T) T

WithLockReturnValue 是一个支持返回值的函数,用于在给定的锁上执行操作,不返回错误

func WithRLock added in v0.11.28

func WithRLock(lock RLocker, operation func())

WithRLock 是一个用于在给定的读锁上执行操作的函数

func WithRLockReturn added in v0.11.28

func WithRLockReturn[T any](lock RLocker, operation func() (T, error)) (T, error)

WithRLockReturn 是一个支持返回值的函数,用于在给定的读锁上执行操作

func WithRLockReturnValue added in v0.11.29

func WithRLockReturnValue[T any](lock RLocker, operation func() T) T

WithRLockReturnValue 是一个支持返回值的函数,用于在给定的读锁上执行操作,不返回错误

Types

type Bool added in v0.11.29

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

Bool 类型,使用 atomic.Int32 来表示布尔值

func NewBool added in v0.11.29

func NewBool(b bool) *Bool

NewBool 创建一个新的 Bool 实例

func (*Bool) CAS added in v0.11.29

func (b *Bool) CAS(old, new bool) bool

CAS 原子地比较并交换布尔值

func (*Bool) Load added in v0.11.29

func (b *Bool) Load() bool

Load 原子地加载布尔值

func (*Bool) Store added in v0.11.29

func (b *Bool) Store(val bool)

Store 原子地存储布尔值

func (*Bool) Toggle added in v0.11.29

func (b *Bool) Toggle() bool

Toggle 原子地切换布尔值

type Int32 added in v0.11.29

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

Int32 类型,使用 atomic.Int32 来表示整数

func NewInt32 added in v0.11.29

func NewInt32(i int32) *Int32

NewInt32 创建一个新的 Int32 实例

func (*Int32) Add added in v0.11.29

func (i32 *Int32) Add(i int32) int32

Add 原子地增加值

func (*Int32) CAS added in v0.11.29

func (i32 *Int32) CAS(old, new int32) bool

CAS 原子地比较并交换值

func (*Int32) Load added in v0.11.29

func (i32 *Int32) Load() int32

Load 原子地加载值

func (*Int32) Store added in v0.11.29

func (i32 *Int32) Store(i int32)

Store 原子地存储值

func (*Int32) String added in v0.11.29

func (i32 *Int32) String() string

String 返回值的字符串表示

func (*Int32) Sub added in v0.11.29

func (i32 *Int32) Sub(i int32) int32

Sub 原子地减少值

func (*Int32) Swap added in v0.11.29

func (i32 *Int32) Swap(i int32) int32

Swap 原子地交换值

type Int64 added in v0.11.29

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

Int64 类型,使用 atomic.Int64 来表示整数

func NewInt64 added in v0.11.29

func NewInt64(i int64) *Int64

NewInt64 创建一个新的 Int64 实例

func (*Int64) Add added in v0.11.29

func (i64 *Int64) Add(i int64) int64

Add 原子地增加值

func (*Int64) CAS added in v0.11.29

func (i64 *Int64) CAS(old, new int64) bool

CAS 原子地比较并交换值

func (*Int64) Load added in v0.11.29

func (i64 *Int64) Load() int64

Load 原子地加载值

func (*Int64) Store added in v0.11.29

func (i64 *Int64) Store(i int64)

Store 原子地存储值

func (*Int64) String added in v0.11.29

func (i64 *Int64) String() string

String 返回值的字符串表示

func (*Int64) Sub added in v0.11.29

func (i64 *Int64) Sub(i int64) int64

Sub 原子地减少值

func (*Int64) Swap added in v0.11.29

func (i64 *Int64) Swap(i int64) int64

Swap 原子地交换值

type LimitedPool added in v0.11.31

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

LimitedPool 管理多个 levelPool,以支持不同大小的字节切片

func NewLimitedPool added in v0.11.31

func NewLimitedPool(minSize, maxSize int) *LimitedPool

NewLimitedPool 创建一个新的 LimitedPool,指定最小和最大大小

func (*LimitedPool) Get added in v0.11.31

func (p *LimitedPool) Get(size int) *[]byte

Get 从池中获取指定大小的字节切片

func (*LimitedPool) Put added in v0.11.31

func (p *LimitedPool) Put(b *[]byte)

Put 将字节切片放回池中以供重用

type Locker added in v0.11.20

type Locker interface {
	Lock()
	Unlock()
}

Locker 是一个接口,定义了锁定和解锁的方法

type Map

type Map[K comparable, V comparable] struct {
	// contains filtered or unexported fields
}

Map 是一个线程安全的映射,使用泛型 K 和 V。

func NewMap

func NewMap[K comparable, V comparable]() *Map[K, V]

NewMap 创建一个新的 Map 实例。

func (*Map[K, V]) Clear added in v0.11.6

func (m *Map[K, V]) Clear()

Clear 清空 Map 中的所有键值对

func (*Map[K, V]) Clone added in v0.11.15

func (m *Map[K, V]) Clone() *Map[K, V]

Clone 克隆当前 Map 实例,返回一个新的 Map 实例

func (*Map[K, V]) CompareAndDelete

func (m *Map[K, V]) CompareAndDelete(key K, value V) bool

CompareAndDelete 比较指定键的值,如果相等则删除该键的值。

func (*Map[K, V]) CompareAndSwap

func (m *Map[K, V]) CompareAndSwap(key K, old, new V) bool

CompareAndSwap 比较指定键的现有值,如果相等则将其替换为新值。

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete 删除指定键的值。

func (*Map[K, V]) Equals

func (m *Map[K, V]) Equals(key K, value V, cmpFunc func(existing V) bool) bool

Equals 函数用于比较两个键值对是否相等, 需要用户提供自定义的比较函数。

func (*Map[K, V]) Keys added in v0.11.6

func (m *Map[K, V]) Keys() []K

Keys 返回所有键的切片

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (V, bool)

Load 获取指定键的值。

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (V, bool)

LoadAndDelete 方法从 Map 中加载并删除指定键的值

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, new V) (V, bool)

LoadOrStore 获取指定键的值,如果不存在则存储新值。

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(run func(key K, value V) bool)

Range 遍历 Map 中的所有键值对。

func (*Map[K, V]) Size added in v0.11.6

func (m *Map[K, V]) Size() int

Size 返回当前 Map 中元素的数量

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

Store 设置指定键的值。

func (*Map[K, V]) Swap

func (m *Map[K, V]) Swap(key K, value V) (pre V, ok bool)

Swap 替换指定键的值,并返回之前的值。

func (*Map[K, V]) Values added in v0.11.6

func (m *Map[K, V]) Values() []V

Values 返回所有值的切片

type RLocker added in v0.11.28

type RLocker interface {
	RLock()
	RUnlock()
}

RLocker 是一个接口,定义了读锁和解锁的方法

type Set added in v0.11.6

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

Set 是一个线程安全的集合,使用 Map 实现。

func NewSet added in v0.11.6

func NewSet[K comparable]() *Set[K]

NewSet 创建一个新的 Set 实例。

func (*Set[K]) Add added in v0.11.6

func (s *Set[K]) Add(key K)

Add 向集合中添加一个元素。

func (*Set[K]) AddAll added in v0.11.6

func (s *Set[K]) AddAll(keys ...K)

AddAll 向集合中添加多个元素。

func (*Set[K]) Clear added in v0.11.6

func (s *Set[K]) Clear()

Clear 清空集合中的所有元素。

func (*Set[K]) Delete added in v0.11.6

func (s *Set[K]) Delete(key K)

Delete 从集合中删除指定的元素。

func (*Set[K]) DeleteAll added in v0.11.6

func (s *Set[K]) DeleteAll(keys ...K)

DeleteAll 从集合中删除多个元素。

func (*Set[K]) Elements added in v0.11.6

func (s *Set[K]) Elements() []K

Elements 返回集合中的所有元素。

func (*Set[K]) Has added in v0.11.6

func (s *Set[K]) Has(key K) bool

Has 检查集合中是否存在指定的元素。

func (*Set[K]) HasAll added in v0.11.6

func (s *Set[K]) HasAll(keys ...K) (existing []K, all bool)

HasAll 检查集合中是否包含所有指定的元素。

func (*Set[K]) IsEmpty added in v0.11.6

func (s *Set[K]) IsEmpty() bool

IsEmpty 检查集合是否为空。

func (*Set[K]) Size added in v0.11.6

func (s *Set[K]) Size() int

Size 返回集合中元素的数量。

type Uint32 added in v0.11.29

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

Uint32 类型,使用 atomic.Uint32 来表示无符号整数

func NewUint32 added in v0.11.29

func NewUint32(i uint32) *Uint32

NewUint32 创建一个新的 Uint32 实例

func (*Uint32) Add added in v0.11.29

func (u32 *Uint32) Add(i uint32) uint32

Add 原子地增加值

func (*Uint32) CAS added in v0.11.29

func (u32 *Uint32) CAS(old, new uint32) bool

CAS 原子地比较并交换值

func (*Uint32) Load added in v0.11.29

func (u32 *Uint32) Load() uint32

Load 原子地加载值

func (*Uint32) Store added in v0.11.29

func (u32 *Uint32) Store(i uint32)

Store 原子地存储值

func (*Uint32) String added in v0.11.29

func (u32 *Uint32) String() string

String 返回值的字符串表示

func (*Uint32) Sub added in v0.11.29

func (u32 *Uint32) Sub(i uint32) uint32

Sub 原子地减少值

func (*Uint32) Swap added in v0.11.29

func (u32 *Uint32) Swap(i uint32) uint32

Swap 原子地交换值

type Uint64 added in v0.11.29

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

Uint64 类型,使用 atomic.Uint64 来表示无符号整数

func NewUint64 added in v0.11.29

func NewUint64(i uint64) *Uint64

NewUint64 创建一个新的 Uint64 实例

func (*Uint64) Add added in v0.11.29

func (u64 *Uint64) Add(i uint64) uint64

Add 原子地增加值

func (*Uint64) CAS added in v0.11.29

func (u64 *Uint64) CAS(old, new uint64) bool

CAS 原子地比较并交换值

func (*Uint64) Load added in v0.11.29

func (u64 *Uint64) Load() uint64

Load 原子地加载值

func (*Uint64) Store added in v0.11.29

func (u64 *Uint64) Store(i uint64)

Store 原子地存储值

func (*Uint64) String added in v0.11.29

func (u64 *Uint64) String() string

String 返回值的字符串表示

func (*Uint64) Sub added in v0.11.29

func (u64 *Uint64) Sub(i uint64) uint64

Sub 原子地减少值

func (*Uint64) Swap added in v0.11.29

func (u64 *Uint64) Swap(i uint64) uint64

Swap 原子地交换值

type Uintptr added in v0.11.29

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

Uintptr 类型,使用 atomic.Uintptr 来表示无符号指针

func NewUintptr added in v0.11.29

func NewUintptr(i uintptr) *Uintptr

NewUintptr 创建一个新的 Uintptr 实例

func (*Uintptr) CAS added in v0.11.29

func (ptr *Uintptr) CAS(old, new uintptr) bool

CAS 原子地比较并交换值

func (*Uintptr) Load added in v0.11.29

func (ptr *Uintptr) Load() uintptr

Load 原子地加载值

func (*Uintptr) Store added in v0.11.29

func (ptr *Uintptr) Store(i uintptr)

Store 原子地存储值

func (*Uintptr) String added in v0.11.29

func (ptr *Uintptr) String() string

String 返回值的字符串表示

func (*Uintptr) Swap added in v0.11.29

func (ptr *Uintptr) Swap(i uintptr) uintptr

Swap 原子地交换值

type WaitGroup added in v0.11.29

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

WaitGroup 是一个自定义的等待组,支持并发操作并捕获错误

func NewWaitGroup added in v0.11.29

func NewWaitGroup(catchPanic bool, maxChannel ...uint) *WaitGroup

NewWaitGroup 创建一个新的 WaitGroup,支持最大并发数量

params:

  • catchPanic: 是否捕获 goroutine 中的 panic
  • maxChannel: 可选参数,最大并发数量

return:

  • *WaitGroup: 返回初始化后的 WaitGroup 实例

func (*WaitGroup) Add added in v0.11.29

func (h *WaitGroup) Add(delta int)

Add 增加等待计数

params:

  • delta: 增加的计数值,通常为 1

func (*WaitGroup) Done added in v0.11.29

func (h *WaitGroup) Done()

Done 减少等待计数 每当一个 goroutine 完成时调用此方法

func (*WaitGroup) GetChannelSize added in v0.11.29

func (wg *WaitGroup) GetChannelSize() int

GetChannelSize 返回当前通道的可用容量

return:

  • int: 当前通道的可用容量

func (*WaitGroup) GetError added in v0.11.29

func (wg *WaitGroup) GetError() error

GetError 返回错误,如果没有错误则返回 nil

return:

  • error: 捕获的错误,如果没有则返回 nil

func (*WaitGroup) Go added in v0.11.29

func (h *WaitGroup) Go(f func())

Go 启动一个新的 goroutine,并根据需要处理 panic

params:

  • f: 要执行的函数

func (*WaitGroup) SetError added in v0.11.29

func (wg *WaitGroup) SetError(err error)

SetError 设置错误

params:

  • err: 要设置的错误,如果为 nil 则不进行设置

func (*WaitGroup) Wait added in v0.11.29

func (h *WaitGroup) Wait() error

Wait 等待所有 goroutine 完成并返回任何捕获的错误

return:

  • error: 如果没有错误发生,则返回 nil

Jump to

Keyboard shortcuts

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