Skip to content

feat(java): pooled resource scope#376

Merged
pratyush618 merged 5 commits into
masterfrom
feat/java-resource-pool
Jul 6, 2026
Merged

feat(java): pooled resource scope#376
pratyush618 merged 5 commits into
masterfrom
feat/java-resource-pool

Conversation

@pratyush618

@pratyush618 pratyush618 commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds ResourceScope.POOLED — a bounded checkout/return pool per resource, implementing the cross-SDK pooled-resource contract.
  • New PoolConfig record (poolSize, poolMin, acquireTimeout default 10s, maxLifetime null = unlimited) and ResourcePool (semaphore-bounded capacity, idle deque with lifetime eviction, factory on the acquiring thread, permit returned on factory failure so capacity never leaks, best-effort prewarm, quiet shutdown, stats snapshot).
  • Registration: new Taskito.resource(name, PoolConfig, factory, dispose) overload.
  • Runtime: one checkout per task per resource, cached in the task scope and released (not disposed) at task end; pools are per-worker (capacity never shared across workers); first worker lease prewarms poolMin > 0 pools; worker teardown shuts pools down before worker resources (pooled instances may depend on them).
  • Guard: a pooled factory may only use worker-scoped dependencies — pooled instances outlive tasks.
  • created/disposed counters move only on actual build/disposal; checkout/return never touch them.

Test plan

  • 7 new tests in ResourceTest.java: sequential-task reuse, exhaustion timeout, factory-failure capacity safety, prewarm, maxLifetime eviction, task-scoped-dependency guard, disposal at worker shutdown.
  • ./gradlew spotlessApply build green — full Java suite 173 tests, 0 failures.

Summary by CodeRabbit

  • New Features
    • Added POOLED resource scope backed by a bounded reuse pool (capacity limits, acquire timeouts, pool prewarming, optional max lifetime retirement).
    • Introduced PoolConfig to define pool sizing/timing, with fluent “with” builders and sensible defaults.
    • Added pool statistics and lifecycle-aware pooling/checkout behavior across task duration.
  • Bug Fixes
    • Strengthened validation to prevent mismatched pooled vs non-pooled registrations.
    • Improved pooled cleanup so permits/instances are correctly returned or retired on task/worker shutdown.
  • Documentation / Tests
    • Updated resource lifecycle documentation and added pooled-resource coverage.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6a8c634e-6132-499f-9521-438f74285961

📥 Commits

Reviewing files that changed from the base of the PR and between 1ee820c and 31eff36.

📒 Files selected for processing (4)
  • sdks/java/src/main/java/org/byteveda/taskito/resources/PoolConfig.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourcePool.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceRuntime.java
  • sdks/java/src/test/java/org/byteveda/taskito/ResourceTest.java
🚧 Files skipped from review as they are similar to previous changes (4)
  • sdks/java/src/main/java/org/byteveda/taskito/resources/PoolConfig.java
  • sdks/java/src/test/java/org/byteveda/taskito/ResourceTest.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceRuntime.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourcePool.java

📝 Walkthrough

Walkthrough

This PR adds a pooled resource scope to Taskito. It introduces PoolConfig and ResourcePool, extends ResourceScope and ResourceDefinition, updates ResourceRuntime for pooled checkout and teardown, adds a new public registration overload, and expands tests and Javadocs.

Changes

Pooled Resource Scope

Layer / File(s) Summary
Pool configuration and pooling contracts
sdks/java/src/main/java/org/byteveda/taskito/resources/PoolConfig.java, sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceScope.java, sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceDefinition.java, sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceContext.java, sdks/java/src/main/java/org/byteveda/taskito/resources/package-info.java
Adds PoolConfig, adds POOLED scope, extends ResourceDefinition with pool metadata and validation, and updates related Javadocs.
ResourcePool implementation
sdks/java/src/main/java/org/byteveda/taskito/resources/ResourcePool.java
Implements a bounded pool with idle reuse, expiration, prewarm, release, shutdown, stats, and timeout handling.
ResourceRuntime pooled scope wiring
sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceRuntime.java
Adds per-worker pool storage, pooled dependency restrictions, pooled task resolution, worker prewarming, and pool shutdown ordering.
Public pooled-resource API
sdks/java/src/main/java/org/byteveda/taskito/Taskito.java, sdks/java/src/main/java/org/byteveda/taskito/DefaultTaskito.java
Adds the pooled resource(...) overload and registers pooled resources through ResourceScope.POOLED.
Pooled resource tests
sdks/java/src/test/java/org/byteveda/taskito/ResourceTest.java
Adds tests for reuse, validation, timeout, factory failures, prewarming, expiration, dependency restrictions, and shutdown disposal.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Task
  participant ResourceRuntime
  participant ResourcePool
  participant Factory

  Task->>ResourceRuntime: resolveForTask(POOLED)
  ResourceRuntime->>ResourcePool: acquire()
  ResourcePool->>ResourcePool: awaitCapacity()
  alt idle instance available and not expired
    ResourcePool-->>ResourceRuntime: reuse idle instance
  else no idle instance
    ResourcePool->>Factory: create new instance
    Factory-->>ResourcePool: instance
    ResourcePool-->>ResourceRuntime: new instance
  end
  ResourceRuntime-->>Task: pooled instance
  Task->>ResourceRuntime: task teardown
  ResourceRuntime->>ResourcePool: release(instance)
Loading
sequenceDiagram
  participant Worker
  participant ResourceRuntime
  participant ResourcePool

  Worker->>ResourceRuntime: acquireWorker (first lease)
  ResourceRuntime->>ResourcePool: prewarm()
  ResourcePool-->>ResourceRuntime: instances created up to poolMin

  Worker->>ResourceRuntime: shutdown
  ResourceRuntime->>ResourcePool: shutdown()
  ResourcePool-->>ResourceRuntime: idle instances disposed
  ResourceRuntime->>ResourceRuntime: run worker teardown stack
Loading

Possibly related PRs

  • ByteVeda/taskito#340: Modifies the same Taskito/DefaultTaskito resource-registration API and ResourceRuntime resolution and teardown logic extended by this PR.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately highlights the main change: adding a pooled resource scope in Java.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/java-resource-pool

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@sdks/java/src/main/java/org/byteveda/taskito/resources/PoolConfig.java`:
- Around line 19-35: `PoolConfig` is missing a bounds check that allows
`poolMin` to exceed `poolSize`, which can break the bounded pool invariant.
Update the `PoolConfig` compact constructor to validate that `poolMin` is not
greater than `poolSize`, alongside the existing checks for `poolSize`,
`acquireTimeout`, and `maxLifetime`, so `ResourcePool.prewarm()` cannot create
more idle instances than the semaphore-backed capacity allows.

In `@sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceRuntime.java`:
- Around line 139-145: The `ResourceRuntime.acquireWorker()` method currently
holds the instance monitor while `prewarmPools()` runs, which blocks other
`acquireWorker()` and `teardownWorker()` calls for the full prewarm duration.
Narrow the synchronized section in `ResourceRuntime.acquireWorker` so only the
`leases` check/update is guarded, and move `prewarmPools()` outside the lock
(using a safe one-time prewarm guard if needed) so slow factory work does not
serialize concurrent worker leases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dc31efb9-1b3a-40d2-a718-42aa794b0bbb

📥 Commits

Reviewing files that changed from the base of the PR and between c1de752 and 1ee820c.

📒 Files selected for processing (10)
  • sdks/java/src/main/java/org/byteveda/taskito/DefaultTaskito.java
  • sdks/java/src/main/java/org/byteveda/taskito/Taskito.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/PoolConfig.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceContext.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceDefinition.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourcePool.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceRuntime.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceScope.java
  • sdks/java/src/main/java/org/byteveda/taskito/resources/package-info.java
  • sdks/java/src/test/java/org/byteveda/taskito/ResourceTest.java

@pratyush618
pratyush618 merged commit 43b4447 into master Jul 6, 2026
18 checks passed
@pratyush618
pratyush618 deleted the feat/java-resource-pool branch July 6, 2026 16:21
@coderabbitai coderabbitai Bot mentioned this pull request Jul 7, 2026
2 tasks
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.

1 participant