From 63992bb2a00f6a214c5aaa102a6eb3e85af15d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 12 Jan 2022 21:41:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/open_file_cache.go | 40 +++++++++++++++++------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/internal/caches/open_file_cache.go b/internal/caches/open_file_cache.go index 4b03ea9..b01cab7 100644 --- a/internal/caches/open_file_cache.go +++ b/internal/caches/open_file_cache.go @@ -86,26 +86,32 @@ func (this *OpenFileCache) Put(filename string, file *OpenFile) { if success { this.count++ - // 如果超过当前容量,则关闭多余的 - for this.count > this.maxSize { - var head = this.poolList.Head() - if head == nil { - break + // 如果超过当前容量,则关闭最早的 + if this.count > this.maxSize { + var delta = this.maxSize / 100 // 清理1% + if delta == 0 { + delta = 1 } - - var headPool = head.Value.(*OpenFilePool) - headFile, consumed := headPool.Get() - if consumed { - this.count-- - if headFile != nil { - _ = headFile.Close() + for i := 0; i < delta; i++ { + var head = this.poolList.Head() + if head == nil { + break } - } - if headPool.Len() == 0 { - delete(this.poolMap, headPool.filename) - this.poolList.Remove(head) - _ = this.watcher.Remove(headPool.filename) + var headPool = head.Value.(*OpenFilePool) + headFile, consumed := headPool.Get() + if consumed { + this.count-- + if headFile != nil { + _ = headFile.Close() + } + } + + if headPool.Len() == 0 { + delete(this.poolMap, headPool.filename) + this.poolList.Remove(head) + _ = this.watcher.Remove(headPool.filename) + } } } }