Skip to content
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
5 changes: 3 additions & 2 deletions internal/data/polygon/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ func (c *Client) ListActiveTickers(ctx context.Context, market, tickerType strin
return nil, fmt.Errorf("polygon: parse next_url: %w", err)
}

// Rate limit pause: Polygon free tier allows 5 req/min.
// Rate limit pause: Polygon free tier allows 5 req/min, so paginated
// reference-ticker requests need roughly 12s spacing to avoid 429s.
select {
case <-ctx.Done():
return tickers, ctx.Err()
case <-time.After(250 * time.Millisecond):
case <-time.After(12 * time.Second):
}
Comment on lines +123 to 129
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 12s hard-coded pause on every paginated request makes ListActiveTickers potentially very slow for large universes and is not easily testable/configurable (e.g., for paid tiers or different limits). Consider using a configurable rate limiter/delay on the Client (or an injected limiter/clock) so production can still respect free-tier limits while keeping tests fast and allowing other tiers to override the pacing.

Copilot uses AI. Check for mistakes.
}

Expand Down
52 changes: 52 additions & 0 deletions internal/data/polygon/universe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package polygon

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)

func TestListActiveTickersRespectsFreeTierRateLimit(t *testing.T) {
var firstRequestAt time.Time
var serverURL string

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

switch r.URL.Query().Get("cursor") {
case "":
firstRequestAt = time.Now()
_, _ = fmt.Fprintf(w, `{"results":[{"ticker":"AAA","name":"Alpha","primary_exchange":"XNAS","type":"CS","active":true}],"next_url":"%s/v3/reference/tickers?cursor=page-2"}`,
serverURL,
)
case "page-2":
if time.Since(firstRequestAt) < 11*time.Second {
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte(`{"status":"ERROR","request_id":"req-rate","error":"rate limit exceeded"}`))
return
}
_, _ = w.Write([]byte(`{"results":[{"ticker":"BBB","name":"Beta","primary_exchange":"XNYS","type":"CS","active":true}]}`))
Comment on lines +12 to +31
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test enforces the 12s pagination delay by using real wall-clock time (requires >11s between requests), which will make the test suite take at least ~12s to run and can significantly slow CI. Consider making the pagination delay configurable/injectable (e.g., client field or option) and in this test set it to a small value (and adjust the server threshold accordingly), or mock/simulate the sleep so the behavior can be verified without waiting in real time.

Copilot uses AI. Check for mistakes.
default:
w.WriteHeader(http.StatusBadRequest)
}
}))
defer server.Close()
serverURL = server.URL

client := NewClient("test-key", discardLogger())
client.baseURL = server.URL

tickers, err := client.ListActiveTickers(context.Background(), "stocks", "CS")
if err != nil {
t.Fatalf("ListActiveTickers() error = %v", err)
}
if len(tickers) != 2 {
t.Fatalf("ListActiveTickers() count = %d, want 2", len(tickers))
}
if tickers[0].Ticker != "AAA" || tickers[1].Ticker != "BBB" {
t.Fatalf("ListActiveTickers() tickers = %#v, want AAA then BBB", tickers)
}
}
Loading
Loading