Skip to content

Commit

Permalink
modules: Throttle the "downloading modules …" log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Oct 28, 2023
1 parent e54139c commit 6690409
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
34 changes: 25 additions & 9 deletions modules/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -46,8 +47,6 @@ import (

"github.com/gohugoio/hugo/common/hugio"

"errors"

"github.com/spf13/afero"
)

Expand Down Expand Up @@ -105,10 +104,25 @@ func NewClient(cfg ClientConfig) *Client {
noVendor, _ = hglob.GetGlob(hglob.NormalizePath(cfg.ModuleConfig.NoVendor))
}

var throttleSince time.Time
throttle := func(f func()) {
if throttleSince.IsZero() {
throttleSince = time.Now()
f()
return
}
if time.Since(throttleSince) < 6*time.Second {
return
}
throttleSince = time.Now()
f()
}

return &Client{
fs: fs,
ccfg: cfg,
logger: logger,
throttle: throttle,
noVendor: noVendor,
moduleConfig: mcfg,
environ: env,
Expand All @@ -118,8 +132,9 @@ func NewClient(cfg ClientConfig) *Client {

// Client contains most of the API provided by this package.
type Client struct {
fs afero.Fs
logger loggers.Logger
fs afero.Fs
logger loggers.Logger
throttle func(f func())

noVendor glob.Glob

Expand Down Expand Up @@ -199,7 +214,7 @@ func (c *Client) Vendor() error {
if err := c.rmVendorDir(vendorDir); err != nil {
return err
}
if err := c.fs.MkdirAll(vendorDir, 0755); err != nil {
if err := c.fs.MkdirAll(vendorDir, 0o755); err != nil {
return err
}

Expand Down Expand Up @@ -260,7 +275,7 @@ func (c *Client) Vendor() error {
} else {
targetDir := filepath.Dir(targetFilename)

if err := c.fs.MkdirAll(targetDir, 0755); err != nil {
if err := c.fs.MkdirAll(targetDir, 0o755); err != nil {
return fmt.Errorf("failed to make target dir: %w", err)
}

Expand Down Expand Up @@ -303,7 +318,7 @@ func (c *Client) Vendor() error {
}

if modulesContent.Len() > 0 {
if err := afero.WriteFile(c.fs, filepath.Join(vendorDir, vendorModulesFilename), modulesContent.Bytes(), 0666); err != nil {
if err := afero.WriteFile(c.fs, filepath.Join(vendorDir, vendorModulesFilename), modulesContent.Bytes(), 0o666); err != nil {
return err
}
}
Expand Down Expand Up @@ -558,7 +573,7 @@ func (c *Client) rewriteGoMod(name string, isGoMod map[string]bool) error {
return err
}
if data != nil {
if err := afero.WriteFile(c.fs, filepath.Join(c.ccfg.WorkingDir, name), data, 0666); err != nil {
if err := afero.WriteFile(c.fs, filepath.Join(c.ccfg.WorkingDir, name), data, 0o666); err != nil {
return err
}
}
Expand Down Expand Up @@ -636,7 +651,8 @@ func (c *Client) rmVendorDir(vendorDir string) error {
func (c *Client) runGo(
ctx context.Context,
stdout io.Writer,
args ...string) error {
args ...string,
) error {
if c.goBinaryStatus != 0 {
return nil
}
Expand Down
12 changes: 3 additions & 9 deletions modules/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ package modules

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/bep/debounce"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/loggers"

Expand All @@ -37,8 +37,6 @@ import (

"github.com/rogpeppe/go-internal/module"

"errors"

"github.com/gohugoio/hugo/config"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -124,7 +122,6 @@ func (m ModulesConfig) HasConfigFile() bool {
if len(mod.ConfigFilenames()) > 0 {
return true
}

}
return false
}
Expand Down Expand Up @@ -220,7 +217,6 @@ func (c *collector) getVendoredDir(path string) (vendoredModule, bool) {
}

func (c *collector) add(owner *moduleAdapter, moduleImport Import) (*moduleAdapter, error) {

var (
mod *goModule
moduleDir string
Expand Down Expand Up @@ -509,11 +505,10 @@ LOOP:

func (c *collector) collect() {
defer c.logger.PrintTimerIfDelayed(time.Now(), "hugo: collected modules")
d := debounce.New(2 * time.Second)
d(func() {

c.throttle(func() {
c.logger.Println("hugo: downloading modules …")
})
defer d(func() {})

if err := c.initModules(); err != nil {
c.err = err
Expand Down Expand Up @@ -669,7 +664,6 @@ func (c *collector) normalizeMounts(owner *moduleAdapter, mounts []Mount) ([]Mou
} else {
continue
}

}

// Verify that target points to one of the predefined component dirs
Expand Down

0 comments on commit 6690409

Please sign in to comment.