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

cleanup #14

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
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
41 changes: 17 additions & 24 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ var ErrNoActiveFeed = errors.New("no active feed found")

// Manager manages GTFS data.
type Manager struct {
RealtimeTTL time.Duration
RealtimeTimeout time.Duration
RealtimeMaxSize int
StaticTimeout time.Duration
StaticMaxSize int
RefreshInterval time.Duration
Downloader downloader.Downloader
RealtimeTTL time.Duration
RealtimeTimeout time.Duration
RealtimeMaxSize int
StaticTimeout time.Duration
StaticMaxSize int
StaticRefreshInterval time.Duration
Downloader downloader.Downloader

storage storage.Storage
}
Expand All @@ -47,13 +47,12 @@ type Manager struct {
// storage.
func NewManager(s storage.Storage) *Manager {
return &Manager{
RealtimeTTL: DefaultRealtimeTTL,
RealtimeTimeout: DefaultRealtimeTimeout,
RealtimeMaxSize: DefaultRealtimeMaxSize,
StaticTimeout: DefaultStaticTimeout,
StaticMaxSize: DefaultStaticMaxSize,
RefreshInterval: DefaultStaticRefreshInterval,
// TODO: s/RefreshInterval/StaticRefreshInterval/
RealtimeTTL: DefaultRealtimeTTL,
RealtimeTimeout: DefaultRealtimeTimeout,
RealtimeMaxSize: DefaultRealtimeMaxSize,
StaticTimeout: DefaultStaticTimeout,
StaticMaxSize: DefaultStaticMaxSize,
StaticRefreshInterval: DefaultStaticRefreshInterval,

Downloader: downloader.NewMemoryDownloader(),

Expand Down Expand Up @@ -150,7 +149,7 @@ func (m *Manager) Refresh(ctx context.Context) error {
return fmt.Errorf("listing feeds: %w", err)
}
for _, feed := range feeds {
feedsByHash[feed.SHA256] = append(feedsByHash[feed.SHA256], feed)
feedsByHash[feed.Hash] = append(feedsByHash[feed.Hash], feed)
}

// Check all requests for URLs in need of refreshing
Expand All @@ -161,7 +160,7 @@ func (m *Manager) Refresh(ctx context.Context) error {

errs := []error{}
for _, req := range requests {
if req.RefreshedAt.Before(time.Now().Add(-m.RefreshInterval)) {
if req.RefreshedAt.Before(time.Now().Add(-m.StaticRefreshInterval)) {
err = m.processRequest(req, feedsByHash)
if err != nil {
errs = append(errs, fmt.Errorf("refreshing feed at %s: %w", req.URL, err))
Expand Down Expand Up @@ -256,7 +255,7 @@ func (m *Manager) processRequest(
}

// And write the metadata
metadata.SHA256 = hash
metadata.Hash = hash
metadata.URL = req.URL
metadata.RetrievedAt = time.Now().UTC()

Expand Down Expand Up @@ -295,7 +294,7 @@ func (m *Manager) loadMostRecentActive(feeds []*storage.FeedMetadata, when time.
}

// This is the one!
reader, err := m.storage.GetReader(feeds[i].SHA256)
reader, err := m.storage.GetReader(feeds[i].Hash)
if err != nil {
return nil, fmt.Errorf("getting reader: %w", err)
}
Expand Down Expand Up @@ -325,12 +324,6 @@ func feedActive(feed *storage.FeedMetadata, now time.Time) (bool, error) {
feedTz,
).Format("20060102")

if feed.FeedStartDate != "" && feed.FeedStartDate > todayThere {
return false, nil
}
if feed.FeedEndDate != "" && feed.FeedEndDate < todayThere {
return false, nil
}
if feed.CalendarStartDate > todayThere {
return false, nil
}
Expand Down
12 changes: 6 additions & 6 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func testManagerLoadWithRefresh(t *testing.T, strg storage.Storage) {
// Set a very low refresh interval, and manager will consider
// existing data stale. Refresh, and we'll see the feed 2
// data served.
m.RefreshInterval = time.Duration(0)
m.StaticRefreshInterval = time.Duration(0)
require.NoError(t, m.Refresh(context.Background()))
s2, err = m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
Expand Down Expand Up @@ -583,7 +583,7 @@ func testManagerLoadWithRefresh(t *testing.T, strg storage.Storage) {

// Set a high refresh interval, and refresh. Server should not
// be hit.
m.RefreshInterval = time.Hour
m.StaticRefreshInterval = time.Hour
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, []string{"/static.zip", "/static.zip", "/static.zip"}, server.Requests)

Expand Down Expand Up @@ -648,10 +648,10 @@ func testManagerBrokenData(t *testing.T, strg storage.Storage) {
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 3, len(server.Requests))

// Lower RefreshInterval and make the good zip
// Lower StaticRefreshInterval and make the good zip
// available. Refresh will download the new data.
server.Feeds["/static.zip"] = goodZip
m.RefreshInterval = time.Duration(0)
m.StaticRefreshInterval = time.Duration(0)
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 4, len(server.Requests))

Expand All @@ -662,7 +662,7 @@ func testManagerBrokenData(t *testing.T, strg storage.Storage) {
require.NoError(t, err)
require.Equal(t, 1, len(stops))

// With RefreshInterval at 0, refresh will do a request each
// With StaticRefreshInterval at 0, refresh will do a request each
// time.
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 5, len(server.Requests))
Expand Down Expand Up @@ -850,7 +850,7 @@ func testManagerRespectTimezones(t *testing.T, strg storage.Storage) {
}

// Verifies that manager can refresh a bunch of feeds according to the
// RefreshInterval.
// StaticRefreshInterval.
func testManagerRefreshFeeds(t *testing.T, strg storage.Storage) {
// TODO: write me
}
Expand Down
Loading