Skip to content

Commit 3bbe023

Browse files
authored
Add a default maintenance service timeout (#1019)
Here, replace a number of raw `30 * time.Second` timeouts with a shared default recommended timeout for maintenance services. Some services may still opt to use a different value, but in case they don't need one the default provides a good fall back.
1 parent 20287ed commit 3bbe023

5 files changed

Lines changed: 11 additions & 4 deletions

File tree

internal/maintenance/job_rescuer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (s *JobRescuer) runOnce(ctx context.Context) (*rescuerRunOnceResult, error)
274274
}
275275

276276
func (s *JobRescuer) getStuckJobs(ctx context.Context) ([]*rivertype.JobRow, error) {
277-
ctx, cancelFunc := context.WithTimeout(ctx, 30*time.Second)
277+
ctx, cancelFunc := context.WithTimeout(ctx, riversharedmaintenance.TimeoutDefault)
278278
defer cancelFunc()
279279

280280
stuckHorizon := time.Now().Add(-s.Config.RescueAfter)

internal/maintenance/job_scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (s *JobScheduler) runOnce(ctx context.Context) (*schedulerRunOnceResult, er
164164
for {
165165
// Wrapped in a function so that defers run as expected.
166166
numScheduled, err := func() (int, error) {
167-
ctx, cancelFunc := context.WithTimeout(ctx, 30*time.Second)
167+
ctx, cancelFunc := context.WithTimeout(ctx, riversharedmaintenance.TimeoutDefault)
168168
defer cancelFunc()
169169

170170
tx, err := s.exec.Begin(ctx)

internal/maintenance/queue_cleaner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (s *QueueCleaner) runOnce(ctx context.Context) (*queueCleanerRunOnceResult,
158158
for {
159159
// Wrapped in a function so that defers run as expected.
160160
queuesDeleted, err := func() ([]string, error) {
161-
ctx, cancelFunc := context.WithTimeout(ctx, 30*time.Second)
161+
ctx, cancelFunc := context.WithTimeout(ctx, riversharedmaintenance.TimeoutDefault)
162162
defer cancelFunc()
163163

164164
queuesDeleted, err := s.exec.QueueDeleteExpired(ctx, &riverdriver.QueueDeleteExpiredParams{

rivershared/circuitbreaker/circuit_breaker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func (b *CircuitBreaker) ResetIfNotOpen() bool {
8181

8282
// Trip "trips" the circuit breaker by counting an action towards opening it. If
8383
// the action causes the breaker to reach its limit within its window, the
84-
// breaker opens and the function returns true.
84+
// breaker opens and the function returns true. Subsequent calls to Trip after
85+
// the breaker is open will also return true.
8586
func (b *CircuitBreaker) Trip() bool {
8687
if b.open {
8788
return true

rivershared/riversharedmaintenance/river_shared_maintenance.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const (
2020
LogPrefixRanSuccessfully = ": Ran successfully"
2121
LogPrefixRunLoopStarted = ": Run loop started"
2222
LogPrefixRunLoopStopped = ": Run loop stopped"
23+
24+
// TimeoutDefault is a reasonable timeout for any large maintenance-related
25+
// queries. Some maintainers may opt to switch to their own timeout, but
26+
// this one should generally be used unless there's a good reason to have a
27+
// specific version.
28+
TimeoutDefault = 30 * time.Second
2329
)
2430

2531
// Constants related to JobCleaner.

0 commit comments

Comments
 (0)