mirror of
https://github.com/charlienet/go-mixed.git
synced 2026-07-14 08:17:26 +08:00
c44a325384
locker: 导出 RWLocker/EmptyLocker 接口, 新增 WithRWLocker, 重构 SourceLocker 竞态、Synchronize 数据竞态 collections/linked_list: 补齐 ForEach/Size/GetAt/RemoveAt/ToList 等方法的锁保护 collections/queue,stack: Peek/Size/IsEmpty 加锁 collections/list/linked_list: Front/Back/GetAt 加锁 maps: rwlock_map.Exist/Count, hash_map.Count 加锁 delay_queue/mem_store: Pop 原子化、Len/IsEmpty 加锁 cleanup_guard: Run() 检查 enable 标志 sets/ketama: 修复接口变更导致的 nil 空指针
42 lines
618 B
Go
42 lines
618 B
Go
package ketama
|
|
|
|
import (
|
|
"github.com/charlienet/go-mixed/locker"
|
|
"github.com/charlienet/go-mixed/maps"
|
|
)
|
|
|
|
type Ketama struct {
|
|
mu locker.WithRWLocker
|
|
replicas int
|
|
m maps.Map[uint64, string]
|
|
}
|
|
|
|
func New() *Ketama {
|
|
return &Ketama{
|
|
m: maps.NewHashMap[uint64, string](),
|
|
}
|
|
}
|
|
|
|
func (k *Ketama) Synchronize() {
|
|
if k.mu == nil {
|
|
k.mu = &locker.RWLock{}
|
|
}
|
|
k.mu.Synchronize()
|
|
}
|
|
|
|
func (k *Ketama) Add(nodes ...string) {
|
|
k.mu.Lock()
|
|
defer k.mu.Unlock()
|
|
|
|
for _, node := range nodes {
|
|
_ = node
|
|
}
|
|
}
|
|
|
|
func (k *Ketama) IsEmpty() bool {
|
|
k.mu.RLock()
|
|
defer k.mu.RUnlock()
|
|
|
|
return k.m.Count() == 0
|
|
}
|