Skip to content

fix: prevent connection pool exhaustion from concurrent is-active checks#3278

Closed
grutt wants to merge 1 commit intomainfrom
fix--active-queue
Closed

fix: prevent connection pool exhaustion from concurrent is-active checks#3278
grutt wants to merge 1 commit intomainfrom
fix--active-queue

Conversation

@grutt
Copy link
Contributor

@grutt grutt commented Mar 14, 2026

Description

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation change (pure documentation change)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking changes to code which doesn't change any behaviour)
  • CI (any automation pipeline changes)
  • Chore (changes which are not directly related to any business logic)
  • Test changes (add, refactor, improve or change a test)
  • This change requires a documentation update

What's Changed

  • Add a list of tasks or features here...

Copilot AI review requested due to automatic review settings March 14, 2026 12:56
@vercel
Copy link

vercel bot commented Mar 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hatchet-docs Ready Ready Preview, Comment Mar 14, 2026 1:00pm

Request Review

@github-actions
Copy link
Contributor

Benchmark results

goos: linux
goarch: amd64
pkg: github.com/hatchet-dev/hatchet/pkg/scheduling/v1
cpu: AMD Ryzen 9 7950X3D 16-Core Processor          
              │ /tmp/old.txt │         /tmp/new.txt          │
              │    sec/op    │    sec/op     vs base         │
RateLimiter-8    50.44µ ± 4%   51.69µ ± 14%  ~ (p=0.485 n=6)

              │ /tmp/old.txt │         /tmp/new.txt          │
              │     B/op     │     B/op      vs base         │
RateLimiter-8   137.7Ki ± 0%   137.7Ki ± 0%  ~ (p=0.965 n=6)

              │ /tmp/old.txt │          /tmp/new.txt          │
              │  allocs/op   │  allocs/op   vs base           │
RateLimiter-8    1.022k ± 0%   1.022k ± 0%  ~ (p=1.000 n=6) ¹
¹ all samples are equal

Compared against main (a07cc9a)

Copy link
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 mitigates database connection pool exhaustion caused by many ConcurrencyManager instances concurrently performing “is-active” checks in the scheduler subsystem.

Changes:

  • Added a shared semaphore in the scheduling pool to cap concurrent is_active checks across all tenants/strategies.
  • Switched loopCheckActive to a randomized ticker interval and ensured the ticker is stopped.
  • Updated UpdateConcurrencyStrategyIsActive to use pg_try_advisory_xact_lock (non-blocking) to reduce lock-wait connection usage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/scheduling/v1/pool.go Introduces and initializes a shared semaphore to throttle concurrent is-active checks.
pkg/scheduling/v1/concurrency.go Uses the shared semaphore to limit concurrent is-active checks and randomizes the check interval.
pkg/repository/scheduler_concurrency.go Replaces blocking advisory lock acquisition with try-lock to avoid holding DB connections while waiting.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 72 to 76
schedulerConcurrencyRateLimit: schedulerConcurrencyRateLimit,
schedulerConcurrencyPollingMinInterval: schedulerConcurrencyPollingMinInterval,
schedulerConcurrencyPollingMaxInterval: schedulerConcurrencyPollingMaxInterval,
isActiveSemaphore: make(chan struct{}, 10),
},
Comment on lines +172 to +177
// Limit the number of concurrent is-active checks to avoid saturating the connection pool.
select {
case <-ctx.Done():
return
case c.isActiveSemaphore <- struct{}{}:
}
@grutt grutt closed this Mar 14, 2026
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