feat(java): pooled resource scope#376
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (4)
📝 WalkthroughWalkthroughThis 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. ChangesPooled Resource Scope
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)
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
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (10)
sdks/java/src/main/java/org/byteveda/taskito/DefaultTaskito.javasdks/java/src/main/java/org/byteveda/taskito/Taskito.javasdks/java/src/main/java/org/byteveda/taskito/resources/PoolConfig.javasdks/java/src/main/java/org/byteveda/taskito/resources/ResourceContext.javasdks/java/src/main/java/org/byteveda/taskito/resources/ResourceDefinition.javasdks/java/src/main/java/org/byteveda/taskito/resources/ResourcePool.javasdks/java/src/main/java/org/byteveda/taskito/resources/ResourceRuntime.javasdks/java/src/main/java/org/byteveda/taskito/resources/ResourceScope.javasdks/java/src/main/java/org/byteveda/taskito/resources/package-info.javasdks/java/src/test/java/org/byteveda/taskito/ResourceTest.java
Summary
ResourceScope.POOLED— a bounded checkout/return pool per resource, implementing the cross-SDK pooled-resource contract.PoolConfigrecord (poolSize,poolMin,acquireTimeoutdefault 10s,maxLifetimenull = unlimited) andResourcePool(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).Taskito.resource(name, PoolConfig, factory, dispose)overload.poolMin > 0pools; worker teardown shuts pools down before worker resources (pooled instances may depend on them).useworker-scoped dependencies — pooled instances outlive tasks.created/disposedcounters move only on actual build/disposal; checkout/return never touch them.Test plan
ResourceTest.java: sequential-task reuse, exhaustion timeout, factory-failure capacity safety, prewarm, maxLifetime eviction, task-scoped-dependency guard, disposal at worker shutdown../gradlew spotlessApply buildgreen — full Java suite 173 tests, 0 failures.Summary by CodeRabbit
POOLEDresource scope backed by a bounded reuse pool (capacity limits, acquire timeouts, pool prewarming, optional max lifetime retirement).PoolConfigto define pool sizing/timing, with fluent “with” builders and sensible defaults.