awful worker pool implementation
Jes Olson j3s@c3f.net
Wed, 14 Jun 2023 22:06:57 -0500
1 files changed,
10 insertions(+),
5 deletions(-)
jump to
M
reaper/reaper.go
→
reaper/reaper.go
@@ -1,9 +1,9 @@
package reaper import ( + "fmt" "log" "sort" - "sync" "time" "git.j3s.sh/vore/rss"@@ -59,17 +59,22 @@ // UpdateAll fetches every feed & attempts updating them
// asynchronously, then prints the duration of the sync func (r *Reaper) refreshAllFeeds() { start := time.Now() - var wg sync.WaitGroup + semaphore := make(chan struct{}, 20) + for i := range r.feeds { if r.feeds[i].Stale() { - wg.Add(1) + semaphore <- struct{}{} + fmt.Println(r.feeds[i].UpdateURL) + go func(f *rss.Feed) { - defer wg.Done() + // ensure we always free the channel + defer func() { + <-semaphore + }() r.refreshFeed(f) }(r.feeds[i]) } } - wg.Wait() log.Printf("reaper: refresh complete in %s\n", time.Since(start)) }