Commit cc05a5b
authored
perf(serverless): lazy-load boto3, fastapi, and pydantic to reduce cold start time (#466)
* perf(serverless): lazy-load boto3 to reduce cold start time
Move boto3 imports from module level to function level in rp_upload.py.
This defers loading ~50MB of boto3/botocore dependencies until S3 upload
functions are actually called, improving initial import time and memory
footprint for users who don't use S3 features.
Changes:
- Refactored get_boto_client() and bucket_upload() with lazy imports
- Added ImportError handling with helpful error messages
- Updated tests to mock boto3 modules directly
- Enhanced documentation to explain lazy-loading behavior
All upload functions maintain backward compatibility and graceful
fallback to local file storage when boto3 is unavailable.
* refactor(serverless): address PR review feedback for boto3 lazy-loading
Address Copilot review comments from PR #466:
1. Extract boto3 import logic into shared helper function
- Created _import_boto3_dependencies() to reduce duplication
- Used by both get_boto_client() and bucket_upload()
- Consistent error handling across all S3 upload functions
2. Add TransferConfig import to bucket_upload()
- Now imports all boto3 dependencies via shared helper
- Maintains consistency with get_boto_client()
3. Clarify documentation about fallback directories
- Documented that upload_image() uses simulated_uploaded/
- Documented that public API functions use local_upload/
- Added context about when fallback behavior occurs
All 358 tests pass with 97% coverage.
* refactor(serverless): replace print() with logger in upload fallback paths
Address Copilot review comment from PR #466:
Replace print() statements with logger.warning() for consistency with
the module's logging setup. This allows proper log level control and
maintains consistent logging behavior throughout the module.
Changes:
- upload_image(): Use logger.warning() instead of print()
- upload_file_to_bucket(): Use logger.warning() instead of print()
- upload_in_memory_object(): Use logger.warning() instead of print()
All fallback messages now use structured logging with single-line
format for better log parsing and filtering.
* refactor(serverless): extract local fallback logic into shared helper
Address Copilot review comment from PR #466:
Extract duplicated fallback logic from upload_file_to_bucket() and
upload_in_memory_object() into a shared _save_to_local_fallback() helper
function to reduce code duplication and ensure consistent behavior.
Changes:
- Created _save_to_local_fallback() helper function
- Handles both file-based (source_path) and in-memory (file_data) uploads
- Consolidated logging, directory creation, and file saving logic
- upload_file_to_bucket() now calls helper with source_path parameter
- upload_in_memory_object() now calls helper with file_data parameter
Test improvements:
- Added test_upload_file_to_bucket_fallback() for file-based fallback
- Added test_upload_in_memory_object_fallback() for in-memory fallback
- Added test_save_to_local_fallback_invalid_args() for error handling
- Added test_import_boto3_dependencies_missing() for ImportError path
- Achieved 100% test coverage for rp_upload.py module
Benefits:
- Reduced code duplication (removed 12 lines of duplicate code)
- Single source of truth for fallback behavior
- Easier to maintain and test
- Consistent error messages and logging
- Complete test coverage ensures reliability
All 10 upload tests pass with 100% module coverage.
* refactor(serverless): consolidate fallback logic across all upload functions
Address latest Copilot review comment from PR #466:
Eliminate remaining duplication by making upload_image() use the
_save_to_local_fallback() helper function. Added a 'directory' parameter
to the helper to support different fallback directories.
Changes:
- Added 'directory' parameter to _save_to_local_fallback() (default: 'local_upload')
- Updated upload_image() to use helper with directory='simulated_uploaded'
- Removed duplicate warning message and URL from upload_image()
- Consolidated all fallback logic into single helper function
Benefits:
- Complete elimination of code duplication
- Single source of truth for all fallback behavior
- Consistent warning messages across all upload functions
- Easier to maintain and update fallback logic
All 362 tests pass with 97% overall coverage, 100% coverage on rp_upload.py.
* refactor(serverless): restore type hints for boto3 lazy-loading
Use TYPE_CHECKING to import boto3 types only during static type checking,
maintaining proper type safety without runtime import cost.
Changes:
- Import BaseClient and TransferConfig under TYPE_CHECKING guard
- Restore get_boto_client() return type from Tuple[Any, Any] to
Tuple[Optional[BaseClient], Optional[TransferConfig]]
- Remove # pragma: no cover comment as it's no longer needed
This addresses PR review feedback about maintaining type safety while
preserving the lazy-loading optimization.
* perf(serverless): lazy-load FastAPI to reduce cold start time
Move FastAPI/Uvicorn/Pydantic imports from module-level to conditional
blocks where they're actually used. This stack is only needed when
--rp_serve_api flag is set (local dev) or realtime mode is enabled.
Performance Impact:
- Cold start: 480ms → 280-326ms (32-42% faster)
- Modules loaded: 841 → 640 (24% reduction, ~200 fewer)
- Production workers: Never load FastAPI/Uvicorn/Pydantic stack
- Dev mode: FastAPI loads on-demand when needed
Changes:
- Remove eager import of rp_fastapi from module level
- Add lazy import in start() when rp_serve_api flag is True
- Add lazy import in start() when realtime mode is enabled
All tests pass. No breaking changes.
* test(serverless): fix tests for lazy-loaded FastAPI
Update test mocks to use correct import path for lazy-loaded rp_fastapi module.
Since FastAPI is now imported on-demand inside start() function rather than
at module level, tests need to mock the actual module path.
Changes:
- Update test_local_api to mock runpod.serverless.modules.rp_fastapi.WorkerAPI
- Update test_start_does_not_set_excepthook to mock correct module path
All 362 tests pass with 96.76% coverage.1 parent 227ef38 commit cc05a5b
File tree
6 files changed
+179
-40
lines changed- docs/serverless/utils
- runpod/serverless
- modules
- utils
- tests/test_serverless
- test_utils
6 files changed
+179
-40
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
7 | 15 | | |
8 | 16 | | |
9 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
| |||
155 | 154 | | |
156 | 155 | | |
157 | 156 | | |
| 157 | + | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
| 169 | + | |
169 | 170 | | |
170 | 171 | | |
171 | 172 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
| 198 | + | |
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | 16 | | |
21 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
27 | 82 | | |
28 | 83 | | |
29 | 84 | | |
| |||
43 | 98 | | |
44 | 99 | | |
45 | 100 | | |
46 | | - | |
47 | | - | |
48 | | - | |
| 101 | + | |
49 | 102 | | |
50 | 103 | | |
| 104 | + | |
51 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
52 | 115 | | |
53 | 116 | | |
54 | 117 | | |
| |||
111 | 174 | | |
112 | 175 | | |
113 | 176 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
126 | 184 | | |
127 | 185 | | |
128 | 186 | | |
| |||
180 | 238 | | |
181 | 239 | | |
182 | 240 | | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
183 | 250 | | |
184 | 251 | | |
185 | 252 | | |
| |||
231 | 298 | | |
232 | 299 | | |
233 | 300 | | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
| 301 | + | |
245 | 302 | | |
246 | 303 | | |
247 | 304 | | |
| |||
285 | 342 | | |
286 | 343 | | |
287 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
288 | 348 | | |
289 | 349 | | |
290 | 350 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
35 | 44 | | |
36 | 45 | | |
37 | 46 | | |
38 | 47 | | |
39 | 48 | | |
40 | 49 | | |
41 | 50 | | |
42 | | - | |
| 51 | + | |
43 | 52 | | |
44 | | - | |
| 53 | + | |
45 | 54 | | |
46 | 55 | | |
47 | 56 | | |
| |||
110 | 119 | | |
111 | 120 | | |
112 | 121 | | |
| 122 | + | |
113 | 123 | | |
114 | | - | |
| 124 | + | |
115 | 125 | | |
116 | 126 | | |
117 | 127 | | |
| |||
178 | 188 | | |
179 | 189 | | |
180 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
181 | 204 | | |
182 | 205 | | |
183 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
184 | 231 | | |
185 | 232 | | |
186 | 233 | | |
| |||
220 | 267 | | |
221 | 268 | | |
222 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
223 | 293 | | |
224 | 294 | | |
225 | 295 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
88 | | - | |
| 87 | + | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
544 | 544 | | |
545 | 545 | | |
546 | 546 | | |
547 | | - | |
| 547 | + | |
548 | 548 | | |
549 | 549 | | |
550 | 550 | | |
| |||
0 commit comments