Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Update removedChecker to use countInterval (#839)
Browse files Browse the repository at this point in the history
* Update removedChecker to use countInterval

* gofmt

* Requested changes

* Revert using errgroup

Co-authored-by: Mason Liang <mason@0x.org>
  • Loading branch information
z2trillion and Mason Liang authored Jun 24, 2020
1 parent d09210d commit a161d3a
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions zeroex/orderwatch/order_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ const (
// were not caught by the event watcher process.
minCleanupInterval = 1 * time.Hour

// minRemovedCheckInterval specifies the minimum amount of time between checks
// on whether to remove orders flaggged for removal from the DB
minRemovedCheckInterval = 5 * time.Minute
// maxDeleteInterval specifies the maximum amount of time between calls to permanentlyDeleteStaleRemovedOrders
maxDeleteInterval = 5 * time.Minute

// checkDatabaseUtilizationThresholdInterval specifies the amount of time between calls to db.CountOrders. If the count is higher than a threshold, then permanentlyDeleteStaleRemovedOrders will be called, so this is the minimum interval between calls to permanentlyDeleteStaleRemovedOrders.
checkDatabaseUtilizationThresholdInterval = 5 * time.Second

// If, after a call to db.CountOrders, the database utilization exceeds databaseUtilizationThreshold, then permanentlyDeleteStaleRemovedOrders will be called.
databaseUtilizationThreshold = 0.5

// defaultLastUpdatedBuffer specifies how long it must have been since an order was
// last updated in order to be re-validated by the cleanup worker
Expand Down Expand Up @@ -264,20 +269,28 @@ func (w *Watcher) cleanupLoop(ctx context.Context) error {
}

func (w *Watcher) removedCheckerLoop(ctx context.Context) error {
for {
start := time.Now()
if err := w.permanentlyDeleteStaleRemovedOrders(ctx); err != nil {
return err
}
if err := w.permanentlyDeleteStaleRemovedOrders(ctx); err != nil {
return err
}
lastDeleted := time.Now()

for {
select {
case <-ctx.Done():
return nil
// Wait minRemovedCheckInterval before calling permanentlyDeleteStaleRemovedOrders again. Since
// we only start waiting _after_ permanentlyDeleteStaleRemovedOrders completes, we will never
// have multiple calls to permanentlyDeleteStaleRemovedOrders running in parallel
case <-time.After(minRemovedCheckInterval - time.Since(start)):
continue
case <-time.After(checkDatabaseUtilizationThresholdInterval):
count, err := w.db.CountOrders(nil)
if err != nil {
return err
}
databaseUtilization := float64(count) / float64(w.maxOrders)

if time.Since(lastDeleted) > maxDeleteInterval || databaseUtilization > databaseUtilizationThreshold {
if err := w.permanentlyDeleteStaleRemovedOrders(ctx); err != nil {
return err
}
lastDeleted = time.Now()
}
}
}
}
Expand Down

0 comments on commit a161d3a

Please sign in to comment.