major refactoring

This commit is contained in:
ston1th 2022-03-23 21:50:01 +01:00
commit 78b5e5f796
14 changed files with 712 additions and 576 deletions

View file

@ -4,7 +4,7 @@ package fs
import (
"errors"
"math"
"sync"
"sync/atomic"
"github.com/go-logr/logr"
@ -13,52 +13,37 @@ import (
var InvalidMaxQuota = errors.New("invalid max quota")
type Quota struct {
mu sync.Mutex
log logr.Logger
fs *FS
max int64
cur int64
max int64
}
func NewQuota(max int64, fs *FS, log logr.Logger) (q *Quota, err error) {
func NewQuota(cur, max int64, fs *FS, log logr.Logger) (q *Quota, err error) {
if max <= 0 {
err = InvalidMaxQuota
return
}
q = &Quota{log: log, fs: fs, max: max}
q = &Quota{log: log, fs: fs, cur: cur, max: max}
q.log.Info("quota usage", "current", q.cur, "max", q.max)
return
}
func (q *Quota) Init() {
for _, v := range q.fs.md {
q.cur += v.Chunks.Size()
}
q.log.Info("quota usage", "current", q.cur, "max", q.max)
func (q *Quota) Usage() (cur, max int64) {
cur = atomic.LoadInt64(&q.cur)
max = q.max
return
}
func (q *Quota) cleanup() {
if !q.fs.mu.TryLock() {
if !q.mu.TryLock() {
return
}
q.log.Info("quota usage", "current", q.cur, "max", q.max)
defer q.fs.mu.Unlock()
atime := int64(math.MaxInt64)
var m *Metadata
for _, v := range q.fs.md {
if v.Atime != 0 && v.f != nil && v.Atime < atime {
atime = v.Atime
m = v
}
}
if m != nil {
log := q.log.WithValues("file", m.name)
log.Info("deleting oldest file")
err := m.Delete()
if err != nil {
q.log.Error(err, "error deleting oldest file")
return
}
atomic.AddInt64(&q.cur, -m.Size)
}
q.log.Info("quota usage", "current", atomic.LoadInt64(&q.cur), "max", q.max)
defer q.mu.Unlock()
size := q.fs.mh.DeleteOldest()
atomic.AddInt64(&q.cur, -size)
}
func (q *Quota) Add(n int) {