Files
EdgeNode/internal/caches/open_file_cache.go

173 lines
3.5 KiB
Go
Raw Normal View History

2022-01-12 21:09:00 +08:00
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package caches
import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
"github.com/fsnotify/fsnotify"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
"path/filepath"
2022-03-31 13:30:52 +08:00
"runtime"
2022-01-12 21:09:00 +08:00
"sync"
"time"
)
type OpenFileCache struct {
poolMap map[string]*OpenFilePool // file path => Pool
2023-10-03 21:38:45 +08:00
poolList *linkedlist.List[*OpenFilePool]
2022-01-12 21:09:00 +08:00
watcher *fsnotify.Watcher
2022-11-08 21:37:20 +08:00
locker sync.RWMutex
2022-01-12 21:09:00 +08:00
maxSize int
count int
}
func NewOpenFileCache(maxSize int) (*OpenFileCache, error) {
if maxSize <= 0 {
maxSize = 16384
}
var cache = &OpenFileCache{
maxSize: maxSize,
poolMap: map[string]*OpenFilePool{},
2023-10-03 21:38:45 +08:00
poolList: linkedlist.NewList[*OpenFilePool](),
2022-01-12 21:09:00 +08:00
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
cache.watcher = watcher
goman.New(func() {
for event := range watcher.Events {
2022-03-31 13:30:52 +08:00
if runtime.GOOS == "linux" || event.Op&fsnotify.Chmod != fsnotify.Chmod {
2022-01-12 21:09:00 +08:00
cache.Close(event.Name)
}
}
})
return cache, nil
}
func (this *OpenFileCache) Get(filename string) *OpenFile {
2022-11-08 21:37:20 +08:00
this.locker.RLock()
2022-01-12 21:09:00 +08:00
pool, ok := this.poolMap[filename]
2022-11-08 21:37:20 +08:00
this.locker.RUnlock()
2022-01-12 21:09:00 +08:00
if ok {
file, consumed := pool.Get()
if consumed {
2022-11-08 21:37:20 +08:00
this.locker.Lock()
2022-01-12 21:09:00 +08:00
this.count--
2022-11-25 14:52:04 +08:00
// pool如果为空也不需要从列表中删除避免put时需要重新创建
2022-11-08 21:37:20 +08:00
this.locker.Unlock()
2022-01-12 21:09:00 +08:00
}
return file
}
return nil
}
func (this *OpenFileCache) Put(filename string, file *OpenFile) {
this.locker.Lock()
defer this.locker.Unlock()
pool, ok := this.poolMap[filename]
var success bool
if ok {
success = pool.Put(file)
} else {
_ = this.watcher.Add(filename)
pool = NewOpenFilePool(filename)
pool.version = file.version
2022-01-12 21:09:00 +08:00
this.poolMap[filename] = pool
success = pool.Put(file)
}
this.poolList.Push(pool.linkItem)
// 检查长度
if success {
this.count++
2022-01-12 21:41:05 +08:00
// 如果超过当前容量,则关闭最早的
if this.count > this.maxSize {
var delta = this.maxSize / 100 // 清理1%
if delta == 0 {
delta = 1
2022-01-12 21:09:00 +08:00
}
2022-01-12 21:41:05 +08:00
for i := 0; i < delta; i++ {
var head = this.poolList.Head()
if head == nil {
break
}
2022-01-12 21:09:00 +08:00
2023-10-03 21:38:45 +08:00
var headPool = head.Value
2022-01-12 21:41:05 +08:00
headFile, consumed := headPool.Get()
if consumed {
this.count--
if headFile != nil {
_ = headFile.Close()
}
2022-01-12 21:09:00 +08:00
}
2022-01-12 21:41:05 +08:00
if headPool.Len() == 0 {
delete(this.poolMap, headPool.filename)
this.poolList.Remove(head)
_ = this.watcher.Remove(headPool.filename)
}
2022-01-12 21:09:00 +08:00
}
}
}
}
func (this *OpenFileCache) Close(filename string) {
this.locker.Lock()
pool, ok := this.poolMap[filename]
if ok {
2022-12-05 11:16:04 +08:00
// 设置关闭状态
pool.SetClosing()
2022-01-12 21:09:00 +08:00
delete(this.poolMap, filename)
this.poolList.Remove(pool.linkItem)
_ = this.watcher.Remove(filename)
this.count -= pool.Len()
}
this.locker.Unlock()
// 在locker之外提升性能
if ok {
pool.Close()
}
}
func (this *OpenFileCache) CloseAll() {
this.locker.Lock()
for _, pool := range this.poolMap {
pool.Close()
}
this.poolMap = map[string]*OpenFilePool{}
this.poolList.Reset()
_ = this.watcher.Close()
2022-11-25 14:52:04 +08:00
this.count = 0
2022-01-12 21:09:00 +08:00
this.locker.Unlock()
}
func (this *OpenFileCache) Debug() {
var ticker = time.NewTicker(5 * time.Second)
goman.New(func() {
for range ticker.C {
logs.Println("==== " + types.String(this.count) + " ====")
2023-10-03 21:38:45 +08:00
this.poolList.Range(func(item *linkedlist.Item[*OpenFilePool]) (goNext bool) {
logs.Println(filepath.Base(item.Value.Filename()), item.Value.Len())
2022-01-12 21:09:00 +08:00
return true
})
}
})
}