small pixel drawing of a pufferfish jrss

Make jrss concurrent lmao
Jes Olson j3s@c3f.net
Mon, 04 Apr 2022 16:59:31 -0400
commit

6387c3457a0b2544f34da3dcd2d6630e47b5dc85

parent

eb552529c422210dd03d5f96ccd84a3aecb4c5ac

1 files changed, 20 insertions(+), 10 deletions(-)

jump to
M main.gomain.go

@@ -10,6 +10,7 @@ "net/http"

"os" "sort" "strings" + "sync" "github.com/mmcdole/gofeed" )

@@ -31,17 +32,26 @@ feeds := make([]gofeed.Feed, 0)

var feed *gofeed.Feed fetchFailures := make([]error, 0) + var wg sync.WaitGroup + var m sync.Mutex + wg.Add(len(scrapedFeeds)) for _, f := range scrapedFeeds { - feed, err = getFeed(f) - if err != nil { - // break loop in case of - // feed fetching failure - err = fmt.Errorf("%s<br>%s", f, err) - fetchFailures = append(fetchFailures, err) - continue - } - feeds = append(feeds, *feed) + go func(f string) { + defer wg.Done() + feed, err = getFeed(f) + if err != nil { + // break loop in case of + // feed fetching failure + err = fmt.Errorf("%s<br>%s", f, err) + m.Lock() + defer m.Unlock() + fetchFailures = append(fetchFailures, err) + return + } + feeds = append(feeds, *feed) + }(f) } + wg.Wait() consolidatedFeed := consolidateFeeds(feeds) // sort posts by date, most recent first

@@ -63,7 +73,7 @@ }

for _, post := range consolidatedFeed.Items { // TODO: Add author name (nil pointer dereference) fmt.Fprintf(w, "<tr>") - fmt.Fprintf(w, "<td>%s: (%s)<a href=\"%s\">%s</a> <sub>%s</sub></td>", + fmt.Fprintf(w, "<td>%s: (%s)<a href=\"%s\">%s</a> <small>%s</small></td>", post.Custom["feed-title"], post.Author.Name, post.Link, post.Title, post.Published) fmt.Fprintf(w, "</tr>") }