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

Renaming for clarity #120

Merged
merged 1 commit into from
May 18, 2023
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
22 changes: 12 additions & 10 deletions pkg/api/product_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import (
)

const (
// Default is the standard real-time stats available to all services
Default = "default"
// OriginInspector is the product name used to determine access to Origin Inspector via the entitlement API
OriginInspector = "origin_inspector"
// ProductDefault represents the standard real-time stats available to all services.
ProductDefault = "default"

// ProductOriginInspector represents the origin inspector stats available via the
// entitlement API.
ProductOriginInspector = "origin_inspector"
)

// Products is the slice of available products supported by real-time stats.
var Products = []string{Default, OriginInspector}
var Products = []string{ProductDefault, ProductOriginInspector}

// Product models the response from the Fastly Product Entitlement API.
type Product struct {
HasAccess bool `json:"has_access"`
Meta struct {
Product struct {
Name string `json:"id"`
} `json:"product"`
}
Expand Down Expand Up @@ -54,7 +56,7 @@ func NewProductCache(client HTTPClient, token string, logger log.Logger) *Produc
// Refresh requests data from the Fastly API and stores data in the cache.
func (p *ProductCache) Refresh(ctx context.Context) error {
for _, product := range Products {
if product == Default {
if product == ProductDefault {
continue
}
uri := fmt.Sprintf("https://api.fastly.com/entitled-products/%s", product)
Expand Down Expand Up @@ -82,10 +84,10 @@ func (p *ProductCache) Refresh(ctx context.Context) error {
return fmt.Errorf("error decoding API product response: %w", err)
}

level.Debug(p.logger).Log("product", response.Meta.Name, "hasAccess", response.HasAccess)
level.Debug(p.logger).Log("product", response.Product.Name, "hasAccess", response.HasAccess)

p.mtx.Lock()
p.products[response.Meta.Name] = response.HasAccess
p.products[response.Product.Name] = response.HasAccess
p.mtx.Unlock()

}
Expand All @@ -96,7 +98,7 @@ func (p *ProductCache) Refresh(ctx context.Context) error {
// HasAccess takes a product as a string and returns a boolean
// based on the response from the Product API.
func (p *ProductCache) HasAccess(product string) bool {
if product == Default {
if product == ProductDefault {
return true
}
p.mtx.Lock()
Expand Down
10 changes: 5 additions & 5 deletions pkg/rt/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type ServiceIdentifier interface {
ServiceIDs() []string
}

// ProductCache represents the api.ProductCache behavior.
type ProductCache interface {
// HasAccesser models the read side of an api.ProductCache.
type HasAccesser interface {
HasAccess(string) bool
}

Expand All @@ -43,7 +43,7 @@ type Manager struct {
token string
metrics MetricsProvider
subscriberOptions []SubscriberOption
productCache ProductCache
productCache HasAccesser
logger log.Logger

mtx sync.RWMutex
Expand All @@ -54,7 +54,7 @@ type Manager struct {
// regular schedule to keep the set of managed subscribers up-to-date. The HTTP
// client, token, metrics, and subscriber options parameters are passed thru to
// constructed subscribers.
func NewManager(ids ServiceIdentifier, client HTTPClient, token string, metrics MetricsProvider, subscriberOptions []SubscriberOption, productCache ProductCache, logger log.Logger) *Manager {
func NewManager(ids ServiceIdentifier, client HTTPClient, token string, metrics MetricsProvider, subscriberOptions []SubscriberOption, productCache HasAccesser, logger log.Logger) *Manager {
return &Manager{
ids: ids,
client: client,
Expand Down Expand Up @@ -160,7 +160,7 @@ func (m *Manager) spawn(serviceID string, product string) interrupt {
done = make(chan error, 1)
)
switch product {
case api.OriginInspector:
case api.ProductOriginInspector:
go func() { done <- fmt.Errorf("origins: %w", subscriber.RunOrigins(ctx)) }()
default:
go func() { done <- fmt.Errorf("realtime: %w", subscriber.RunRealtime(ctx)) }()
Expand Down
4 changes: 2 additions & 2 deletions pkg/rt/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestManager(t *testing.T) {

assertStringSliceEqual(t, []string{}, sortedServiceIDs(manager))

products.update(api.OriginInspector, false)
products.update(api.ProductOriginInspector, false)

cache.update([]api.Service{s1, s2})
manager.Refresh() // create s1, create s2
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestManager(t *testing.T) {
manager.StopAll() // stop s2, stop s3
assertStringSliceEqual(t, []string{}, sortedServiceIDs(manager))

products.update(api.OriginInspector, true)
products.update(api.ProductOriginInspector, true)
cache.update([]api.Service{s1})
manager.Refresh() // create s1 with origin inspector
// expecting the ID twice -- one for each product
Expand Down