Skip to content

Commit

Permalink
fix: resourceoffer updates (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
walkah authored Sep 24, 2024
1 parent a4ddd54 commit 6353895
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions pkg/resourceprovider/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type ResourceProviderController struct {
// loop is just for in case we miss any events
const CONTROL_LOOP_INTERVAL = 10 * time.Second

// the interval at which we check and post resource offers
const RESOURCE_OFFER_INTERVAL = 10 * time.Minute

// simple time tracking for the last time we posted resource offers
var lastResourceOfferPost time.Time

func NewResourceProviderController(
options ResourceProviderOptions,
web3SDK *web3.Web3SDK,
Expand Down Expand Up @@ -157,7 +163,11 @@ func (controller *ResourceProviderController) Start(ctx context.Context, cm *sys
ctx,
CONTROL_LOOP_INTERVAL,
func() error {
err := controller.solve(ctx)
err := controller.ensureResourceOffers()
if err != nil {
errorChan <- err
}
err = controller.solve(ctx)
if err != nil {
errorChan <- err
}
Expand Down Expand Up @@ -189,16 +199,9 @@ func (controller *ResourceProviderController) Start(ctx context.Context, cm *sys
func (controller *ResourceProviderController) solve(ctx context.Context) error {
controller.log.Debug("solving", "")

// if the solver does not know about resource offers
// that we have - we should post them to the solver
err := controller.ensureResourceOffers()
if err != nil {
return err
}

// if there are deals that have been matched and we have not agreed
// then we should agree to them
err = controller.agreeToDeals()
err := controller.agreeToDeals()
if err != nil {
return err
}
Expand Down Expand Up @@ -246,6 +249,11 @@ func (controller *ResourceProviderController) getResourceOffer(index int, spec d
}

func (controller *ResourceProviderController) ensureResourceOffers() error {
// We only want to run this every RESOURCE_OFFER_INTERVAL
if !lastResourceOfferPost.IsZero() && time.Since(lastResourceOfferPost) < RESOURCE_OFFER_INTERVAL {
return nil
}

// load the resource offers that are currently active and so should not be replaced
activeResourceOffers, err := controller.solverClient.GetResourceOffers(store.GetResourceOffersQuery{
ResourceProvider: controller.web3SDK.GetAddress().String(),
Expand Down Expand Up @@ -287,6 +295,7 @@ func (controller *ResourceProviderController) ensureResourceOffers() error {
}
}

lastResourceOfferPost = time.Now()
return err
}

Expand Down

0 comments on commit 6353895

Please sign in to comment.