mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-01-26 11:34:03 +00:00
* chore(ci): update go to 1.25, add permissions and envs * fix(ci): update pr-lint.yml * chore: update go, fix linting * fix: tests and linting * fix(lint): lint fixes, renovate should now pass * fix: updates, security upgrades * chore: workflow updates, lint * fix: more lint, checkmake, and other fixes * fix: more lint, convert scripts to POSIX compliant * fix: simplify codeql workflow * tests: increase test coverage, fix found issues * fix(lint): editorconfig checking, add to linters * fix(lint): shellcheck, add to linters * fix(lint): apply cr comment suggestions * fix(ci): remove step-security/harden-runner * fix(lint): remove duplication, apply cr fixes * fix(ci): tests in CI/CD pipeline * chore(lint): deduplication of strings * fix(lint): apply cr comment suggestions * fix(ci): actionlint * fix(lint): apply cr comment suggestions * chore: lint, add deps management
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package fileproc
|
|
|
|
import (
|
|
"runtime"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/ivuorinen/gibidify/gibidiutils"
|
|
)
|
|
|
|
// RecordFileProcessed records that a file has been successfully processed.
|
|
func (rm *ResourceMonitor) RecordFileProcessed(fileSize int64) {
|
|
if rm.enabled {
|
|
atomic.AddInt64(&rm.filesProcessed, 1)
|
|
atomic.AddInt64(&rm.totalSizeProcessed, fileSize)
|
|
}
|
|
}
|
|
|
|
// GetMetrics returns current resource usage metrics.
|
|
func (rm *ResourceMonitor) GetMetrics() ResourceMetrics {
|
|
if !rm.enableResourceMon {
|
|
return ResourceMetrics{}
|
|
}
|
|
|
|
rm.mu.RLock()
|
|
defer rm.mu.RUnlock()
|
|
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
|
|
filesProcessed := atomic.LoadInt64(&rm.filesProcessed)
|
|
totalSize := atomic.LoadInt64(&rm.totalSizeProcessed)
|
|
duration := time.Since(rm.startTime)
|
|
|
|
avgFileSize := float64(0)
|
|
if filesProcessed > 0 {
|
|
avgFileSize = float64(totalSize) / float64(filesProcessed)
|
|
}
|
|
|
|
processingRate := float64(0)
|
|
if duration.Seconds() > 0 {
|
|
processingRate = float64(filesProcessed) / duration.Seconds()
|
|
}
|
|
|
|
// Collect violations
|
|
violations := make([]string, 0, len(rm.violationLogged))
|
|
for violation := range rm.violationLogged {
|
|
violations = append(violations, violation)
|
|
}
|
|
|
|
return ResourceMetrics{
|
|
FilesProcessed: filesProcessed,
|
|
TotalSizeProcessed: totalSize,
|
|
ConcurrentReads: atomic.LoadInt64(&rm.concurrentReads),
|
|
ProcessingDuration: duration,
|
|
AverageFileSize: avgFileSize,
|
|
ProcessingRate: processingRate,
|
|
MemoryUsageMB: gibidiutils.SafeUint64ToInt64WithDefault(m.Alloc, 0) / 1024 / 1024,
|
|
MaxMemoryUsageMB: int64(rm.hardMemoryLimitMB),
|
|
ViolationsDetected: violations,
|
|
DegradationActive: rm.degradationActive,
|
|
EmergencyStopActive: rm.emergencyStopRequested,
|
|
LastUpdated: time.Now(),
|
|
}
|
|
}
|
|
|
|
// LogResourceInfo logs current resource limit configuration.
|
|
func (rm *ResourceMonitor) LogResourceInfo() {
|
|
if rm.enabled {
|
|
logrus.Infof(
|
|
"Resource limits enabled: maxFiles=%d, maxTotalSize=%dMB, fileTimeout=%ds, overallTimeout=%ds",
|
|
rm.maxFiles,
|
|
rm.maxTotalSize/1024/1024,
|
|
int(rm.fileProcessingTimeout.Seconds()),
|
|
int(rm.overallTimeout.Seconds()),
|
|
)
|
|
logrus.Infof("Resource limits: maxConcurrentReads=%d, rateLimitFPS=%d, hardMemoryMB=%d",
|
|
rm.maxConcurrentReads, rm.rateLimitFilesPerSec, rm.hardMemoryLimitMB)
|
|
logrus.Infof("Resource features: gracefulDegradation=%v, monitoring=%v",
|
|
rm.enableGracefulDegr, rm.enableResourceMon)
|
|
} else {
|
|
logrus.Info("Resource limits disabled")
|
|
}
|
|
}
|