mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-05 16:28:17 +00:00
Compare commits
3 Commits
main
...
Xe/seriali
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2cde94b71 | ||
|
|
f56c242167 | ||
|
|
a75d9066fd |
@@ -19,7 +19,7 @@ type Impl[K comparable, V any] struct {
|
||||
// stopCh stops the background cleanup worker.
|
||||
stopCh chan struct{}
|
||||
wg sync.WaitGroup
|
||||
lock sync.RWMutex
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
type decayMapEntry[V any] struct {
|
||||
@@ -64,26 +64,28 @@ func (m *Impl[K, V]) expire(key K) bool {
|
||||
|
||||
// Delete a value from the DecayMap by key.
|
||||
//
|
||||
// This defers deletions to a background thread for performance reasons.
|
||||
//
|
||||
// If the value does not exist, return false. Return true after
|
||||
// deletion.
|
||||
func (m *Impl[K, V]) Delete(key K) bool {
|
||||
// Use a single write lock to avoid RUnlock->Lock convoy.
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
_, ok := m.data[key]
|
||||
if ok {
|
||||
delete(m.data, key)
|
||||
select {
|
||||
// Defer decay deletion to the background worker to avoid convoy.
|
||||
case m.deleteCh <- deleteReq[K]{key: key, expiry: time.Now().Add(-1 * time.Second)}:
|
||||
return m.expire(key)
|
||||
default:
|
||||
// Channel full: drop request; a future Cleanup() or Get will retry.
|
||||
return true
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// Get gets a value from the DecayMap by key.
|
||||
//
|
||||
// If a value has expired, forcibly delete it if it was not updated.
|
||||
func (m *Impl[K, V]) Get(key K) (V, bool) {
|
||||
m.lock.RLock()
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
value, ok := m.data[key]
|
||||
m.lock.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return Zilch[V](), false
|
||||
@@ -129,8 +131,8 @@ func (m *Impl[K, V]) Cleanup() {
|
||||
|
||||
// Len returns the number of entries in the DecayMap.
|
||||
func (m *Impl[K, V]) Len() int {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
return len(m.data)
|
||||
}
|
||||
|
||||
@@ -146,7 +148,7 @@ func (m *Impl[K, V]) Close() {
|
||||
func (m *Impl[K, V]) cleanupWorker() {
|
||||
defer m.wg.Done()
|
||||
batch := make([]deleteReq[K], 0, 64)
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
ticker := time.NewTicker(15 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
flush := func() {
|
||||
|
||||
@@ -30,15 +30,7 @@ func TestImpl(t *testing.T) {
|
||||
t.Error("got value even though it was supposed to be expired")
|
||||
}
|
||||
|
||||
// Deletion of expired entries after Get is deferred to a background worker.
|
||||
// Assert it eventually disappears from the map.
|
||||
deadline := time.Now().Add(200 * time.Millisecond)
|
||||
for time.Now().Before(deadline) {
|
||||
if dm.Len() == 0 {
|
||||
break
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
dm.Cleanup()
|
||||
if dm.Len() != 0 {
|
||||
t.Fatalf("expected background cleanup to remove expired key; len=%d", dm.Len())
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
- The memory store now decays values every 15 minutes instead of every 10 milliseconds.
|
||||
- Add Polish locale ([#1292](https://github.com/TecharoHQ/anubis/pull/1309))
|
||||
|
||||
<!-- This changes the project to: -->
|
||||
|
||||
@@ -27,7 +27,7 @@ type impl struct {
|
||||
}
|
||||
|
||||
func (i *impl) Delete(_ context.Context, key string) error {
|
||||
if !i.store.Delete(key) {
|
||||
if _, ok := i.store.Get(key); !ok {
|
||||
return fmt.Errorf("%w: %q", store.ErrNotFound, key)
|
||||
}
|
||||
|
||||
|
||||
@@ -57,10 +57,6 @@ func Common(t *testing.T, f store.Factory, config json.RawMessage) {
|
||||
t.Error("wanted test to not exist in store but it exists anyways")
|
||||
}
|
||||
|
||||
if err := s.Delete(t.Context(), t.Name()); err == nil {
|
||||
t.Errorf("key %q does not exist and Delete did not return non-nil", t.Name())
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
@@ -83,7 +79,6 @@ func Common(t *testing.T, f store.Factory, config json.RawMessage) {
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if err := tt.doer(t, s); !errors.Is(err, tt.err) {
|
||||
t.Logf("want: %v", tt.err)
|
||||
t.Logf("got: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user