-
Notifications
You must be signed in to change notification settings - Fork 0
Phase 2 of API improvements #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moltude
wants to merge
16
commits into
main
Choose a base branch
from
fix/api-phase-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e2a4508
Phase 2 of API improvements
moltude 31c58ea
Update src/main/scala/dpla/api/v2/search/queryBuilders/QueryBuilder.s…
moltude 573389a
Update src/main/scala/dpla/api/v2/search/paramValidators/ParamValidat…
moltude 490ef38
Fix tests and misspellings
moltude ad58d6d
Fix parameter validation to reject over-limit values instead of clamp…
Copilot a4c778a
Fix broken test
moltude 3c4f02e
Fix facet guard bypass via empty filter values
DominicBM 99732bc
Address CodeRabbit: normalize empty facets/fields, fix error message …
DominicBM d7524b1
Fix test failure: use exists(_.nonEmpty) for facets guard, keep filte…
DominicBM 9bc8091
fix: address CodeRabbit review — ConcurrencyLimiter, fetch limit, fil…
DominicBM 5dc77df
fix: restore exact hit counts and clean up query builder
DominicBM 3a2583d
fix: update track_total_hits test to expect true instead of 10000
DominicBM e98d96a
fix: remove dead pagination depth guard; add OR operator multi_match …
DominicBM 214fce2
fix: remove leftover unused pagination variables
DominicBM 9407a71
test: rename outdated pagination test descriptions to reflect rejecti…
DominicBM 815dc55
fix: scope facet-only guard to ebook validator, preserving item endpo…
DominicBM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
src/main/scala/dpla/api/v2/search/ConcurrencyLimiter.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package dpla.api.v2.search | ||
|
|
||
| import java.util.concurrent.{Semaphore, TimeUnit} | ||
| import scala.concurrent.{ExecutionContext, Future} | ||
| import scala.util.control.NonFatal | ||
|
|
||
| /** Limits concurrent execution of Futures using a semaphore. | ||
| * | ||
| * IMPORTANT: The `apply` method uses `tryAcquire` with a timeout, which BLOCKS | ||
| * the calling thread for up to `timeoutSeconds`. In Akka actor contexts, this | ||
| * means actor threads may be blocked. This is intentional to provide | ||
| * backpressure when the system is overloaded, but callers should be aware of | ||
| * this behavior. | ||
| * | ||
| * For high-throughput scenarios, consider: | ||
| * - Using a dedicated blocking dispatcher for operations that use this | ||
| * limiter | ||
| * - Tuning `maxConcurrent` and `timeoutSeconds` based on your workload | ||
| * - Monitoring permit acquisition times | ||
| * | ||
| * @param maxConcurrent | ||
| * Maximum number of concurrent operations allowed | ||
| * @param timeoutSeconds | ||
| * Maximum time to wait for a permit before failing | ||
| */ | ||
| class ConcurrencyLimiter( | ||
| val maxConcurrent: Int, | ||
| val timeoutSeconds: Long | ||
| ) { | ||
| require( | ||
| maxConcurrent > 0, | ||
| s"maxConcurrent must be positive, got: $maxConcurrent" | ||
| ) | ||
| require( | ||
| timeoutSeconds > 0, | ||
| s"timeoutSeconds must be positive, got: $timeoutSeconds" | ||
| ) | ||
|
|
||
| private val semaphore = new Semaphore(maxConcurrent) | ||
|
|
||
| /** Wraps a Future with concurrency limiting. | ||
| * | ||
| * - Attempts to acquire a permit with timeout | ||
| * - If permit acquired, executes the Future and releases permit on | ||
| * completion | ||
| * - If timeout exceeded, returns a failed Future immediately | ||
| * - Ensures permit is released even if Future construction throws | ||
| * | ||
| * @param f | ||
| * The Future to execute (call-by-name, evaluated only if permit acquired) | ||
| * @param ec | ||
| * ExecutionContext for Future callbacks | ||
| * @return | ||
| * The wrapped Future, or a failed Future if permit couldn't be acquired | ||
| */ | ||
| def apply[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] = { | ||
| val acquired = try { | ||
| semaphore.tryAcquire(timeoutSeconds, TimeUnit.SECONDS) | ||
| } catch { | ||
| case e: InterruptedException => | ||
| Thread.currentThread().interrupt() | ||
| return Future.failed(e) | ||
| } | ||
|
|
||
| if (!acquired) { | ||
| Future.failed( | ||
| ConcurrencyLimitExceeded( | ||
| maxConcurrent = maxConcurrent, | ||
| timeoutSeconds = timeoutSeconds | ||
| ) | ||
| ) | ||
| } else { | ||
| try { | ||
| val future = f | ||
| future.andThen { case _ => semaphore.release() }(ec) | ||
| } catch { | ||
| case NonFatal(e) => | ||
| semaphore.release() | ||
| Future.failed(e) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| /** Returns the number of permits currently available. Useful for monitoring | ||
| * and debugging. | ||
| */ | ||
| def availablePermits: Int = semaphore.availablePermits() | ||
| } | ||
|
|
||
| /** Exception thrown when a concurrency limit is exceeded and the timeout | ||
| * expires. | ||
| */ | ||
| case class ConcurrencyLimitExceeded( | ||
| maxConcurrent: Int, | ||
| timeoutSeconds: Long | ||
| ) extends RuntimeException( | ||
| s"Concurrency limit ($maxConcurrent) exceeded, " + | ||
| s"timed out after ${timeoutSeconds}s waiting for permit" | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: dpla/api
Length of output: 12060
Wire ConcurrencyLimiter into the ES request path to eliminate duplication.
ConcurrencyLimiteris defined and tested, but production code still bypasses it.ElasticSearchClient.withConcurrencyLimit(lines 95–115) implements its own semaphore wrapper with identical logic. Either integrateConcurrencyLimiterinto the ES request path or remove the inline implementation to prevent maintenance divergence.🤖 Prompt for AI Agents