Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manager: use Downloader for static feeds #11

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"crypto/sha256"
"errors"
"fmt"
"io/ioutil"
"net/http"
"sort"
"time"

Expand All @@ -20,6 +18,8 @@ const (
DefaultRealtimeTTL = 1 * time.Minute
DefaultRealtimeTimeout = 30 * time.Second
DefaultRealtimeMaxSize = 1 << 20 // 1 MB
DefaultStaticTimeout = 60 * time.Second
DefaultStaticMaxSize = 800 << 20 // 800 MB
)

var ErrNoActiveFeed = errors.New("no active feed found")
Expand All @@ -30,6 +30,8 @@ type Manager struct {
RealtimeTTL time.Duration
RealtimeTimeout time.Duration
RealtimeMaxSize int
StaticTimeout time.Duration
StaticMaxSize int
Downloader downloader.Downloader
storage storage.Storage
}
Expand All @@ -39,16 +41,18 @@ type Manager struct {
// By default, the manager will use a an in memory cache for realtime
// data, but not for static schedules as these will be persisted in
// storage.
func NewManager(storage storage.Storage) *Manager {
func NewManager(s storage.Storage) *Manager {
return &Manager{
RefreshInterval: DefaultStaticRefreshInterval,
RealtimeTTL: DefaultRealtimeTTL,
RealtimeTimeout: DefaultRealtimeTimeout,
RealtimeMaxSize: DefaultRealtimeMaxSize,
StaticTimeout: DefaultStaticTimeout,
StaticMaxSize: DefaultStaticMaxSize,

Downloader: downloader.NewMemoryDownloader(),

storage: storage,
storage: s,
}
}

Expand Down Expand Up @@ -138,6 +142,8 @@ func (m *Manager) LoadRealtime(
downloader.GetOptions{
Cache: true,
CacheTTL: m.RealtimeTTL,
Timeout: m.RealtimeTimeout,
MaxSize: m.RealtimeMaxSize,
},
)
if err != nil {
Expand Down Expand Up @@ -232,21 +238,22 @@ func (m *Manager) refreshStatic(url string) (*storage.FeedMetadata, error) {
// TODO: add support for ETag?

// GET the feed
client := http.Client{Timeout: 60 * time.Second}
resp, err := client.Get(url)
body, err := m.Downloader.Get(
context.Background(),
url,
nil,
downloader.GetOptions{
Cache: false,
Timeout: m.StaticTimeout,
MaxSize: m.StaticMaxSize,
},
)
if err != nil {
return nil, fmt.Errorf("downloading: %w", err)
}
defer resp.Body.Close()

// Compute SHA256 of body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading: %w", err)
}
hash := fmt.Sprintf("%x", sha256.Sum256(body))

// Check if this exact feed is already in storage
hash := fmt.Sprintf("%x", sha256.Sum256(body))
feeds, err := m.storage.ListFeeds(storage.ListFeedsFilter{SHA256: hash})
if err != nil {
return nil, fmt.Errorf("listing feeds: %w", err)
Expand Down