Skip to content

Fix: Prevent function from spamming API after sleep #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,25 @@
options: WatchOptions = defaultWatchOptions,
): AsyncGenerator<RandomnessBeacon> {
const info = await client.chain().info()
let currentRound = roundAt(Date.now(), info)


while (!abortController.signal.aborted) {
const now = Date.now()
await sleep(roundTime(info, currentRound) - now)

const beacon = await retryOnError(async () => client.get(currentRound), options.retriesOnFailure)
yield validatedBeacon(client, beacon, currentRound)
currentRound = currentRound + 1
const targetRound = roundAt(now, info)

const nextRoundTime = roundTime(info, targetRound)
const waitTime = nextRoundTime - now
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will waitTime ever be > 0 here given we're checking the round at now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the difference between nextRoundTime (the time of the next round, which will occur in the future) and the current time will be greater than 0.

Is there anything incorrect here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming 3 second rounds:
If we start at time 0 and it's currently time 1, roundAt(now, info) returns 1 (which started at time 0), therefore we will wait 0 seconds.
On the next iteration, it will still be time 1(ish), and roundAt(now, info) will still return 1, waiting 0 seconds again
I'd have assumed you need to increment the round to get the next round

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed it seems the tests are failing for the same reason


await sleep(Math.max(0, waitTime))

try {

Check failure on line 107 in lib/index.ts

View workflow job for this annotation

GitHub Actions / build_test_and_publish

Unnecessary try/catch wrapper
const beacon = await retryOnError(
async () => client.get(targetRound),
options.retriesOnFailure
)
yield validatedBeacon(client, beacon, targetRound)
} catch (error) {
throw error
}
}
}

Expand Down
Loading