mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	优化OpenFileCache功能
This commit is contained in:
		@@ -62,6 +62,9 @@ func (this *OpenFileCache) Get(filename string) *OpenFile {
 | 
			
		||||
		if consumed {
 | 
			
		||||
			this.locker.Lock()
 | 
			
		||||
			this.count--
 | 
			
		||||
 | 
			
		||||
			// pool如果为空,也不需要从列表中删除,避免put时需要重新创建
 | 
			
		||||
 | 
			
		||||
			this.locker.Unlock()
 | 
			
		||||
		}
 | 
			
		||||
		return file
 | 
			
		||||
@@ -148,6 +151,7 @@ func (this *OpenFileCache) CloseAll() {
 | 
			
		||||
	this.poolMap = map[string]*OpenFilePool{}
 | 
			
		||||
	this.poolList.Reset()
 | 
			
		||||
	_ = this.watcher.Close()
 | 
			
		||||
	this.count = 0
 | 
			
		||||
	this.locker.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								internal/caches/open_file_cache_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								internal/caches/open_file_cache_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
 | 
			
		||||
 | 
			
		||||
package caches_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/caches"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestNewOpenFileCache_Close(t *testing.T) {
 | 
			
		||||
	cache, err := caches.NewOpenFileCache(1024)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	cache.Debug()
 | 
			
		||||
	cache.Put("a.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Put("c.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Get("b.txt")
 | 
			
		||||
	cache.Get("d.txt")
 | 
			
		||||
	cache.Close("a.txt")
 | 
			
		||||
 | 
			
		||||
	time.Sleep(100 * time.Second)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestNewOpenFileCache_CloseAll(t *testing.T) {
 | 
			
		||||
	cache, err := caches.NewOpenFileCache(1024)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	cache.Debug()
 | 
			
		||||
	cache.Put("a.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Put("c.txt", caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	cache.Get("b.txt")
 | 
			
		||||
	cache.Get("d.txt")
 | 
			
		||||
	cache.CloseAll()
 | 
			
		||||
 | 
			
		||||
	time.Sleep(6 * time.Second)
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,8 @@ package caches_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/caches"
 | 
			
		||||
	"github.com/iwind/TeaGo/rands"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -15,3 +17,30 @@ func TestOpenFilePool_Get(t *testing.T) {
 | 
			
		||||
	t.Log(pool.Get())
 | 
			
		||||
	t.Log(pool.Get())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestOpenFilePool_Close(t *testing.T) {
 | 
			
		||||
	var pool = caches.NewOpenFilePool("a")
 | 
			
		||||
	pool.Put(caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	pool.Put(caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
	pool.Close()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestOpenFilePool_Concurrent(t *testing.T) {
 | 
			
		||||
	var pool = caches.NewOpenFilePool("a")
 | 
			
		||||
	var concurrent = 1000
 | 
			
		||||
	var wg = &sync.WaitGroup{}
 | 
			
		||||
	wg.Add(concurrent)
 | 
			
		||||
	for i := 0; i < concurrent; i++ {
 | 
			
		||||
		go func() {
 | 
			
		||||
			defer wg.Done()
 | 
			
		||||
 | 
			
		||||
			if rands.Int(0, 1) == 1 {
 | 
			
		||||
				pool.Put(caches.NewOpenFile(nil, nil, nil, 0))
 | 
			
		||||
			}
 | 
			
		||||
			if rands.Int(0, 1) == 0 {
 | 
			
		||||
				pool.Get()
 | 
			
		||||
			}
 | 
			
		||||
		}()
 | 
			
		||||
	}
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user