add 20seccy timeout
Jes Olson j3s@c3f.net
Wed, 14 Aug 2024 13:39:44 -0400
2 files changed,
19 insertions(+),
5 deletions(-)
M
reaper/reaper.go
→
reaper/reaper.go
@@ -1,6 +1,7 @@
package reaper import ( + "context" "errors" "log" "runtime"@@ -105,8 +106,15 @@
// refreshFeed triggers a fetch on the given feed, // and sets a fetch error in the db if there is one. func (r *Reaper) refreshFeed(f *rss.Feed) { - err := f.Update() + // 20 seccy timeout + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + + err := f.Update(ctx) if err != nil { + if ctx.Err() == context.DeadlineExceeded { + log.Printf("reaper: %s timed out after 20 seconds", f.UpdateURL) + } r.handleFeedFetchFailure(f.UpdateURL, err) } }
M
rss/rss.go
→
rss/rss.go
@@ -2,6 +2,7 @@ package rss
import ( "bytes" + "context" "errors" "fmt" "io"@@ -134,11 +135,16 @@ // The default value is 12 hours.
var DefaultRefreshInterval = 12 * time.Hour // Update fetches any new items and updates f. -func (f *Feed) Update() error { - if f.FetchFunc == nil { - f.FetchFunc = DefaultFetchFunc +func (f *Feed) Update(ctx context.Context) error { + select { + case <-ctx.Done(): + return ctx.Err() // Return the context error if it was canceled or timed out + default: + if f.FetchFunc == nil { + f.FetchFunc = DefaultFetchFunc + } + return f.UpdateByFunc(f.FetchFunc) } - return f.UpdateByFunc(f.FetchFunc) } // UpdateByFunc uses a func to update f.