feat(timeout): add utility for executing functions with a timeout#1
Merged
feat(timeout): add utility for executing functions with a timeout#1
Conversation
Introduce `DoWithTimeout` function in the `timeout` package to execute a function with a specified timeout. This utility ensures operations are bound by a time limit, preventing indefinite blocking. Includes comprehensive test coverage: - Success case - Timeout scenario - Context cancellation before and during execution - Function returning an error Also provides an example demonstrating usage in different scenarios.
25d508e to
e61c478
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the DoWithTimeout function by removing unreachable dead code in its select statement and cleaning up imports.
- Simplifies the channel receive to a single-value receive, eliminating the unused
okcheck - Removes the now-unnecessary
errorsimport fromtimeout.go - Verifies behavior remains correct via existing tests
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| timeout/timeout.go | Simplified select clause: removed two-value receive and dead code, deleted errors import |
| timeout/timeout_test.go | Adds comprehensive tests for success, timeout, cancellation, and function errors |
Comments suppressed due to low confidence (1)
timeout/timeout.go:29
- [nitpick] Closing the buffered channel isn't necessary when it's only used for a single send/receive. Consider removing
defer close(done)to simplify and avoid potential confusion.
defer close(done)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce
DoWithTimeoutfunction in thetimeoutpackage to execute afunction with a specified timeout. This utility ensures operations are bound by
a time limit, preventing indefinite blocking.
Includes comprehensive test coverage:
Also provides an example demonstrating usage in different scenarios.