133 lines
3.3 KiB
Go
133 lines
3.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
const (
|
|
runSummaryAggregateCacheTTL = 5 * time.Second
|
|
runSummaryAggregateCacheMaxEntries = 128
|
|
)
|
|
|
|
type runSummaryAggregates struct {
|
|
Total int64
|
|
StatusCounts map[domain.RunStatus]int64
|
|
}
|
|
|
|
type runSummaryAggregateCacheKey struct {
|
|
OrganizationID string
|
|
ProjectID string
|
|
WorkflowID string
|
|
Mode domain.RunMode
|
|
Search string
|
|
}
|
|
|
|
func newRunSummaryAggregateCacheKey(organizationID string, query domain.RunSummaryQuery) runSummaryAggregateCacheKey {
|
|
// Status, cursor and limit only affect the visible page. The aggregate cards
|
|
// intentionally describe the complete result set for the remaining filters.
|
|
return runSummaryAggregateCacheKey{
|
|
OrganizationID: strings.TrimSpace(organizationID),
|
|
ProjectID: query.ProjectID,
|
|
WorkflowID: query.WorkflowID,
|
|
Mode: query.Mode,
|
|
Search: strings.TrimSpace(query.Search),
|
|
}
|
|
}
|
|
|
|
type runSummaryAggregateCacheEntry struct {
|
|
value runSummaryAggregates
|
|
expiresAt time.Time
|
|
}
|
|
|
|
type runSummaryAggregateCache struct {
|
|
mu sync.Mutex
|
|
ttl time.Duration
|
|
maxEntries int
|
|
now func() time.Time
|
|
entries map[runSummaryAggregateCacheKey]runSummaryAggregateCacheEntry
|
|
loading map[runSummaryAggregateCacheKey]chan struct{}
|
|
}
|
|
|
|
func newRunSummaryAggregateCache(ttl time.Duration, maxEntries int) *runSummaryAggregateCache {
|
|
return &runSummaryAggregateCache{
|
|
ttl: ttl,
|
|
maxEntries: maxEntries,
|
|
now: time.Now,
|
|
entries: make(map[runSummaryAggregateCacheKey]runSummaryAggregateCacheEntry),
|
|
loading: make(map[runSummaryAggregateCacheKey]chan struct{}),
|
|
}
|
|
}
|
|
|
|
func cloneRunSummaryAggregates(value runSummaryAggregates) runSummaryAggregates {
|
|
counts := make(map[domain.RunStatus]int64, len(value.StatusCounts))
|
|
for status, count := range value.StatusCounts {
|
|
counts[status] = count
|
|
}
|
|
return runSummaryAggregates{Total: value.Total, StatusCounts: counts}
|
|
}
|
|
|
|
func (cache *runSummaryAggregateCache) getOrLoad(
|
|
ctx context.Context,
|
|
key runSummaryAggregateCacheKey,
|
|
loader func(context.Context) (runSummaryAggregates, error),
|
|
) (runSummaryAggregates, error) {
|
|
for {
|
|
now := cache.now()
|
|
cache.mu.Lock()
|
|
if entry, ok := cache.entries[key]; ok {
|
|
if now.Before(entry.expiresAt) {
|
|
value := cloneRunSummaryAggregates(entry.value)
|
|
cache.mu.Unlock()
|
|
return value, nil
|
|
}
|
|
delete(cache.entries, key)
|
|
}
|
|
if ready, ok := cache.loading[key]; ok {
|
|
cache.mu.Unlock()
|
|
select {
|
|
case <-ctx.Done():
|
|
return runSummaryAggregates{}, ctx.Err()
|
|
case <-ready:
|
|
continue
|
|
}
|
|
}
|
|
ready := make(chan struct{})
|
|
cache.loading[key] = ready
|
|
cache.mu.Unlock()
|
|
|
|
value, err := loader(ctx)
|
|
|
|
cache.mu.Lock()
|
|
delete(cache.loading, key)
|
|
if err == nil {
|
|
cache.pruneLocked(now)
|
|
cache.entries[key] = runSummaryAggregateCacheEntry{
|
|
value: cloneRunSummaryAggregates(value),
|
|
expiresAt: cache.now().Add(cache.ttl),
|
|
}
|
|
}
|
|
close(ready)
|
|
cache.mu.Unlock()
|
|
return value, err
|
|
}
|
|
}
|
|
|
|
func (cache *runSummaryAggregateCache) pruneLocked(now time.Time) {
|
|
for key, entry := range cache.entries {
|
|
if !now.Before(entry.expiresAt) {
|
|
delete(cache.entries, key)
|
|
}
|
|
}
|
|
for cache.maxEntries > 0 && len(cache.entries) >= cache.maxEntries {
|
|
for key := range cache.entries {
|
|
delete(cache.entries, key)
|
|
break
|
|
}
|
|
}
|
|
}
|