From b0eb2843473da062fd07f60662f0638b742104ee Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 11 Oct 2016 15:36:15 +0100 Subject: [PATCH 1/2] Add a caching HTTP client --- src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go index a5b157c..4158f9c 100644 --- a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go +++ b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go @@ -4,15 +4,19 @@ import ( "errors" "fmt" log "github.com/Sirupsen/logrus" + "github.com/gregjones/httpcache" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/polling" "github.com/matrix-org/go-neb/types" "github.com/mmcdole/gofeed" "html" + "net/http" "time" ) +var cachingClient *http.Client + const minPollingIntervalSeconds = 60 // 1 min (News feeds can be genuinely spammy) type rssBotService struct { @@ -50,6 +54,7 @@ func (s *rssBotService) Register(oldService types.Service, client *matrix.Client // Make sure we can parse the feed for feedURL, feedInfo := range s.Feeds { fp := gofeed.NewParser() + fp.Client = cachingClient if _, err := fp.ParseURL(feedURL); err != nil { return fmt.Errorf("Failed to read URL %s: %s", feedURL, err.Error()) } @@ -138,6 +143,7 @@ func (s *rssBotService) queryFeed(feedURL string) (*gofeed.Feed, []gofeed.Item, log.WithField("feed_url", feedURL).Info("Querying feed") var items []gofeed.Item fp := gofeed.NewParser() + fp.Client = cachingClient feed, err := fp.ParseURL(feedURL) if err != nil { return nil, items, err @@ -214,6 +220,7 @@ func itemToHTML(feed *gofeed.Feed, item gofeed.Item) matrix.HTMLMessage { } func init() { + cachingClient = httpcache.NewMemoryCacheTransport().Client() types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { r := &rssBotService{ id: serviceID, From e088e118211fbd2f05bc7019719cd8ca8fdeed22 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 11 Oct 2016 15:54:40 +0100 Subject: [PATCH 2/2] Swap to an LRU cache set to 20MB --- src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go index 4158f9c..92661e7 100644 --- a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go +++ b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" log "github.com/Sirupsen/logrus" + "github.com/die-net/lrucache" "github.com/gregjones/httpcache" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" @@ -220,7 +221,8 @@ func itemToHTML(feed *gofeed.Feed, item gofeed.Item) matrix.HTMLMessage { } func init() { - cachingClient = httpcache.NewMemoryCacheTransport().Client() + lruCache := lrucache.New(1024*1024*20, 0) // 20 MB cache, no max-age + cachingClient = httpcache.NewTransport(lruCache).Client() types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { r := &rssBotService{ id: serviceID,