diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-26 17:44:20 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-26 17:44:20 +0200 |
commit | 9e69c102b596924d589693ce537c4fecae3aa44c (patch) | |
tree | bc43276b03f1f92da51e41b48ebe5fa5adb437c7 | |
parent | 835d5f3547f10ede1997a938d9278a506a089bb7 (diff) | |
download | feed2imap-go-9e69c102b596924d589693ce537c4fecae3aa44c.tar.gz feed2imap-go-9e69c102b596924d589693ce537c4fecae3aa44c.tar.bz2 feed2imap-go-9e69c102b596924d589693ce537c4fecae3aa44c.zip |
Handle the waitGroup internally in ForeachGo
-rw-r--r-- | internal/feed/parse.go | 4 | ||||
-rw-r--r-- | internal/feed/state.go | 20 | ||||
-rw-r--r-- | main.go | 9 |
3 files changed, 13 insertions, 20 deletions
diff --git a/internal/feed/parse.go b/internal/feed/parse.go index d7b20ad..c027bd3 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -3,7 +3,6 @@ package feed import ( ctxt "context" "fmt" - "sync" "time" "github.com/mmcdole/gofeed" @@ -32,8 +31,7 @@ func parseFeed(feed *Feed) error { return nil } -func handleFeed(feed *Feed, group *sync.WaitGroup) { - defer group.Done() +func handleFeed(feed *Feed) { log.Printf("Fetching %s from %s", feed.Name, feed.Url) err := parseFeed(feed) diff --git a/internal/feed/state.go b/internal/feed/state.go index 8ec8dfd..6f284ad 100644 --- a/internal/feed/state.go +++ b/internal/feed/state.go @@ -19,12 +19,17 @@ func (state *State) Foreach(f func(*Feed)) { } } -func (state *State) ForeachGo(goFunc func(*Feed, *sync.WaitGroup)) { +func (state *State) ForeachGo(goFunc func(*Feed)) { var wg sync.WaitGroup wg.Add(len(state.feeds)) + f := func(feed *Feed, wg *sync.WaitGroup) { + goFunc(feed) + wg.Done() + } + for _, feed := range state.feeds { - go goFunc(feed, &wg) + go f(feed, &wg) } wg.Wait() } @@ -62,7 +67,7 @@ func (state *State) Fetch() int { return ctr } -func filterFeed(feed *Feed, group *sync.WaitGroup) { +func filterFeed(feed *Feed) { if len(feed.items) > 0 { origLen := len(feed.items) @@ -80,19 +85,12 @@ func filterFeed(feed *Feed, group *sync.WaitGroup) { } else { log.Debugf("No items for %s. No filtering.", feed.Name) } - - if group != nil { - // group is nil in debug case - group.Done() - } } func (state *State) Filter() { if log.IsDebug() { // single threaded for better output - state.Foreach(func(f *Feed) { - filterFeed(f, nil) - }) + state.Foreach(filterFeed) } else { state.ForeachGo(filterFeed) } @@ -5,7 +5,6 @@ import ( "fmt" "net/url" "os" - "sync" "github.com/Necoro/feed2imap-go/internal/feed" "github.com/Necoro/feed2imap-go/internal/imap" @@ -18,9 +17,7 @@ var cacheFile = flag.String("c", "feed.cache", "cache file") var verbose = flag.Bool("v", false, "enable verbose output") var debug = flag.Bool("d", false, "enable debug output") -func processFeed(feed *feed.Feed, cfg *config.Config, client *imap.Client, wg *sync.WaitGroup) { - defer wg.Done() - +func processFeed(feed *feed.Feed, cfg *config.Config, client *imap.Client) { mails, err := feed.ToMails(cfg) if err != nil { log.Errorf("Processing items of feed %s: %s", feed.Name, err) @@ -99,8 +96,8 @@ func run() error { defer c.Disconnect() - state.ForeachGo(func(f *feed.Feed, wg *sync.WaitGroup) { - processFeed(f, cfg, c, wg) + state.ForeachGo(func(f *feed.Feed) { + processFeed(f, cfg, c) }) if err = state.StoreCache(*cacheFile); err != nil { |