Commit 12c4bfd
feat(rtc): Add event_types filtering to FfiQueue.subscribe()
PROBLEM:
FfiQueue.put() broadcasts ALL FFI events to ALL subscribers via
call_soon_threadsafe(). Each call creates asyncio.Handle + context objects.
AudioStream/VideoStream filter events with wait_for(predicate), but objects
are already allocated. With N streams, this creates N × all_events objects,
with 95%+ discarded after allocation.
In a 2-hour meeting with 4 participants, we observed:
- 903,154 FFI events accumulated
- Memory grew from 312 MB to 1.29 GB
- Event loop lag increased to 20+ seconds
SOLUTION:
Add optional `event_types` parameter to FfiQueue.subscribe(). When specified,
events are filtered by type BEFORE calling call_soon_threadsafe(), preventing
unnecessary object allocation.
AudioStream now subscribes with event_types={"audio_stream_event"}
VideoStream now subscribes with event_types={"video_stream_event"}
This reduces memory allocations by ~95% for stream subscribers while
maintaining full backwards compatibility (event_types=None = all events).
TESTING:
- Added unit tests for event filtering functionality
- Verified 95% reduction in object creation with filtered subscribers
- Tested in production environment with stable memory usage
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>1 parent 20d73ec commit 12c4bfd
File tree
4 files changed
+375
-8
lines changed- livekit-rtc/livekit/rtc
- tests/rtc
4 files changed
+375
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
118 | 121 | | |
119 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
120 | 130 | | |
121 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
122 | 137 | | |
123 | 138 | | |
124 | 139 | | |
125 | 140 | | |
126 | 141 | | |
127 | 142 | | |
128 | 143 | | |
129 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
130 | 159 | | |
131 | 160 | | |
132 | 161 | | |
133 | | - | |
| 162 | + | |
134 | 163 | | |
135 | 164 | | |
136 | 165 | | |
137 | 166 | | |
138 | 167 | | |
139 | | - | |
| 168 | + | |
140 | 169 | | |
141 | 170 | | |
142 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
106 | 110 | | |
107 | 111 | | |
108 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
51 | 55 | | |
52 | 56 | | |
53 | 57 | | |
| |||
0 commit comments