1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2023-08-25 15:31:00 +08:00
parent 04aecd4abc
commit b0a97978d8
58 changed files with 1330 additions and 476 deletions

38
ketama/ketama.go Normal file
View File

@ -0,0 +1,38 @@
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() {
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
}

32
ketama/ketama_test.go Normal file
View File

@ -0,0 +1,32 @@
package ketama_test
import (
"testing"
"github.com/charlienet/go-mixed/ketama"
)
func TestNew(t *testing.T) {
k := ketama.New()
t.Logf("%+v", k)
k.Synchronize()
// k.Lock()
t.Logf("%+v", k)
t.Log(k.IsEmpty())
t.Logf("%+v", k)
t.Log(k.IsEmpty())
t.Logf("%+v", k)
k.Synchronize()
t.Log(k.IsEmpty())
t.Logf("%+v", k)
t.Log(k.IsEmpty())
t.Logf("%+v", k)
t.Log(k.IsEmpty())
t.Logf("%+v", k)
}