func NewProvider()

in pkg/providers/pricing/pricing.go [69:116]


func NewProvider(ctx context.Context, pricing client.PricingAPI, region string, startAsync <-chan struct{}) *Provider {
	// see if we've got region specific pricing data
	staticPricing, ok := initialOnDemandPrices[region]
	if !ok {
		// and if not, fall back to the always available eastus
		staticPricing = initialOnDemandPrices[defaultRegion]
	}

	p := &Provider{
		region:             region,
		onDemandUpdateTime: initialPriceUpdate,
		onDemandPrices:     staticPricing,
		spotUpdateTime:     initialPriceUpdate,
		// default our spot pricing to the same as the on-demand pricing until a price update
		spotPrices: staticPricing,
		pricing:    pricing,
		cm:         pretty.NewChangeMonitor(),
	}
	ctx = log.IntoContext(ctx, log.FromContext(ctx).WithName("pricing"))

	go func() {
		// perform an initial price update at startup
		p.updatePricing(ctx)

		startup := time.Now()
		// wait for leader election or to be signaled to exit
		select {
		case <-startAsync:
		case <-ctx.Done():
			return
		}
		// if it took many hours to be elected leader, we want to re-fetch pricing before we start our periodic
		// polling
		if time.Since(startup) > pricingUpdatePeriod {
			p.updatePricing(ctx)
		}

		for {
			select {
			case <-ctx.Done():
				return
			case <-time.After(pricingUpdatePeriod):
				p.updatePricing(ctx)
			}
		}
	}()
	return p
}