Skip to content

Commit

Permalink
renaming for better clarity. (#120)
Browse files Browse the repository at this point in the history
Thanks @peterbourgon for the review comments
  • Loading branch information
leklund committed May 18, 2023
1 parent 0cb708d commit 25a56b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
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

0 comments on commit 25a56b0

Please sign in to comment.