mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	bfs: bfs.FS增加锁 (experimental)
This commit is contained in:
		@@ -4,6 +4,7 @@ package bfs
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
						fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -19,20 +20,29 @@ type FS struct {
 | 
				
			|||||||
	isClosed bool
 | 
						isClosed bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	syncTicker *time.Ticker
 | 
						syncTicker *time.Ticker
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						locker *fsutils.Locker
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewFS(dir string, options *FSOptions) *FS {
 | 
					func OpenFS(dir string, options *FSOptions) (*FS, error) {
 | 
				
			||||||
	options.EnsureDefaults()
 | 
						options.EnsureDefaults()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var locker = fsutils.NewLocker(dir + "/fs")
 | 
				
			||||||
 | 
						err := locker.Lock()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var fs = &FS{
 | 
						var fs = &FS{
 | 
				
			||||||
		dir:        dir,
 | 
							dir:        dir,
 | 
				
			||||||
		bMap:       map[string]*BlocksFile{},
 | 
							bMap:       map[string]*BlocksFile{},
 | 
				
			||||||
		mu:         &sync.RWMutex{},
 | 
							mu:         &sync.RWMutex{},
 | 
				
			||||||
		opt:        options,
 | 
							opt:        options,
 | 
				
			||||||
		syncTicker: time.NewTicker(1 * time.Second),
 | 
							syncTicker: time.NewTicker(1 * time.Second),
 | 
				
			||||||
 | 
							locker:     locker,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	go fs.init()
 | 
						go fs.init()
 | 
				
			||||||
	return fs
 | 
						return fs, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *FS) init() {
 | 
					func (this *FS) init() {
 | 
				
			||||||
@@ -47,7 +57,7 @@ func (this *FS) OpenFileWriter(hash string, bodySize int64, isPartial bool) (*Fi
 | 
				
			|||||||
		return nil, errors.New("invalid body size for partial content")
 | 
							return nil, errors.New("invalid body size for partial content")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bFile, err := this.openBFileForWriting(hash)
 | 
						bFile, err := this.openBFileForHashWriting(hash)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -55,7 +65,7 @@ func (this *FS) OpenFileWriter(hash string, bodySize int64, isPartial bool) (*Fi
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *FS) OpenFileReader(hash string, isPartial bool) (*FileReader, error) {
 | 
					func (this *FS) OpenFileReader(hash string, isPartial bool) (*FileReader, error) {
 | 
				
			||||||
	bFile, err := this.openBFileForReading(hash)
 | 
						bFile, err := this.openBFileForHashReading(hash)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -63,7 +73,7 @@ func (this *FS) OpenFileReader(hash string, isPartial bool) (*FileReader, error)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *FS) ExistFile(hash string) (bool, error) {
 | 
					func (this *FS) ExistFile(hash string) (bool, error) {
 | 
				
			||||||
	bFile, err := this.openBFileForReading(hash)
 | 
						bFile, err := this.openBFileForHashReading(hash)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return false, err
 | 
							return false, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -71,7 +81,7 @@ func (this *FS) ExistFile(hash string) (bool, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *FS) RemoveFile(hash string) error {
 | 
					func (this *FS) RemoveFile(hash string) error {
 | 
				
			||||||
	bFile, err := this.openBFileForWriting(hash)
 | 
						bFile, err := this.openBFileForHashWriting(hash)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -90,6 +100,12 @@ func (this *FS) Close() error {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this.mu.Unlock()
 | 
						this.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := this.locker.Release()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							lastErr = err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return lastErr
 | 
						return lastErr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -139,7 +155,7 @@ func (this *FS) syncLoop() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *FS) openBFileForWriting(hash string) (*BlocksFile, error) {
 | 
					func (this *FS) openBFileForHashWriting(hash string) (*BlocksFile, error) {
 | 
				
			||||||
	err := CheckHashErr(hash)
 | 
						err := CheckHashErr(hash)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
@@ -160,7 +176,7 @@ func (this *FS) openBFileForWriting(hash string) (*BlocksFile, error) {
 | 
				
			|||||||
	return this.openBFile(bPath, bName)
 | 
						return this.openBFile(bPath, bName)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *FS) openBFileForReading(hash string) (*BlocksFile, error) {
 | 
					func (this *FS) openBFileForHashReading(hash string) (*BlocksFile, error) {
 | 
				
			||||||
	err := CheckHashErr(hash)
 | 
						err := CheckHashErr(hash)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,7 +13,10 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFS_OpenFileWriter(t *testing.T) {
 | 
					func TestFS_OpenFileWriter(t *testing.T) {
 | 
				
			||||||
	var fs = bfs.NewFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
						fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
				
			||||||
 | 
						if openErr != nil {
 | 
				
			||||||
 | 
							t.Fatal(openErr)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		_ = fs.Close()
 | 
							_ = fs.Close()
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
@@ -54,7 +57,10 @@ func TestFS_OpenFileWriter(t *testing.T) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFS_OpenFileReader(t *testing.T) {
 | 
					func TestFS_OpenFileReader(t *testing.T) {
 | 
				
			||||||
	var fs = bfs.NewFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
						fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
				
			||||||
 | 
						if openErr != nil {
 | 
				
			||||||
 | 
							t.Fatal(openErr)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		_ = fs.Close()
 | 
							_ = fs.Close()
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
@@ -76,7 +82,10 @@ func TestFS_OpenFileReader(t *testing.T) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFS_ExistFile(t *testing.T) {
 | 
					func TestFS_ExistFile(t *testing.T) {
 | 
				
			||||||
	var fs = bfs.NewFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
						fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
				
			||||||
 | 
						if openErr != nil {
 | 
				
			||||||
 | 
							t.Fatal(openErr)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		_ = fs.Close()
 | 
							_ = fs.Close()
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
@@ -89,7 +98,10 @@ func TestFS_ExistFile(t *testing.T) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFS_RemoveFile(t *testing.T) {
 | 
					func TestFS_RemoveFile(t *testing.T) {
 | 
				
			||||||
	var fs = bfs.NewFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
						fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
 | 
				
			||||||
 | 
						if openErr != nil {
 | 
				
			||||||
 | 
							t.Fatal(openErr)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		_ = fs.Close()
 | 
							_ = fs.Close()
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user