Import Anubis v1.28.2

Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3).
Masyunis customizations are re-applied in the following commits.
This commit is contained in:
sol_bot Bot committed 2026-11-28 14:52:26 +03:00
1 parent eb3e46d8d0
commit e07de898cd
573 files changed
+35846 -6807

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
package memory
import (
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/TecharoHQ/anubis/lib/store"
)
func TestStoreCapacity(t *testing.T) {
st := New(t.Context())
for idx := range 10001 {
if err := st.Set(t.Context(), fmt.Sprint(idx), []byte("value"), time.Hour); err != nil {
t.Fatal(err)
}
}
if _, err := st.Get(t.Context(), "0"); !errors.Is(err, store.ErrNotFound) {
t.Fatalf("oldest key retained beyond capacity: %v", err)
}
if _, err := st.Get(t.Context(), "10000"); err != nil {
t.Fatal(err)
}
}
func TestStoreCapacityPreservesUpdates(t *testing.T) {
st := New(t.Context())
for range 10001 {
if err := st.Set(t.Context(), "same", []byte("value"), time.Hour); err != nil {
t.Fatal(err)
}
}
if _, err := st.Get(t.Context(), "same"); err != nil {
t.Fatal(err)
}
}
func TestCapacityWithConcurrentWrites(t *testing.T) {
st := New(t.Context()).(*impl)
var wg sync.WaitGroup
for worker := range 16 {
wg.Go(func() {
for idx := range 1000 {
if err := st.Set(t.Context(), fmt.Sprintf("%d-%d", worker, idx), []byte("value"), time.Hour); err != nil {
t.Error(err)
}
}
})
}
wg.Wait()
if got := st.store.Len(); got > maxEntries {
t.Fatalf("concurrent writes exceeded capacity: %d", got)
}
}
+25 -2
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/TecharoHQ/anubis/decaymap"
@@ -22,11 +23,20 @@ func init() {
store.Register("memory", factory{})
}
const maxEntries = 10000
type impl struct {
store *decaymap.Impl[string, []byte]
lock sync.Mutex
slots [maxEntries]string
positions map[string]int
next int
store *decaymap.Impl[string, []byte]
}
func (i *impl) Delete(_ context.Context, key string) error {
i.lock.Lock()
defer i.lock.Unlock()
delete(i.positions, key)
if !i.store.Delete(key) {
return fmt.Errorf("%w: %q", store.ErrNotFound, key)
}
@@ -44,6 +54,18 @@ func (i *impl) Get(_ context.Context, key string) ([]byte, error) {
}
func (i *impl) Set(_ context.Context, key string, value []byte, expiry time.Duration) error {
i.lock.Lock()
defer i.lock.Unlock()
if _, exists := i.positions[key]; !exists {
old := i.slots[i.next]
if index, exists := i.positions[old]; exists && index == i.next {
i.store.Delete(old)
delete(i.positions, old)
}
i.slots[i.next] = key
i.positions[key] = i.next
i.next = (i.next + 1) % maxEntries
}
i.store.Set(key, value, expiry)
return nil
}
@@ -69,7 +91,8 @@ func (i *impl) cleanupThread(ctx context.Context) {
// New creates a simple in-memory store. This will not scale to multiple Anubis instances.
func New(ctx context.Context) store.Interface {
result := &impl{
store: decaymap.New[string, []byte](),
store: decaymap.New[string, []byte](),
positions: make(map[string]int),
}
go result.cleanupThread(ctx)