storage

package
v0.48.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2026 License: Unlicense Imports: 9 Imported by: 0

Documentation

Overview

Package storage provides storage management functionality including filesystem space detection, access tracking for events, and garbage collection based on access patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateMaxStorage

func CalculateMaxStorage(dataDir string, configuredMax int64) (int64, error)

CalculateMaxStorage calculates the maximum storage limit for the relay. If configuredMax > 0, it returns that value directly. Otherwise, it returns 80% of the available filesystem space.

func GetCurrentStorageUsage

func GetCurrentStorageUsage(dataDir string) (int64, error)

GetCurrentStorageUsage calculates the current storage usage of the data directory. This is an approximation based on filesystem stats for the given path.

Types

type AccessTracker

type AccessTracker struct {
	// contains filtered or unexported fields
}

AccessTracker tracks event access patterns with session deduplication. It maintains an in-memory cache to deduplicate accesses from the same connection, reducing database writes while ensuring unique session counting.

func NewAccessTracker

func NewAccessTracker(db AccessTrackerDatabase, maxSeenEntries int) *AccessTracker

NewAccessTracker creates a new access tracker. maxSeenEntries controls the size of the deduplication cache.

func (*AccessTracker) ClearConnection

func (t *AccessTracker) ClearConnection(connectionID string)

ClearConnection removes all dedup entries for a specific connection. Call this when a connection closes to free up cache space.

func (*AccessTracker) GetAccessInfo

func (t *AccessTracker) GetAccessInfo(serial uint64) (lastAccess int64, accessCount uint32, err error)

GetAccessInfo returns the access information for an event.

func (*AccessTracker) GetColdestEvents

func (t *AccessTracker) GetColdestEvents(limit int, minAgeSec int64) ([]uint64, error)

GetColdestEvents returns event serials sorted by coldness. limit: max events to return minAgeSec: minimum age in seconds since last access

func (*AccessTracker) RecordAccess

func (t *AccessTracker) RecordAccess(serial uint64, connectionID string) (bool, error)

RecordAccess records an access to an event by a connection. Deduplicates accesses from the same connection within the cache window. Returns true if this was a new access, false if deduplicated.

func (*AccessTracker) Start

func (t *AccessTracker) Start()

Start starts any background goroutines for the tracker. Currently a no-op but provided for future use.

func (*AccessTracker) Stats

func (t *AccessTracker) Stats() AccessTrackerStats

Stats returns current cache statistics.

func (*AccessTracker) Stop

func (t *AccessTracker) Stop()

Stop stops the access tracker and releases resources.

type AccessTrackerDatabase

type AccessTrackerDatabase interface {
	RecordEventAccess(serial uint64, connectionID string) error
	GetEventAccessInfo(serial uint64) (lastAccess int64, accessCount uint32, err error)
	GetLeastAccessedEvents(limit int, minAgeSec int64) (serials []uint64, err error)
}

AccessTrackerDatabase defines the interface for the underlying database that stores access tracking information.

type AccessTrackerStats

type AccessTrackerStats struct {
	CachedEntries int
	MaxEntries    int
}

AccessTrackerStats holds access tracker statistics.

type FilesystemStats

type FilesystemStats struct {
	Total     uint64 // Total bytes on filesystem
	Available uint64 // Available bytes (for unprivileged users)
	Used      uint64 // Used bytes
}

FilesystemStats holds information about filesystem space usage.

func GetFilesystemStats

func GetFilesystemStats(path string) (stats FilesystemStats, err error)

GetFilesystemStats returns filesystem space information for the given path. The path should be a directory within the filesystem to check.

type GCConfig

type GCConfig struct {
	MaxStorageBytes int64         // 0 = auto-calculate (80% of filesystem)
	Interval        time.Duration // How often to check storage
	BatchSize       int           // Events to consider per GC run
	MinAgeSec       int64         // Minimum age before eviction (default: 1 hour)
}

GCConfig holds configuration for the garbage collector.

func DefaultGCConfig

func DefaultGCConfig() GCConfig

DefaultGCConfig returns a default GC configuration.

type GCDatabase

type GCDatabase interface {
	Path() string
	FetchEventBySerial(ser *types.Uint40) (ev *event.E, err error)
	DeleteEventBySerial(ctx context.Context, ser *types.Uint40, ev *event.E) error
}

GCDatabase defines the interface for database operations needed by the GC.

type GCStats

type GCStats struct {
	Running             bool
	LastRunTime         time.Time
	TotalEvicted        uint64
	CurrentStorageBytes int64
	MaxStorageBytes     int64
	StoragePercentage   float64
}

GCStats holds garbage collector statistics.

type GarbageCollector

type GarbageCollector struct {
	// contains filtered or unexported fields
}

GarbageCollector manages continuous event eviction based on access patterns. It monitors storage usage and evicts the least accessed events when the storage limit is exceeded.

func NewGarbageCollector

func NewGarbageCollector(
	ctx context.Context,
	db GCDatabase,
	tracker *AccessTracker,
	cfg GCConfig,
) *GarbageCollector

NewGarbageCollector creates a new garbage collector.

func (*GarbageCollector) Start

func (gc *GarbageCollector) Start()

Start begins the garbage collection loop.

func (*GarbageCollector) Stats

func (gc *GarbageCollector) Stats() GCStats

Stats returns current GC statistics.

func (*GarbageCollector) Stop

func (gc *GarbageCollector) Stop()

Stop stops the garbage collector.

Source Files

  • access_tracker.go
  • gc.go
  • limits.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL