Skip to content

Commit

Permalink
Allow for the configuration of the size of the fetch queue and workers
Browse files Browse the repository at this point in the history
The defaults should be fine in 99% of cases, but some may want to tune
them down on more powerful machines.
  • Loading branch information
kgaughan committed Dec 17, 2024
1 parent fa7f793 commit f67ca5c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/mercury/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
// Populate the manifest with the contents of the config file
manifest.Populate(config.Feeds)
if !*flags.NoFetch {
manifest.Prime(config.Cache, config.Timeout.Duration)
manifest.Prime(config.Cache, config.Timeout.Duration, config.Parallelism, config.JobQueueDepth)
}
if err := manifest.Save(manifestPath); err != nil {
log.Fatal(err)
Expand Down
40 changes: 27 additions & 13 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"

"github.com/BurntSushi/toml"
"github.com/kgaughan/mercury/internal/manifest"
Expand All @@ -14,19 +15,21 @@ import (

// Config describes our configuration.
type Config struct {
Name string `toml:"name"`
URL string `toml:"url"`
Owner string `toml:"owner"`
Email string `toml:"email"`
FeedID string `toml:"feed_id"`
Cache string `toml:"cache"`
Timeout utils.Duration `toml:"timeout"`
themePath string `toml:"theme"`
Theme fs.FS `toml:"-"`
Output string `toml:"output"`
Feeds []manifest.Feed `toml:"feed"`
ItemsPerPage int `toml:"items"`
MaxPages int `toml:"max_pages"`
Name string `toml:"name"`
URL string `toml:"url"`
Owner string `toml:"owner"`
Email string `toml:"email"`
FeedID string `toml:"feed_id"`
Cache string `toml:"cache"`
Timeout utils.Duration `toml:"timeout"`
themePath string `toml:"theme"`
Theme fs.FS `toml:"-"`
Output string `toml:"output"`
Feeds []manifest.Feed `toml:"feed"`
ItemsPerPage int `toml:"items"`
MaxPages int `toml:"max_pages"`
JobQueueDepth int `toml:"job_queue_depth"`
Parallelism int `toml:"parallelism"`
}

// Load loads our configuration file.
Expand All @@ -37,6 +40,9 @@ func (c *Config) Load(path string) error {
c.Output = "./output"
c.ItemsPerPage = 10
c.MaxPages = 5
// These are both somewhat arbitrary
c.JobQueueDepth = 2 * runtime.NumCPU()
c.Parallelism = runtime.NumCPU()

if _, err := toml.DecodeFile(path, c); err != nil {
return fmt.Errorf("cannot load configuration: %w", err)
Expand All @@ -47,6 +53,14 @@ func (c *Config) Load(path string) error {
return fmt.Errorf("cannot normalize configuration path: %w", err)
}

// Enforce some sensible lower bounds on feed fetching parallelism
if c.Parallelism < 1 {
c.Parallelism = 1
}
if c.JobQueueDepth < c.Parallelism {
c.JobQueueDepth = c.Parallelism
}

c.Cache = filepath.Join(configDir, c.Cache)
if c.themePath == "" {
c.Theme = dflt.Theme
Expand Down
9 changes: 3 additions & 6 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -56,13 +55,11 @@ func (m *Manifest) Save(path string) error {
return nil
}

func (m *Manifest) Prime(cache string, timeout time.Duration) {
func (m *Manifest) Prime(cache string, timeout time.Duration, parallelism, jobQueueDepth int) {
var wg sync.WaitGroup

// The channel depth is kind of arbitrary.
jobs := make(chan *fetchJob, 2*runtime.NumCPU())

for i := 0; i < runtime.NumCPU(); i++ {
jobs := make(chan *fetchJob, jobQueueDepth)
for i := 0; i < parallelism; i++ {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down

0 comments on commit f67ca5c

Please sign in to comment.