Skip to content

Commit

Permalink
Fix: add recycleLock preventing recycle FileSystem used by another go…
Browse files Browse the repository at this point in the history
…routine
  • Loading branch information
HFO4 committed Nov 21, 2020
1 parent 11c218e commit d97bc26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ type FileSystem struct {
文件系统处理适配器
*/
Handler Handler

// 回收锁
recycleLock sync.Mutex
}

// getEmptyFS 从pool中获取新的FileSystem
Expand All @@ -107,6 +110,7 @@ func getEmptyFS() *FileSystem {

// Recycle 回收FileSystem资源
func (fs *FileSystem) Recycle() {
fs.recycleLock.Lock()
fs.reset()
FSPool.Put(fs)
}
Expand All @@ -120,6 +124,7 @@ func (fs *FileSystem) reset() {
fs.Handler = nil
fs.Root = nil
fs.Lock = sync.Mutex{}
fs.recycleLock = sync.Mutex{}
}

// NewFileSystem 初始化一个文件系统
Expand Down
8 changes: 7 additions & 1 deletion pkg/filesystem/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ func GenericAfterUpdate(ctx context.Context, fs *FileSystem) error {

// 尝试清空原有缩略图并重新生成
if originFile.GetPolicy().IsThumbGenerateNeeded() {
fs.recycleLock.Lock()
go func() {
defer fs.recycleLock.Unlock()
if originFile.PicInfo != "" {
_, _ = fs.Handler.Delete(ctx, []string{originFile.SourceName + conf.ThumbConfig.FileSuffix})
fs.GenerateThumbnail(ctx, &originFile)
Expand Down Expand Up @@ -297,7 +299,11 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {

// 异步尝试生成缩略图
if fs.User.Policy.IsThumbGenerateNeeded() {
go fs.GenerateThumbnail(ctx, file)
fs.recycleLock.Lock()
go func() {
defer fs.recycleLock.Unlock()
fs.GenerateThumbnail(ctx, file)
}()
}

return nil
Expand Down

0 comments on commit d97bc26

Please sign in to comment.