Skip to content

Timeout for bot.tabComplete()#3293

Merged
rom1504 merged 8 commits into
PrismarineJS:masterfrom
Ynfuien:timeout-for-tabComplete
Jan 27, 2024
Merged

Timeout for bot.tabComplete()#3293
rom1504 merged 8 commits into
PrismarineJS:masterfrom
Ynfuien:timeout-for-tabComplete

Conversation

@Ynfuien
Copy link
Copy Markdown
Contributor

@Ynfuien Ynfuien commented Jan 27, 2024

I added a timeout, because without it, a promise was never* resolved, if provided command/string had no completions.

*Never, meaning till next successful tab completion, when it would just double the newer result.

I set the default timeout to, what I think is enough low/high, 100 ms. But it can be changed with a function argument, so everyone will set whatever they want.

Comment thread lib/plugins/chat.js Outdated
const [packet] = await once(bot._client, 'tab_complete')
// Doing promise race, because of 'tab_complete' event never happening,
// in situation when there was no completions for tabbed command.
const eventPromise = once(bot._client, 'tab_complete')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We already have an onceWithCleanup function that handles timeouts. You should use that instead here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think now is good?

Comment thread lib/plugins/chat.js Outdated
@@ -1,4 +1,4 @@
const { once } = require('events')
const { onceWithCleanup } = require('mineflayer/lib/promise_utils')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

require('../promise_utils')

Comment thread lib/plugins/chat.js Outdated
}

async function tabComplete (text, assumeCommand = false, sendBlockInSight = true) {
async function tabComplete (text, assumeCommand = false, sendBlockInSight = true, timeout = 100) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

100ms seems rather low of a timeout for high latency connections

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Then someone can change it for their use case. But if you think that e.g. 200 would be better, then I can change it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How about now? It's based on bot's ping

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, that's worse. Ideally we stick to something at least 1 second, high latency connections are perfectly valid, they should not be an error case. Also, we shouldn't have every function having inconsistent timeout values, that makes things very messy.

I just checked the mineflayer code for existing timeouts, for blocks.js and command blocks and uses 5000ms timeout, so I'd say use that here also.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't see why dynamic timeout would be worse, but alright, I will change it to 5s

Copy link
Copy Markdown
Member

@extremeheat extremeheat left a comment

Choose a reason for hiding this comment

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

LGTM, but I'm not sure returning an empty array is any better than letting an exception propagate to the user

Comment thread lib/plugins/chat.js Outdated
const [packet] = await onceWithCleanup(bot._client, 'tab_complete', { timeout })
return packet.matches
} catch {
return []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes I think let's not try catch here. The user should handle the error appropriately (for example maybe by retrying)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@rom1504 rom1504 merged commit 4231a16 into PrismarineJS:master Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants