Skip to content

Commit

Permalink
Concurrency bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evenh committed Jan 23, 2020
1 parent afd90b6 commit d9f372d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net/url"
"os"
"sync"

"github.com/Azure/azure-storage-blob-go/azblob"
log "github.com/sirupsen/logrus"
Expand All @@ -30,6 +31,7 @@ const maxAzResults = 5000
const channelSize = maxAzResults * 2

func Generate(c *GenerateConfig) {
var wg sync.WaitGroup
files := make(chan *HashdeepEntry, channelSize)
writer := &HashdeepOutputFile{OutputFile: c.OutputFile, PathPrefix: c.Prefix}

Expand All @@ -39,14 +41,17 @@ func Generate(c *GenerateConfig) {
log.Fatalf("Error while configuring output: %+v", err)
}

configureSubscriber(files, writer)
configureSubscriber(files, writer, &wg)
traverseBlobStorage(files, c)

wg.Wait()
log.Info("All done, exiting!")
os.Exit(0)
}

func configureSubscriber(files chan *HashdeepEntry, writer *HashdeepOutputFile) {
func configureSubscriber(files chan *HashdeepEntry, writer *HashdeepOutputFile, wg *sync.WaitGroup) {
count := 0
wg.Add(1)
go func() {
for {
fileEntry, more := <-files
Expand All @@ -56,8 +61,13 @@ func configureSubscriber(files chan *HashdeepEntry, writer *HashdeepOutputFile)
if err != nil {
log.Warn(err)
}

count++
} else {
log.Println("Closing files channel")
writer.Close()
log.Printf("Processed %d entries", count)
wg.Done()
return
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/hashdeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const header = `%%%% HASHDEEP-1.0
Expand Down Expand Up @@ -95,5 +96,7 @@ func (h *HashdeepOutputFile) Close() error {
return errors.Wrapf(err, "Could not close results file '%s'", h.OutputFile)
}

log.Println("Flushed and closed results file")

return nil
}

0 comments on commit d9f372d

Please sign in to comment.