Detect stalled files

This commit is contained in:
Alex Yatskov 2015-12-17 17:55:59 +09:00
parent 067b501d49
commit 803f98775b
3 changed files with 31 additions and 21 deletions

View File

@ -28,6 +28,7 @@ import (
"path/filepath" "path/filepath"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
) )
type stage struct { type stage struct {
@ -41,10 +42,11 @@ type goldsmith struct {
stages []*stage stages []*stage
refs map[string]bool refs map[string]bool
mtx sync.Mutex mtx sync.Mutex
count int32 busy int64
stalled int64
} }
func (gs *goldsmith) queueFiles() { func (gs *goldsmith) queueFiles(target uint) {
files := make(chan string) files := make(chan string)
go scanDir(gs.srcDir, files, nil) go scanDir(gs.srcDir, files, nil)
@ -54,6 +56,14 @@ func (gs *goldsmith) queueFiles() {
defer close(s.output) defer close(s.output)
for path := range files { for path := range files {
for {
if gs.busy-gs.stalled >= int64(target) {
time.Sleep(time.Millisecond)
} else {
break
}
}
relPath, err := filepath.Rel(gs.srcDir, path) relPath, err := filepath.Rel(gs.srcDir, path)
if err != nil { if err != nil {
panic(err) panic(err)
@ -67,7 +77,7 @@ func (gs *goldsmith) queueFiles() {
f.Close() f.Close()
} }
s.output <- file s.AddFile(file)
} }
}() }()
} }
@ -117,7 +127,7 @@ func (gs *goldsmith) cleanupFiles() {
func (gs *goldsmith) exportFile(file *File) { func (gs *goldsmith) exportFile(file *File) {
defer func() { defer func() {
file.Buff.Reset() file.Buff.Reset()
gs.decFiles() atomic.AddInt64(&gs.busy, -1)
}() }()
if file.Err != nil { if file.Err != nil {
@ -158,20 +168,8 @@ func (gs *goldsmith) refFile(path string) {
} }
} }
func (gs *goldsmith) incFiles() {
atomic.AddInt32(&gs.count, 1)
}
func (gs *goldsmith) decFiles() {
atomic.AddInt32(&gs.count, -1)
}
func (gs *goldsmith) newStage() *stage { func (gs *goldsmith) newStage() *stage {
s := &stage{ s := &stage{gs: gs, output: make(chan *File)}
gs: gs,
output: make(chan *File),
}
if len(gs.stages) > 0 { if len(gs.stages) > 0 {
s.input = gs.stages[len(gs.stages)-1].output s.input = gs.stages[len(gs.stages)-1].output
} }
@ -200,6 +198,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
} else { } else {
mtx.Lock() mtx.Lock()
batch = append(batch, f) batch = append(batch, f)
atomic.AddInt64(&s.gs.stalled, 1)
mtx.Unlock() mtx.Unlock()
} }
} }
@ -211,7 +210,9 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
} }
for file := range s.input { for file := range s.input {
if file.Err != nil || proc == nil || (accept != nil && !accept.Accept(file)) { if file.Err != nil || accept != nil && !accept.Accept(file) {
s.output <- file
} else if proc == nil {
dispatch(file) dispatch(file)
} else { } else {
wg.Add(1) wg.Add(1)
@ -220,7 +221,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
if proc.Process(s, f) { if proc.Process(s, f) {
dispatch(f) dispatch(f)
} else { } else {
gs.decFiles() atomic.AddInt64(&gs.busy, -1)
} }
}(file) }(file)
} }
@ -231,6 +232,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
if fin != nil { if fin != nil {
if s.err = fin.Finalize(s, batch); s.err == nil { if s.err = fin.Finalize(s, batch); s.err == nil {
for _, file := range batch { for _, file := range batch {
atomic.AddInt64(&s.gs.stalled, -1)
s.output <- file s.output <- file
} }
} }

View File

@ -22,12 +22,14 @@
package goldsmith package goldsmith
import "sync/atomic"
func (s *stage) RefFile(path string) { func (s *stage) RefFile(path string) {
s.gs.refFile(path) s.gs.refFile(path)
} }
func (s *stage) AddFile(file *File) { func (s *stage) AddFile(file *File) {
s.gs.incFiles() atomic.AddInt64(&s.gs.busy, 1)
s.output <- file s.output <- file
} }

View File

@ -24,14 +24,20 @@ package goldsmith
import "bytes" import "bytes"
const TargetFileCount = 32
type Goldsmith interface { type Goldsmith interface {
Chain(p Plugin) Goldsmith Chain(p Plugin) Goldsmith
Complete() ([]*File, []error) Complete() ([]*File, []error)
} }
func New(srcDir, dstDir string) Goldsmith { func New(srcDir, dstDir string) Goldsmith {
return NewThrottled(srcDir, dstDir, TargetFileCount)
}
func NewThrottled(srcDir, dstDir string, targetFileCount uint) Goldsmith {
gs := &goldsmith{srcDir: srcDir, dstDir: dstDir} gs := &goldsmith{srcDir: srcDir, dstDir: dstDir}
gs.queueFiles() gs.queueFiles(targetFileCount)
return gs return gs
} }