torrent cleanup
This commit is contained in:
parent
b069b3ad1c
commit
fc6b838cf5
24 changed files with 1316 additions and 395 deletions
pkg/maxcache
114
pkg/maxcache/cache.go
Normal file
114
pkg/maxcache/cache.go
Normal file
|
@ -0,0 +1,114 @@
|
|||
package maxcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cespare/xxhash/v2"
|
||||
"github.com/goware/singleflight"
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
)
|
||||
|
||||
func NewCache[K comparable, V any](size int, freshFor, ttl time.Duration) *Cache[K, V] {
|
||||
values, _ := lru.New[K, value[V]](size)
|
||||
return &Cache[K, V]{
|
||||
values: values,
|
||||
}
|
||||
}
|
||||
|
||||
type Cache[K comparable, V any] struct {
|
||||
values *lru.Cache[K, value[V]]
|
||||
|
||||
mu sync.RWMutex
|
||||
callGroup singleflight.Group[K, V]
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Get(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, error) {
|
||||
return c.get(ctx, key, false, fn)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) GetFresh(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, error) {
|
||||
return c.get(ctx, key, true, fn)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Set(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, bool, error) {
|
||||
v, err, shared := c.callGroup.Do(key, c.set(key, fn))
|
||||
return v, shared, err
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn singleflight.DoFunc[V]) (V, error) {
|
||||
c.mu.RLock()
|
||||
val, ok := c.values.Get(key)
|
||||
c.mu.RUnlock()
|
||||
|
||||
// value exists and is fresh - just return
|
||||
if ok && val.IsFresh() {
|
||||
return val.Value(), nil
|
||||
}
|
||||
|
||||
// value exists and is stale, and we're OK with serving it stale while updating in the background
|
||||
// note: stale means its still okay, but not fresh. but if its expired, then it means its useless.
|
||||
if ok && !freshOnly && !val.IsExpired() {
|
||||
// TODO: technically could be a stampede of goroutines here if the value is expired
|
||||
// and we're OK with serving it stale
|
||||
go c.Set(ctx, key, fn)
|
||||
return val.Value(), nil
|
||||
}
|
||||
|
||||
// value doesn't exist or is expired, or is stale and we need it fresh (freshOnly:true) - sync update
|
||||
v, _, err := c.Set(ctx, key, fn)
|
||||
return v, err
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) set(key K, fn singleflight.DoFunc[V]) singleflight.DoFunc[V] {
|
||||
return singleflight.DoFunc[V](func() (V, error) {
|
||||
val, err := fn()
|
||||
if err != nil {
|
||||
return val, err
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
c.values.Add(key, value[V]{
|
||||
v: val,
|
||||
})
|
||||
c.mu.Unlock()
|
||||
|
||||
return val, nil
|
||||
})
|
||||
}
|
||||
|
||||
type value[V any] struct {
|
||||
v V
|
||||
|
||||
bestBefore time.Time // cache entry freshness cutoff
|
||||
expiry time.Time // cache entry time to live cutoff
|
||||
}
|
||||
|
||||
func (v *value[V]) IsFresh() bool {
|
||||
return v.bestBefore.After(time.Now())
|
||||
}
|
||||
|
||||
func (v *value[V]) IsExpired() bool {
|
||||
return v.expiry.Before(time.Now())
|
||||
}
|
||||
|
||||
func (v *value[V]) Value() V {
|
||||
return v.v
|
||||
}
|
||||
|
||||
func BytesToHash(b ...[]byte) uint64 {
|
||||
d := xxhash.New()
|
||||
for _, v := range b {
|
||||
d.Write(v)
|
||||
}
|
||||
return d.Sum64()
|
||||
}
|
||||
|
||||
func StringToHash(s ...string) uint64 {
|
||||
d := xxhash.New()
|
||||
for _, v := range s {
|
||||
d.WriteString(v)
|
||||
}
|
||||
return d.Sum64()
|
||||
}
|
204
pkg/maxcache/cache_test.go
Normal file
204
pkg/maxcache/cache_test.go
Normal file
|
@ -0,0 +1,204 @@
|
|||
package maxcache_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/stampede"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
var count uint64
|
||||
cache := stampede.NewCache(512, time.Duration(2*time.Second), time.Duration(5*time.Second))
|
||||
|
||||
// repeat test multiple times
|
||||
for x := 0; x < 5; x++ {
|
||||
// time.Sleep(1 * time.Second)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
numGoroutines := runtime.NumGoroutine()
|
||||
|
||||
n := 10
|
||||
ctx := context.Background()
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
t.Logf("numGoroutines now %d", runtime.NumGoroutine())
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
val, err := cache.Get(ctx, "t1", func() (any, error) {
|
||||
t.Log("cache.Get(t1, ...)")
|
||||
|
||||
// some extensive op..
|
||||
time.Sleep(2 * time.Second)
|
||||
atomic.AddUint64(&count, 1)
|
||||
|
||||
return "result1", nil
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result1", val)
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// ensure single call
|
||||
assert.Equal(t, uint64(1), count)
|
||||
|
||||
// confirm same before/after num of goroutines
|
||||
t.Logf("numGoroutines now %d", runtime.NumGoroutine())
|
||||
assert.Equal(t, numGoroutines, runtime.NumGoroutine())
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
var numRequests = 30
|
||||
|
||||
var hits uint32
|
||||
var expectedStatus int = 201
|
||||
var expectedBody = []byte("hi")
|
||||
|
||||
app := func(w http.ResponseWriter, r *http.Request) {
|
||||
// log.Println("app handler..")
|
||||
|
||||
atomic.AddUint32(&hits, 1)
|
||||
|
||||
hitsNow := atomic.LoadUint32(&hits)
|
||||
if hitsNow > 1 {
|
||||
// panic("uh oh")
|
||||
}
|
||||
|
||||
// time.Sleep(100 * time.Millisecond) // slow handler
|
||||
w.Header().Set("X-Httpjoin", "test")
|
||||
w.WriteHeader(expectedStatus)
|
||||
w.Write(expectedBody)
|
||||
}
|
||||
|
||||
var count uint32
|
||||
counter := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddUint32(&count, 1)
|
||||
next.ServeHTTP(w, r)
|
||||
atomic.AddUint32(&count, ^uint32(0))
|
||||
// log.Println("COUNT:", atomic.LoadUint32(&count))
|
||||
})
|
||||
}
|
||||
|
||||
recoverer := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Println("recovered panicing request:", r)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
h := stampede.Handler(512, 1*time.Second)
|
||||
|
||||
ts := httptest.NewServer(counter(recoverer(h(http.HandlerFunc(app)))))
|
||||
defer ts.Close()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for i := 0; i < numRequests; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resp, err := http.Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// log.Println("got resp:", resp, "len:", len(body), "body:", string(body))
|
||||
|
||||
if string(body) != string(expectedBody) {
|
||||
t.Error("expecting response body:", string(expectedBody))
|
||||
}
|
||||
|
||||
if resp.StatusCode != expectedStatus {
|
||||
t.Error("expecting response status:", expectedStatus)
|
||||
}
|
||||
|
||||
assert.Equal(t, "test", resp.Header.Get("X-Httpjoin"), "expecting x-httpjoin test header")
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
totalHits := atomic.LoadUint32(&hits)
|
||||
// if totalHits > 1 {
|
||||
// t.Error("handler was hit more than once. hits:", totalHits)
|
||||
// }
|
||||
log.Println("total hits:", totalHits)
|
||||
|
||||
finalCount := atomic.LoadUint32(&count)
|
||||
if finalCount > 0 {
|
||||
t.Error("queue count was expected to be empty, but count:", finalCount)
|
||||
}
|
||||
log.Println("final count:", finalCount)
|
||||
}
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
h1 := stampede.BytesToHash([]byte{1, 2, 3})
|
||||
assert.Equal(t, uint64(8376154270085342629), h1)
|
||||
|
||||
h2 := stampede.StringToHash("123")
|
||||
assert.Equal(t, uint64(4353148100880623749), h2)
|
||||
}
|
||||
|
||||
func TestPanic(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
middleware := stampede.Handler(100, 1*time.Hour)
|
||||
mux.Handle("/", middleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
t.Log(r.Method, r.URL)
|
||||
})))
|
||||
|
||||
ts := httptest.NewServer(mux)
|
||||
defer ts.Close()
|
||||
|
||||
{
|
||||
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
t.Log(resp.StatusCode)
|
||||
}
|
||||
{
|
||||
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
t.Log(resp.StatusCode)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue