Skip to content

Commit

Permalink
simplify client.WaitForRoundWithTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti committed Dec 18, 2024
1 parent 9cf40d0 commit b94f083
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions daemon/algod/api/client/restClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ func (client RestClient) WaitForBlockAfter(round basics.Round) (response model.N
return
}

// WaitForRound returns the node status after waiting for the given round.
// WaitForRound returns the node status after waiting for the given round. It
// waits no more than waitTime in TOTAL, and returns an error if the round has
// not been reached.
func (client RestClient) WaitForRound(round uint64, waitTime time.Duration) (status model.NodeStatusResponse, err error) {
timeout := time.NewTimer(waitTime)
timeout := time.After(waitTime)
for {
status, err = client.Status()
if err != nil {
Expand All @@ -307,7 +309,7 @@ func (client RestClient) WaitForRound(round uint64, waitTime time.Duration) (sta
return

Check warning on line 309 in daemon/algod/api/client/restClient.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/client/restClient.go#L308-L309

Added lines #L308 - L309 were not covered by tests
}
select {
case <-timeout.C:
case <-timeout:
return model.NodeStatusResponse{}, fmt.Errorf("timeout waiting for round %v with last round = %v", round, status.LastRound)
case <-time.After(200 * time.Millisecond):

Check warning on line 314 in daemon/algod/api/client/restClient.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/client/restClient.go#L311-L314

Added lines #L311 - L314 were not covered by tests
}
Expand All @@ -318,41 +320,19 @@ const singleRoundMaxTime = globals.MaxTimePerRound * 40

// WaitForRoundWithTimeout waits for a given round to be reached. As it
// waits, it returns early with an error if the wait time for any round exceeds
// globals.MaxTimePerRound so we can alert when we're getting "hung" waiting.
// singleRoundMaxTime so we can alert when we're getting "hung" waiting.
func (client RestClient) WaitForRoundWithTimeout(roundToWaitFor uint64) error {
status, err := client.Status()
if err != nil {
return err

Check warning on line 327 in daemon/algod/api/client/restClient.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/client/restClient.go#L324-L327

Added lines #L324 - L327 were not covered by tests
}
lastRound := status.LastRound

// If node is already at or past target round, we're done
if lastRound >= roundToWaitFor {
return nil
}

roundComplete := make(chan error, 2)

for nextRound := lastRound + 1; lastRound < roundToWaitFor; nextRound++ {
roundStarted := time.Now()

go func(done chan error) {
stat, err := client.WaitForRound(nextRound, singleRoundMaxTime)
lastRound = stat.LastRound
done <- err
}(roundComplete)

select {
case lastError := <-roundComplete:
if lastError != nil {
close(roundComplete)
return lastError
}
case <-time.After(singleRoundMaxTime):
// we've timed out.
time := time.Since(roundStarted)
return fmt.Errorf("fixture.WaitForRound took %3.2f seconds between round %d and %d", time.Seconds(), lastRound, nextRound)
for lastRound := status.LastRound; lastRound < roundToWaitFor; {
stat, err := client.WaitForRound(lastRound+1, singleRoundMaxTime)
if err != nil {
return fmt.Errorf("client.WaitForRound took too long between round %d and %d", lastRound, lastRound+1)

Check warning on line 333 in daemon/algod/api/client/restClient.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/client/restClient.go#L330-L333

Added lines #L330 - L333 were not covered by tests
}
lastRound = stat.LastRound

Check warning on line 335 in daemon/algod/api/client/restClient.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/client/restClient.go#L335

Added line #L335 was not covered by tests
}
return nil

Check warning on line 337 in daemon/algod/api/client/restClient.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/client/restClient.go#L337

Added line #L337 was not covered by tests
}
Expand Down

0 comments on commit b94f083

Please sign in to comment.