Skip to content

task: Add CPU and time bounds to ffmpeg calls - #11

Merged
hohonuuli merged 15 commits into
mainfrom
issue/10
Aug 13, 2026
Merged

hohonuuli merged 15 commits into
mainfrom
issue/10

Conversation

@hohonuuli

Copy link
Copy Markdown
Member

Change to improve performance related to #10 and mbari-org/vars-feedback#335

This PR adds CPU and time bounds to ffmpeg calls. I removed the use of Scala's ExecutionContext.global for running ffmpeg processes and add a BoundedExecutor which applies a concurrency limit, back pressure and a mechanism for killing stalled ffmpeg processes.

Concurrency and back pressure

  • BoundedExecutor (new) — Pool defaults to max(2, cores/4) threads. This runs ffmpeg commands. It also restricts the number of requests; providing immediate feedback (i.e. failures) if the processing queue is saturated.
  • /capture* Responds with a 503 when the queue is full.The API key is checked before a pool slot is claimed.
  • Queue-wait deadline (maxwait, 15s) — Submissions to the BoundedExecutor are timestamped and discarded it their wait exceeds BEHOLDER_CAPTURE_MAXWAIT.
  • Jittered Retry-After on both 503 paths now returns a variable jitter (2–8s). A constant value would cause every client to burst back simultaneously, refiling the BoundedExecutor again. BAD!
  • /health moved off the execution context, so it stays responsive under load.

Process handling

  • ProcessRunner (new) — runs ffmpeg/ffprobe with a hard timeout, killing overruns.
  • Configurable timeouts for ffmpeg (60s) and ffprobe (30s).

Correctness

  • Request coalescing (single-flight) in ImageCapture — concurrent requests for the same frame run ffmpeg once instead of repeating the same frame capture for each request

New configuration

Env var Default Description
BEHOLDER_CAPTURE_THREADS 0 (auto) Max concurrent ffmpeg processes
BEHOLDER_CAPTURE_QUEUESIZE 0 (auto) Requests that may wait before 503
BEHOLDER_CAPTURE_MAXWAIT 15s Queued request discarded after this; 0 disables
BEHOLDER_FFMPEG_TIMEOUT 60s A capture running longer is killed
BEHOLDER_FFPROBE_TIMEOUT 30s A probe running longer is killed

@hohonuuli hohonuuli self-assigned this Aug 12, 2026
@hohonuuli hohonuuli added the enhancement New feature or request label Aug 12, 2026
@hohonuuli

Copy link
Copy Markdown
Member Author

I've deployed this for testing as v0.3.1. Once testing evaluation is done I'll merge the pull request.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 bounds ffmpeg/ffprobe resource usage in the beholder frame-grab server to stop CPU/thread saturation (issue #10). It replaces the unbounded ExecutionContext.global used for captures with a fixed, bounded BoundedExecutor that sheds load, adds hard timeouts around subprocesses via a new ProcessRunner, coalesces concurrent requests for the same frame, and returns jittered 503 Retry-After responses when the pool is saturated.

Changes:

  • New concurrency primitives: BoundedExecutor (bounded pool + queue, maxWait stale-drop, submit/execute) and ProcessRunner (timed subprocess execution that force-kills overruns and cleans up partial output).
  • Request coalescing (single-flight) in ImageCapture keyed on the target frame path, plus configurable ffmpeg/ffprobe timeouts and -threads 2 on ffmpeg commands.
  • Load shedding in CaptureEndpoints with a new ServiceUnavailable error type (jittered Retry-After), health endpoint moved off the EC, and new AppConfig/reference.conf settings with BEHOLDER_* overrides.

Reviewed changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated no comments.

Show a summary per file
File Description
etc/jdk/BoundedExecutor.scala New bounded pool with backpressure, stale-work deadline, and StaleWorkException.
etc/jdk/ProcessRunner.scala New timed subprocess runner that kills overruns and captures output via temp files.
ImageCapture.scala Single-flight coalescing of concurrent captures; FrameGrabber injection seam.
api/CaptureEndpoints.scala Submits captures to the bounded pool; 503s on saturation/stale, key check before slot claim.
api/ErrorMsg.scala Adds ServiceUnavailable + jittered busy factory.
api/Endpoints.scala Wires Retry-After header + JSON body as a tapir oneOfVariant.
api/HealthEndpoints.scala Uses Future.successful so /health never schedules work.
etc/ffmpeg/FfmpegUtil.scala Timeout on ffmpeg, deletes partial output on failure, -threads 2.
etc/ffmpeg/FfprobeUtil.scala Object→class refactor with configurable timeout via ProcessRunner.
etc/ffmpeg/FfprobeService.scala Doc-only clarification (runs on calling thread).
AppConfig.scala / reference.conf New capture/ffmpeg/ffprobe tunables with env overrides.
Main.scala Builds the shared capture executor and logs the capture policy.
ImageType.scala, ImageCacheImpl.scala, CaptureRequest.scala, etc/xml/Converters.scala, etc/circe/CirceCodecs.scala Formatting/scaladoc reflow and codec registration for ServiceUnavailable.
Test suites (BoundedExecutorSuite, ProcessRunnerSuite, ServiceUnavailableSuite, HealthEndpointsSuite, AppConfigSuite, ImageCaptureSuite, CaptureEndpointsSuite, FfmpegUtilSuite, FfprobeUtilSuite, others) New coverage for coalescing, timeouts, load shedding, and reformatting of existing suites.

I did not find concrete, objective defects to flag: the coalescing leader/waiter/failure paths, the overflow-safe deadline check, the tapir error-variant wiring, the object→class FfprobeUtil refactor, and the config/env defaults are all consistent and backed by tests. That said, this is a substantial, subtle concurrency change (a hand-rolled bounded pool, forced process termination, single-flight coalescing where waiters block worker threads, and load-shedding semantics) whose correctness depends on runtime behavior that static review cannot fully validate, so it warrants human sign-off.


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@hohonuuli hohonuuli changed the title Issue/10 Add CPU and time bounds to ffmpeg calls Aug 13, 2026
@hohonuuli hohonuuli changed the title Add CPU and time bounds to ffmpeg calls task: Add CPU and time bounds to ffmpeg calls Aug 13, 2026
@hohonuuli
hohonuuli merged commit d01bb8e into main Aug 13, 2026
1 check passed
@hohonuuli
hohonuuli deleted the issue/10 branch August 13, 2026 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants