Skip to content

feat(timeout): add utility for executing functions with a timeout#1

Merged
jaeyeom merged 2 commits intomasterfrom
feature/timeout-pattern
Jun 1, 2025
Merged

feat(timeout): add utility for executing functions with a timeout#1
jaeyeom merged 2 commits intomasterfrom
feature/timeout-pattern

Conversation

@jaeyeom
Copy link
Copy Markdown
Owner

@jaeyeom jaeyeom commented Jun 1, 2025

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.

@jaeyeom jaeyeom requested a review from Copilot June 1, 2025 02:24
@jaeyeom jaeyeom self-assigned this Jun 1, 2025
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.
@jaeyeom jaeyeom force-pushed the feature/timeout-pattern branch from 25d508e to e61c478 Compare June 1, 2025 02:25
@jaeyeom jaeyeom changed the title refactor(timeout): remove unreachable code in select statement feat(timeout): add utility for executing functions with a timeout Jun 1, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 ok check
  • Removes the now-unnecessary errors import from timeout.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>
@jaeyeom jaeyeom merged commit 0514bfc into master Jun 1, 2025
1 check passed
@jaeyeom jaeyeom deleted the feature/timeout-pattern branch June 1, 2025 02:30
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.

2 participants