forked from upstream/filebrowser
+10








3c02eedca6
Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: Gabriel Górski <me@glaeqen.eu> Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com> Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com> Co-authored-by: carloda <35763669+carloda@users.noreply.github.com> Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com> Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com> Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com> Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com> Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com> Co-authored-by: Juan Soler <juansoler@gmail.com> Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net> Co-authored-by: Neil <kylelzzz02@icloud.com> Co-authored-by: Graham <info@quantumx-apps.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package preview
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// MemoryTracker wraps the existing FFmpegService to add memory tracking
|
|
type MemoryTracker struct {
|
|
maxMemoryBytes int64
|
|
usedMemoryBytes int64
|
|
semaphore chan struct{}
|
|
}
|
|
|
|
// NewMemoryTracker creates a memory-aware concurrency limiter
|
|
func NewMemoryTracker(maxConcurrent int, maxMemoryMB int) *MemoryTracker {
|
|
if maxConcurrent < 1 {
|
|
maxConcurrent = 1
|
|
}
|
|
if maxMemoryMB < 50 {
|
|
maxMemoryMB = 500 // Default 500MB
|
|
}
|
|
return &MemoryTracker{
|
|
maxMemoryBytes: int64(maxMemoryMB) * 1024 * 1024,
|
|
semaphore: make(chan struct{}, maxConcurrent),
|
|
}
|
|
}
|
|
|
|
// TryAcquire attempts to acquire resources for processing an image
|
|
// Returns false if memory limit would be exceeded
|
|
func (mt *MemoryTracker) TryAcquire(ctx context.Context, estimatedBytes int64) bool {
|
|
// Check if we have memory headroom
|
|
currentUsed := atomic.LoadInt64(&mt.usedMemoryBytes)
|
|
if currentUsed+estimatedBytes > mt.maxMemoryBytes {
|
|
return false
|
|
}
|
|
|
|
// Try to acquire semaphore slot (non-blocking check first)
|
|
select {
|
|
case mt.semaphore <- struct{}{}:
|
|
// Successfully acquired slot, now add memory
|
|
atomic.AddInt64(&mt.usedMemoryBytes, estimatedBytes)
|
|
return true
|
|
case <-ctx.Done():
|
|
return false
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Acquire waits to acquire resources
|
|
func (mt *MemoryTracker) Acquire(ctx context.Context, estimatedBytes int64) error {
|
|
// Wait for semaphore slot
|
|
select {
|
|
case mt.semaphore <- struct{}{}:
|
|
atomic.AddInt64(&mt.usedMemoryBytes, estimatedBytes)
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
// Release frees up resources
|
|
func (mt *MemoryTracker) Release(estimatedBytes int64) {
|
|
<-mt.semaphore
|
|
atomic.AddInt64(&mt.usedMemoryBytes, -estimatedBytes)
|
|
}
|
|
|
|
// GetMemoryUsageMB returns current memory usage in MB
|
|
func (mt *MemoryTracker) GetMemoryUsageMB() int64 {
|
|
return atomic.LoadInt64(&mt.usedMemoryBytes) / (1024 * 1024)
|
|
}
|