1
0
Fork 0

Adding upstream version 2.52.6.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-17 06:50:16 +02:00
parent a960158181
commit 6d002e9543
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
441 changed files with 95392 additions and 0 deletions

97
internal/memory/memory.go Normal file
View file

@ -0,0 +1,97 @@
// Package memory Is a slight copy of the memory storage, but far from the storage interface it can not only work with bytes
// but directly store any kind of data without having to encode it each time, which gives a huge speed advantage
package memory
import (
"sync"
"sync/atomic"
"time"
"github.com/gofiber/fiber/v2/utils"
)
type Storage struct {
sync.RWMutex
data map[string]item // data
}
type item struct {
// max value is 4294967295 -> Sun Feb 07 2106 06:28:15 GMT+0000
e uint32 // exp
v interface{} // val
}
func New() *Storage {
store := &Storage{
data: make(map[string]item),
}
utils.StartTimeStampUpdater()
go store.gc(1 * time.Second)
return store
}
// Get value by key
func (s *Storage) Get(key string) interface{} {
s.RLock()
v, ok := s.data[key]
s.RUnlock()
if !ok || v.e != 0 && v.e <= atomic.LoadUint32(&utils.Timestamp) {
return nil
}
return v.v
}
// Set key with value
func (s *Storage) Set(key string, val interface{}, ttl time.Duration) {
var exp uint32
if ttl > 0 {
exp = uint32(ttl.Seconds()) + atomic.LoadUint32(&utils.Timestamp)
}
i := item{exp, val}
s.Lock()
s.data[key] = i
s.Unlock()
}
// Delete key by key
func (s *Storage) Delete(key string) {
s.Lock()
delete(s.data, key)
s.Unlock()
}
// Reset all keys
func (s *Storage) Reset() {
nd := make(map[string]item)
s.Lock()
s.data = nd
s.Unlock()
}
func (s *Storage) gc(sleep time.Duration) {
ticker := time.NewTicker(sleep)
defer ticker.Stop()
var expired []string
for range ticker.C {
ts := atomic.LoadUint32(&utils.Timestamp)
expired = expired[:0]
s.RLock()
for key, v := range s.data {
if v.e != 0 && v.e <= ts {
expired = append(expired, key)
}
}
s.RUnlock()
s.Lock()
// Double-checked locking.
// We might have replaced the item in the meantime.
for i := range expired {
v := s.data[expired[i]]
if v.e != 0 && v.e <= ts {
delete(s.data, expired[i])
}
}
s.Unlock()
}
}

View file

@ -0,0 +1,81 @@
package memory
import (
"testing"
"time"
"github.com/gofiber/fiber/v2/utils"
)
// go test -run Test_Memory -v -race
func Test_Memory(t *testing.T) {
t.Parallel()
store := New()
var (
key = "john"
val interface{} = []byte("doe")
exp = 1 * time.Second
)
store.Set(key, val, 0)
store.Set(key, val, 0)
result := store.Get(key)
utils.AssertEqual(t, val, result)
result = store.Get("empty")
utils.AssertEqual(t, nil, result)
store.Set(key, val, exp)
time.Sleep(1100 * time.Millisecond)
result = store.Get(key)
utils.AssertEqual(t, nil, result)
store.Set(key, val, 0)
result = store.Get(key)
utils.AssertEqual(t, val, result)
store.Delete(key)
result = store.Get(key)
utils.AssertEqual(t, nil, result)
store.Set("john", val, 0)
store.Set("doe", val, 0)
store.Reset()
result = store.Get("john")
utils.AssertEqual(t, nil, result)
result = store.Get("doe")
utils.AssertEqual(t, nil, result)
}
// go test -v -run=^$ -bench=Benchmark_Memory -benchmem -count=4
func Benchmark_Memory(b *testing.B) {
keyLength := 1000
keys := make([]string, keyLength)
for i := 0; i < keyLength; i++ {
keys[i] = utils.UUID()
}
value := []byte("joe")
ttl := 2 * time.Second
b.Run("fiber_memory", func(b *testing.B) {
d := New()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, key := range keys {
d.Set(key, value, ttl)
}
for _, key := range keys {
_ = d.Get(key)
}
for _, key := range keys {
d.Delete(key)
}
}
})
}