Skip to content

Commit dd6e2c8

Browse files
authored
deploy: walkLocal worker pool for performance
1 parent 7624176 commit dd6e2c8

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

‎deploy/deploy.go‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/dustin/go-humanize"
3737
"github.com/gobwas/glob"
3838
"github.com/gohugoio/hugo/common/loggers"
39+
"github.com/gohugoio/hugo/common/para"
3940
"github.com/gohugoio/hugo/config"
4041
"github.com/gohugoio/hugo/deploy/deployconfig"
4142
"github.com/gohugoio/hugo/media"
@@ -487,7 +488,12 @@ func knownHiddenDirectory(name string) bool {
487488
// walkLocal walks the source directory and returns a flat list of files,
488489
// using localFile.SlashPath as the map keys.
489490
func (d *Deployer) walkLocal(fs afero.Fs, matchers []*deployconfig.Matcher, include, exclude glob.Glob, mediaTypes media.Types, mappath func(string) string) (map[string]*localFile, error) {
490-
retval := map[string]*localFile{}
491+
retval := make(map[string]*localFile)
492+
var mu sync.Mutex
493+
494+
workers := para.New(d.cfg.Workers)
495+
g, _ := workers.Start(context.Background())
496+
491497
err := afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
492498
if err != nil {
493499
return err
@@ -508,45 +514,54 @@ func (d *Deployer) walkLocal(fs afero.Fs, matchers []*deployconfig.Matcher, incl
508514
return nil
509515
}
510516

511-
// When a file system is HFS+, its filepath is in NFD form.
512-
if runtime.GOOS == "darwin" {
513-
path = norm.NFC.String(path)
514-
}
517+
// Process each file in a worker
518+
g.Run(func() error {
519+
// When a file system is HFS+, its filepath is in NFD form.
520+
if runtime.GOOS == "darwin" {
521+
path = norm.NFC.String(path)
522+
}
515523

516-
// Check include/exclude matchers.
517-
slashpath := filepath.ToSlash(path)
518-
if include != nil && !include.Match(slashpath) {
519-
d.logger.Infof(" dropping %q due to include\n", slashpath)
520-
return nil
521-
}
522-
if exclude != nil && exclude.Match(slashpath) {
523-
d.logger.Infof(" dropping %q due to exclude\n", slashpath)
524-
return nil
525-
}
524+
// Check include/exclude matchers.
525+
slashpath := filepath.ToSlash(path)
526+
if include != nil && !include.Match(slashpath) {
527+
d.logger.Infof(" dropping %q due to include\n", slashpath)
528+
return nil
529+
}
530+
if exclude != nil && exclude.Match(slashpath) {
531+
d.logger.Infof(" dropping %q due to exclude\n", slashpath)
532+
return nil
533+
}
526534

527-
// Find the first matching matcher (if any).
528-
var m *deployconfig.Matcher
529-
for _, cur := range matchers {
530-
if cur.Matches(slashpath) {
531-
m = cur
532-
break
535+
// Find the first matching matcher (if any).
536+
var m *deployconfig.Matcher
537+
for _, cur := range matchers {
538+
if cur.Matches(slashpath) {
539+
m = cur
540+
break
541+
}
533542
}
534-
}
535-
// Apply any additional modifications to the local path, to map it to
536-
// the remote path.
537-
if mappath != nil {
538-
slashpath = mappath(slashpath)
539-
}
540-
lf, err := newLocalFile(fs, path, slashpath, m, mediaTypes)
541-
if err != nil {
542-
return err
543-
}
544-
retval[lf.SlashPath] = lf
543+
// Apply any additional modifications to the local path, to map it to
544+
// the remote path.
545+
if mappath != nil {
546+
slashpath = mappath(slashpath)
547+
}
548+
lf, err := newLocalFile(fs, path, slashpath, m, mediaTypes)
549+
if err != nil {
550+
return err
551+
}
552+
mu.Lock()
553+
retval[lf.SlashPath] = lf
554+
mu.Unlock()
555+
return nil
556+
})
545557
return nil
546558
})
547559
if err != nil {
548560
return nil, err
549561
}
562+
if err := g.Wait(); err != nil {
563+
return nil, err
564+
}
550565
return retval, nil
551566
}
552567

‎deploy/deploy_test.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ func TestEndToEndSync(t *testing.T) {
623623
localFs: test.fs,
624624
bucket: test.bucket,
625625
mediaTypes: media.DefaultTypes,
626-
cfg: deployconfig.DeployConfig{MaxDeletes: -1},
626+
cfg: deployconfig.DeployConfig{Workers: 2, MaxDeletes: -1},
627627
}
628628

629629
// Initial deployment should sync remote with local.
@@ -706,7 +706,7 @@ func TestMaxDeletes(t *testing.T) {
706706
localFs: test.fs,
707707
bucket: test.bucket,
708708
mediaTypes: media.DefaultTypes,
709-
cfg: deployconfig.DeployConfig{MaxDeletes: -1},
709+
cfg: deployconfig.DeployConfig{Workers: 2, MaxDeletes: -1},
710710
}
711711

712712
// Sync remote with local.
@@ -836,7 +836,7 @@ func TestIncludeExclude(t *testing.T) {
836836
}
837837
deployer := &Deployer{
838838
localFs: fsTest.fs,
839-
cfg: deployconfig.DeployConfig{MaxDeletes: -1}, bucket: fsTest.bucket,
839+
cfg: deployconfig.DeployConfig{Workers: 2, MaxDeletes: -1}, bucket: fsTest.bucket,
840840
target: tgt,
841841
mediaTypes: media.DefaultTypes,
842842
}
@@ -893,7 +893,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) {
893893
}
894894
deployer := &Deployer{
895895
localFs: fsTest.fs,
896-
cfg: deployconfig.DeployConfig{MaxDeletes: -1}, bucket: fsTest.bucket,
896+
cfg: deployconfig.DeployConfig{Workers: 2, MaxDeletes: -1}, bucket: fsTest.bucket,
897897
mediaTypes: media.DefaultTypes,
898898
}
899899

@@ -945,7 +945,7 @@ func TestCompression(t *testing.T) {
945945
deployer := &Deployer{
946946
localFs: test.fs,
947947
bucket: test.bucket,
948-
cfg: deployconfig.DeployConfig{MaxDeletes: -1, Matchers: []*deployconfig.Matcher{{Pattern: ".*", Gzip: true, Re: regexp.MustCompile(".*")}}},
948+
cfg: deployconfig.DeployConfig{Workers: 2, MaxDeletes: -1, Matchers: []*deployconfig.Matcher{{Pattern: ".*", Gzip: true, Re: regexp.MustCompile(".*")}}},
949949
mediaTypes: media.DefaultTypes,
950950
}
951951

@@ -1000,7 +1000,7 @@ func TestMatching(t *testing.T) {
10001000
deployer := &Deployer{
10011001
localFs: test.fs,
10021002
bucket: test.bucket,
1003-
cfg: deployconfig.DeployConfig{MaxDeletes: -1, Matchers: []*deployconfig.Matcher{{Pattern: "^subdir/aaa$", Force: true, Re: regexp.MustCompile("^subdir/aaa$")}}},
1003+
cfg: deployconfig.DeployConfig{Workers: 2, MaxDeletes: -1, Matchers: []*deployconfig.Matcher{{Pattern: "^subdir/aaa$", Force: true, Re: regexp.MustCompile("^subdir/aaa$")}}},
10041004
mediaTypes: media.DefaultTypes,
10051005
}
10061006

@@ -1097,5 +1097,6 @@ func verifyRemote(ctx context.Context, bucket *blob.Bucket, local []*fileData) (
10971097
func newDeployer() *Deployer {
10981098
return &Deployer{
10991099
logger: loggers.NewDefault(),
1100+
cfg: deployconfig.DeployConfig{Workers: 2},
11001101
}
11011102
}

0 commit comments

Comments
 (0)