add request buffer for nmap data store

This commit is contained in:
pascal
2026-08-21 12:54:26 +02:00
parent f1c4b664f7
commit 6e677cd2e6
4 changed files with 258 additions and 103 deletions
@@ -0,0 +1,102 @@
// Package requestbuffer coalesces concurrent reads of the same expensive
// resource into a single fetch.
package requestbuffer
import (
"context"
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
// FetchFunc reads the resource identified by key.
type FetchFunc[T any] func(ctx context.Context, key string) (T, error)
// Buffer batches requests per key: the first request opens a window, every
// request arriving within it joins the batch, and a single fetch serves them
// all. The fetch starts only after the window closed, so a caller never
// observes data read before its own request.
type Buffer[T any] struct {
ctx context.Context
name string
fetch FetchFunc[T]
interval time.Duration
mu sync.Mutex
waiting map[string][]chan result[T]
}
type result[T any] struct {
value T
err error
}
// New returns a Buffer serving batched requests through fetch. ctx bounds the
// fetches, not the callers, and must outlive them.
func New[T any](ctx context.Context, name string, interval time.Duration, fetch FetchFunc[T]) *Buffer[T] {
return &Buffer[T]{
ctx: ctx,
name: name,
fetch: fetch,
interval: interval,
waiting: make(map[string][]chan result[T]),
}
}
// Get returns the value for key, sharing one fetch with the other callers of
// the current batch. The value is shared as is, so callers must treat it as
// read-only unless the fetch hands out copies.
func (b *Buffer[T]) Get(ctx context.Context, key string) (T, error) {
ch := make(chan result[T], 1)
b.mu.Lock()
b.waiting[key] = append(b.waiting[key], ch)
first := len(b.waiting[key]) == 1
b.mu.Unlock()
if first {
time.AfterFunc(b.interval, func() { b.flush(key) })
}
select {
case res := <-ch:
return res.value, res.err
case <-ctx.Done():
var zero T
return zero, ctx.Err()
}
}
func (b *Buffer[T]) flush(key string) {
b.mu.Lock()
waiting := b.waiting[key]
delete(b.waiting, key)
b.mu.Unlock()
if len(waiting) == 0 {
return
}
start := time.Now()
value, err := b.fetch(b.ctx, key)
log.WithContext(b.ctx).Tracef("%s: fetched %s for %d waiters in %s", b.name, key, len(waiting), time.Since(start))
for _, ch := range waiting {
ch <- result[T]{value: value, err: err}
}
}
// Interval reads a buffer interval from envVar, falling back to def.
func Interval(ctx context.Context, envVar string, def time.Duration) time.Duration {
value := os.Getenv(envVar)
interval, err := time.ParseDuration(value)
if err != nil {
if value != "" {
log.WithContext(ctx).Warnf("failed to parse %s: %s", envVar, err)
}
return def
}
return interval
}
@@ -0,0 +1,106 @@
package requestbuffer
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBufferCoalescesConcurrentRequests(t *testing.T) {
var fetches atomic.Int32
buffer := New(context.Background(), "test", 50*time.Millisecond,
func(ctx context.Context, key string) (string, error) {
fetches.Add(1)
return key, nil
})
var wg sync.WaitGroup
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()
value, err := buffer.Get(context.Background(), "account")
assert.NoError(t, err)
assert.Equal(t, "account", value)
}()
}
wg.Wait()
assert.Equal(t, int32(1), fetches.Load())
}
func TestBufferSeparatesKeys(t *testing.T) {
keys := make(chan string, 2)
buffer := New(context.Background(), "test", 10*time.Millisecond,
func(ctx context.Context, key string) (string, error) {
keys <- key
return key, nil
})
var wg sync.WaitGroup
for _, key := range []string{"a", "b"} {
wg.Add(1)
go func() {
defer wg.Done()
_, err := buffer.Get(context.Background(), key)
assert.NoError(t, err)
}()
}
wg.Wait()
close(keys)
var fetched []string
for key := range keys {
fetched = append(fetched, key)
}
assert.ElementsMatch(t, []string{"a", "b"}, fetched)
}
func TestBufferFetchesAfterRequest(t *testing.T) {
var version atomic.Int32
buffer := New(context.Background(), "test", 10*time.Millisecond,
func(ctx context.Context, key string) (int32, error) {
return version.Load(), nil
})
first, err := buffer.Get(context.Background(), "account")
require.NoError(t, err)
assert.Equal(t, int32(0), first)
version.Store(1)
second, err := buffer.Get(context.Background(), "account")
require.NoError(t, err)
assert.Equal(t, int32(1), second)
}
func TestBufferPropagatesError(t *testing.T) {
fetchErr := errors.New("fetch failed")
buffer := New(context.Background(), "test", 10*time.Millisecond,
func(ctx context.Context, key string) (*int, error) {
return nil, fetchErr
})
value, err := buffer.Get(context.Background(), "account")
assert.ErrorIs(t, err, fetchErr)
assert.Nil(t, value)
}
func TestBufferHonorsCallerContext(t *testing.T) {
buffer := New(context.Background(), "test", time.Minute,
func(ctx context.Context, key string) (string, error) {
return key, nil
})
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
_, err := buffer.Get(ctx, "account")
assert.ErrorIs(t, err, context.DeadlineExceeded)
}