feat(jpeg): Add keyframe caching with expiration mechanism to JPEG http handler #1155
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.
Summary of Content:
This PR introduces an enhanced mechanism for caching keyframes within the (M)JPEG streaming service. The major changes include:
Cache Structure Implementation:
CacheEntry
structure that includes a byte slice for the frame and a timestamp for expiration was created.keyframeCache
with a read-write mutex for thread-safe operations was established, storing the cache entries in a map.cacheDuration
) has been defined, which is currently set to one minute but marked with a TODO to make it configurable in the future.Cache Utilization in Keyframe Handler:
handlerKeyframe
function, the commit implements a check to serve frames directly from the cache if they are found and haven't expired based oncacheDuration
.Periodic Cache Cleanup:
cleanupCache
goroutine function has been added to periodically clean up the expired keyframes from the cache. The cleanup runs in intervals equal tocacheDuration
, ensuring that stale entries do not persist in memory.Helper Function:
writeJPEGResponse
function to consolidate the response writing part with a correct setup for content-type and content-length in the HTTP header, promoting code reuse and clarity.These modifications improve the efficiency of the JPEG streaming by reducing the need for frequent keyframe reconstructions, thus potentially lowering the processing overhead and speeding up the frame delivery. The implemented caching strategy enables the application to handle high-concurrency scenarios more effectively by reducing duplicate processing and serving cached results wherever applicable.