Adding upstream version 2.52.6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
a960158181
commit
6d002e9543
441 changed files with 95392 additions and 0 deletions
33
internal/storage/memory/config.go
Normal file
33
internal/storage/memory/config.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package memory
|
||||
|
||||
import "time"
|
||||
|
||||
// Config defines the config for storage.
|
||||
type Config struct {
|
||||
// Time before deleting expired keys
|
||||
//
|
||||
// Default is 10 * time.Second
|
||||
GCInterval time.Duration
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
GCInterval: 10 * time.Second,
|
||||
}
|
||||
|
||||
// configDefault is a helper function to set default values
|
||||
func configDefault(config ...Config) Config {
|
||||
// Return default config if nothing provided
|
||||
if len(config) < 1 {
|
||||
return ConfigDefault
|
||||
}
|
||||
|
||||
// Override default config
|
||||
cfg := config[0]
|
||||
|
||||
// Set default values
|
||||
if int(cfg.GCInterval.Seconds()) <= 0 {
|
||||
cfg.GCInterval = ConfigDefault.GCInterval
|
||||
}
|
||||
return cfg
|
||||
}
|
145
internal/storage/memory/memory.go
Normal file
145
internal/storage/memory/memory.go
Normal file
|
@ -0,0 +1,145 @@
|
|||
// Package memory Is a copy of the storage memory from the external storage packet as a purpose to test the behavior
|
||||
// in the unittests when using a storages from these packets
|
||||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
)
|
||||
|
||||
// Storage interface that is implemented by storage providers
|
||||
type Storage struct {
|
||||
mux sync.RWMutex
|
||||
db map[string]entry
|
||||
gcInterval time.Duration
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
data []byte
|
||||
// max value is 4294967295 -> Sun Feb 07 2106 06:28:15 GMT+0000
|
||||
expiry uint32
|
||||
}
|
||||
|
||||
// New creates a new memory storage
|
||||
func New(config ...Config) *Storage {
|
||||
// Set default config
|
||||
cfg := configDefault(config...)
|
||||
|
||||
// Create storage
|
||||
store := &Storage{
|
||||
db: make(map[string]entry),
|
||||
gcInterval: cfg.GCInterval,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Start garbage collector
|
||||
utils.StartTimeStampUpdater()
|
||||
go store.gc()
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
// Get value by key
|
||||
func (s *Storage) Get(key string) ([]byte, error) {
|
||||
if len(key) <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
s.mux.RLock()
|
||||
v, ok := s.db[key]
|
||||
s.mux.RUnlock()
|
||||
if !ok || v.expiry != 0 && v.expiry <= atomic.LoadUint32(&utils.Timestamp) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return v.data, nil
|
||||
}
|
||||
|
||||
// Set key with value
|
||||
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
||||
// Ain't Nobody Got Time For That
|
||||
if len(key) <= 0 || len(val) <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var expire uint32
|
||||
if exp != 0 {
|
||||
expire = uint32(exp.Seconds()) + atomic.LoadUint32(&utils.Timestamp)
|
||||
}
|
||||
|
||||
e := entry{val, expire}
|
||||
s.mux.Lock()
|
||||
s.db[key] = e
|
||||
s.mux.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete key by key
|
||||
func (s *Storage) Delete(key string) error {
|
||||
// Ain't Nobody Got Time For That
|
||||
if len(key) <= 0 {
|
||||
return nil
|
||||
}
|
||||
s.mux.Lock()
|
||||
delete(s.db, key)
|
||||
s.mux.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset all keys
|
||||
func (s *Storage) Reset() error {
|
||||
ndb := make(map[string]entry)
|
||||
s.mux.Lock()
|
||||
s.db = ndb
|
||||
s.mux.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close the memory storage
|
||||
func (s *Storage) Close() error {
|
||||
s.done <- struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) gc() {
|
||||
ticker := time.NewTicker(s.gcInterval)
|
||||
defer ticker.Stop()
|
||||
var expired []string
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-s.done:
|
||||
return
|
||||
case <-ticker.C:
|
||||
ts := atomic.LoadUint32(&utils.Timestamp)
|
||||
expired = expired[:0]
|
||||
s.mux.RLock()
|
||||
for id, v := range s.db {
|
||||
if v.expiry != 0 && v.expiry <= ts {
|
||||
expired = append(expired, id)
|
||||
}
|
||||
}
|
||||
s.mux.RUnlock()
|
||||
s.mux.Lock()
|
||||
// Double-checked locking.
|
||||
// We might have replaced the item in the meantime.
|
||||
for i := range expired {
|
||||
v := s.db[expired[i]]
|
||||
if v.expiry != 0 && v.expiry <= ts {
|
||||
delete(s.db, expired[i])
|
||||
}
|
||||
}
|
||||
s.mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return database client
|
||||
func (s *Storage) Conn() map[string]entry {
|
||||
s.mux.RLock()
|
||||
defer s.mux.RUnlock()
|
||||
return s.db
|
||||
}
|
158
internal/storage/memory/memory_test.go
Normal file
158
internal/storage/memory/memory_test.go
Normal file
|
@ -0,0 +1,158 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
)
|
||||
|
||||
var testStore = New()
|
||||
|
||||
func Test_Storage_Memory_Set(t *testing.T) {
|
||||
t.Parallel()
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Set_Override(t *testing.T) {
|
||||
t.Parallel()
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
err = testStore.Set(key, val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Get(t *testing.T) {
|
||||
t.Parallel()
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
result, err := testStore.Get(key)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, val, result)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Set_Expiration(t *testing.T) {
|
||||
t.Parallel()
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
exp = 1 * time.Second
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, exp)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Get_Expired(t *testing.T) {
|
||||
key := "john"
|
||||
|
||||
result, err := testStore.Get(key)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, len(result) == 0)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Get_NotExist(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := testStore.Get("notexist")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, len(result) == 0)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
var (
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
)
|
||||
|
||||
err := testStore.Set(key, val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
err = testStore.Delete(key)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
result, err := testStore.Get(key)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, len(result) == 0)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Reset(t *testing.T) {
|
||||
t.Parallel()
|
||||
val := []byte("doe")
|
||||
|
||||
err := testStore.Set("john1", val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
err = testStore.Set("john2", val, 0)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
err = testStore.Reset()
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
result, err := testStore.Get("john1")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, len(result) == 0)
|
||||
|
||||
result, err = testStore.Get("john2")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, len(result) == 0)
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Close(t *testing.T) {
|
||||
t.Parallel()
|
||||
utils.AssertEqual(t, nil, testStore.Close())
|
||||
}
|
||||
|
||||
func Test_Storage_Memory_Conn(t *testing.T) {
|
||||
t.Parallel()
|
||||
utils.AssertEqual(t, true, testStore.Conn() != nil)
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_Storage_Memory -benchmem -count=4
|
||||
func Benchmark_Storage_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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue