sync.go 881 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package flatfs

import (
	"os"
	"runtime"
)

// don't block more than 16 threads on sync opearation
// 16 should be able to sataurate most RAIDs
// in case of two used disks per write (RAID 1, 5) and queue depth of 2,
// 16 concurrent Sync calls should be able to saturate 16 HDDs RAID
//TODO: benchmark it out, maybe provide tweak parmeter
var syncSemaphore chan struct{} = make(chan struct{}, 16)

func syncDir(dir string) error {
	if runtime.GOOS == "windows" {
		// dir sync on windows doesn't work: https://git.io/vPnCI
		return nil
	}

	dirF, err := os.Open(dir)
	if err != nil {
		return err
	}
	defer dirF.Close()

	syncSemaphore <- struct{}{}
	defer func() { <-syncSemaphore }()

	if err := dirF.Sync(); err != nil {
		return err
	}
	return nil
}

func syncFile(file *os.File) error {
	syncSemaphore <- struct{}{}
	defer func() { <-syncSemaphore }()
	return file.Sync()
}