Skip to content

Conversation

@matteius
Copy link

  • Add MaxSessions limit (100) to prevent HLS session accumulation
  • Add session cleanup goroutine with 30-second monitoring interval
  • Add MaxBufferSize (16MB) limit to HLS session buffers
  • Add MaxWriteBufferSize (8MB) limit to WriteBuffer
  • Add sync.Pool for UDP read buffers in RTSP conn to reduce allocations
  • Add debug logging for session creation/expiration

These fixes address memory growth issues observed during extended operation where go2rtc memory would grow from ~150MB to 7GB+ over 2.5 days with usage from lightNVR.

- Add MaxSessions limit (100) to prevent HLS session accumulation
- Add session cleanup goroutine with 30-second monitoring interval
- Add MaxBufferSize (16MB) limit to HLS session buffers
- Add MaxWriteBufferSize (8MB) limit to WriteBuffer
- Add sync.Pool for UDP read buffers in RTSP conn to reduce allocations
- Add debug logging for session creation/expiration

These fixes address memory growth issues observed during extended operation
where go2rtc memory would grow from ~150MB to 7GB+ over 2.5 days.
- Add createdAt timestamp to Session struct
- Add MaxSessionAge constant (24 hours) for session lifetime limit
- Update sessionCleanup to identify and remove sessions older than MaxSessionAge
- Stop alive timer when cleaning up stale sessions to prevent double cleanup
- Log removed stale sessions with their age for debugging
@AlexxIT
Copy link
Owner

AlexxIT commented Jan 18, 2026

  • Add sync.Pool for UDP read buffers in RTSP conn to reduce allocations

Using this doesn't make any sense at all. Because you're allocating new memory for every new packet anyway.
You can simply move the creation of the read buffer outside the loop.

PS. It's better to organize these things into different PR. Because they're completely different.

@AlexxIT
Copy link
Owner

AlexxIT commented Jan 18, 2026

  • Add MaxWriteBufferSize (8MB) limit to WriteBuffer

It's quite rare that WriteBuffer will write to *bytes.Buffer. I can't say right away where this happens.

And truncating the data is a bad solution. For many formats, it will completely break streaming. And sending the data further makes no sense.

@AlexxIT AlexxIT added the doubt label Jan 18, 2026
@matteius
Copy link
Author

Thanks for your feedback, I will try a different approach.

@matteius matteius closed this Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants