mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	修复OpenFileCache可能无法更新的Bug
This commit is contained in:
		@@ -14,11 +14,12 @@ type OpenFile struct {
 | 
				
			|||||||
	version int64
 | 
						version int64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewOpenFile(fp *os.File, meta []byte, header []byte) *OpenFile {
 | 
					func NewOpenFile(fp *os.File, meta []byte, header []byte, version int64) *OpenFile {
 | 
				
			||||||
	return &OpenFile{
 | 
						return &OpenFile{
 | 
				
			||||||
		fp:     fp,
 | 
							fp:      fp,
 | 
				
			||||||
		meta:   meta,
 | 
							meta:    meta,
 | 
				
			||||||
		header: header,
 | 
							header:  header,
 | 
				
			||||||
 | 
							version: version,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -77,6 +77,7 @@ func (this *OpenFileCache) Put(filename string, file *OpenFile) {
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		_ = this.watcher.Add(filename)
 | 
							_ = this.watcher.Add(filename)
 | 
				
			||||||
		pool = NewOpenFilePool(filename)
 | 
							pool = NewOpenFilePool(filename)
 | 
				
			||||||
 | 
							pool.version = file.version
 | 
				
			||||||
		this.poolMap[filename] = pool
 | 
							this.poolMap[filename] = pool
 | 
				
			||||||
		success = pool.Put(file)
 | 
							success = pool.Put(file)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -45,7 +45,7 @@ func (this *OpenFilePool) Get() (*OpenFile, bool) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *OpenFilePool) Put(file *OpenFile) bool {
 | 
					func (this *OpenFilePool) Put(file *OpenFile) bool {
 | 
				
			||||||
	if file.version > 0 && file.version != this.version {
 | 
						if this.version > 0 && file.version > 0 && file.version != this.version {
 | 
				
			||||||
		_ = file.Close()
 | 
							_ = file.Close()
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -348,7 +348,7 @@ func (this *FileReader) Close() error {
 | 
				
			|||||||
		if this.openFile != nil {
 | 
							if this.openFile != nil {
 | 
				
			||||||
			this.openFileCache.Put(this.fp.Name(), this.openFile)
 | 
								this.openFileCache.Put(this.fp.Name(), this.openFile)
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			this.openFileCache.Put(this.fp.Name(), NewOpenFile(this.fp, this.meta, this.header))
 | 
								this.openFileCache.Put(this.fp.Name(), NewOpenFile(this.fp, this.meta, this.header, this.LastModified()))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -367,5 +367,12 @@ func (this *FileReader) readToBuff(fp *os.File, buf []byte) (ok bool, err error)
 | 
				
			|||||||
func (this *FileReader) discard() error {
 | 
					func (this *FileReader) discard() error {
 | 
				
			||||||
	_ = this.fp.Close()
 | 
						_ = this.fp.Close()
 | 
				
			||||||
	this.isClosed = true
 | 
						this.isClosed = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// close open file cache
 | 
				
			||||||
 | 
						if this.openFileCache != nil {
 | 
				
			||||||
 | 
							this.openFileCache.Close(this.fp.Name())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// remove file
 | 
				
			||||||
	return os.Remove(this.fp.Name())
 | 
						return os.Remove(this.fp.Name())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -446,6 +446,13 @@ func (this *FileStorage) OpenWriter(key string, expiredAt int64, status int, siz
 | 
				
			|||||||
	// 检查缓存是否已经生成
 | 
						// 检查缓存是否已经生成
 | 
				
			||||||
	var cachePathName = dir + "/" + hash
 | 
						var cachePathName = dir + "/" + hash
 | 
				
			||||||
	var cachePath = cachePathName + ".cache"
 | 
						var cachePath = cachePathName + ".cache"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 关闭OpenFileCache
 | 
				
			||||||
 | 
						var openFileCache = this.openFileCache
 | 
				
			||||||
 | 
						if openFileCache != nil {
 | 
				
			||||||
 | 
							openFileCache.Close(cachePath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	stat, err := os.Stat(cachePath)
 | 
						stat, err := os.Stat(cachePath)
 | 
				
			||||||
	if err == nil && time.Now().Sub(stat.ModTime()) <= 1*time.Second {
 | 
						if err == nil && time.Now().Sub(stat.ModTime()) <= 1*time.Second {
 | 
				
			||||||
		// 防止并发连续写入
 | 
							// 防止并发连续写入
 | 
				
			||||||
@@ -785,8 +792,9 @@ func (this *FileStorage) Stop() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	_ = this.list.Close()
 | 
						_ = this.list.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if this.openFileCache != nil {
 | 
						var openFileCache = this.openFileCache
 | 
				
			||||||
		this.openFileCache.CloseAll()
 | 
						if openFileCache != nil {
 | 
				
			||||||
 | 
							openFileCache.CloseAll()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.ignoreKeys.Reset()
 | 
						this.ignoreKeys.Reset()
 | 
				
			||||||
@@ -1178,12 +1186,21 @@ func (this *FileStorage) increaseHit(key string, hash string, reader Reader) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// 删除缓存文件
 | 
					// 删除缓存文件
 | 
				
			||||||
func (this *FileStorage) removeCacheFile(path string) error {
 | 
					func (this *FileStorage) removeCacheFile(path string) error {
 | 
				
			||||||
 | 
						var openFileCache = this.openFileCache
 | 
				
			||||||
 | 
						if openFileCache != nil {
 | 
				
			||||||
 | 
							openFileCache.Close(path)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var err = os.Remove(path)
 | 
						var err = os.Remove(path)
 | 
				
			||||||
	if err == nil || os.IsNotExist(err) {
 | 
						if err == nil || os.IsNotExist(err) {
 | 
				
			||||||
		err = nil
 | 
							err = nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 删除Partial相关
 | 
							// 删除Partial相关
 | 
				
			||||||
		_ = os.Remove(partialRangesFilePath(path))
 | 
							var partialPath = partialRangesFilePath(path)
 | 
				
			||||||
 | 
							if openFileCache != nil {
 | 
				
			||||||
 | 
								openFileCache.Close(partialPath)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							_ = os.Remove(partialPath)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user