Commit 286315a
Refactor async client connection-handling (#280)
Refactor interfaces related to the Redis client in AsyncRedisIndex (and
possibly others).
## Constraints
- We want to initialize the Redis connection lazily, not at object
instantiation or module import.
- Python doesn't have async properties, so properties are limited to
sync function calls
- Therefore, we don't want properties to be part of the interface
- For functions we "remove," we'll leave them in place with a
deprecation warning
- Remaining backwards-compatible until the next major release
## The Design Today
- Can create a client and set it using `index.connect()`
- Pass in a `redis_url`
- Calls `index.set_client()`
- Can pass in a client and set it with `index.set_client()`
- Pass in a client instance
- **Can't** pass in a client instance with `__init__()`
- Or anything URL, etc.
- Can create a client with `index.from_existing()`
- Pass it a `redis_url`
- Calls `index.set_client()`
- **Can't** use index as an async context manager
- Requires explicit resource handling, `atexit` handler
- But this is broken
- The `setup_async_redis()` decorator creates a function wrapper that
calls `validate_async_redis()` with every `set_client()` call
- The `RedisConnectionFactory.connect()` returns incompatible types
(sync, async `Redis`)
- Sync vs. async depends on a parameter
## The Design After Refactor
- **Can** use as an async context manager
- Either disconnect the index manually with `disconnect()` or use it as
a context manager
- `async with Index()...` will `disconnect()` after you exit the context
block
- Lazily instantiate client with `self._get_client()` method ("private")
- Remove `set_client()` public interface if possible
- Lazily instantiate client
- Remove `connect()` public interface if possible
- Lazily instantiate client
- Leave `from_existing()`
- Leave `client()` property, now just returns `._redis_client` and can
be None
- Call `validate_async_redis()` when setting a new client instance for
the first time
- But make this an internal check rather than attached to public
interface
- Allow `redis_url`, `redis_kwargs`, or `redis_client` in `__init__()`
### Examples Post-Refactor
```python
#1: New instance with redis URL
index = AsyncRedisIndex(redis_url="...")
#2: New instance with existing client
index = AsyncRedisIndex(redis_client=client)
#3: New instance with `from_existing`
index = AsyncRedisIndex.from_existing(..., redis_url="...")
#4 Passing both a client and a URL is isallowed
try:
index = AsyncRedisIndex(redis_client=client, redis_url="...")
except:
pass
else:
raise RuntimeError("Should have raised!")
# The client is lazily connected here, and we send telemetry.
await index.something_using_redis()
await index.something_else_using_redis()
# Close the client.
await index.close()
async with AsyncRedisIndex(redis_url="...") as index:
# Same story: client is lazily created now
await index.something_using_redis()
await index.something_else_using_redis()
# Client is automatically closed
# __enter__ is reentrant
async with index:
# Lazily opens a new client connection.
await index.a_third_thing_using_redis()
# Client is automatically closed
```
---------
Co-authored-by: Tyler Hutcherson <[email protected]>1 parent 1172c49 commit 286315a
File tree
23 files changed
+1180
-680
lines changed- docs/user_guide
- redisvl
- cli
- extensions
- llmcache
- router
- session_manager
- index
- redis
- utils
- tests
- integration
- unit
23 files changed
+1180
-680
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
| 124 | + | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
130 | | - | |
131 | | - | |
| 129 | + | |
| 130 | + | |
132 | 131 | | |
133 | 132 | | |
134 | 133 | | |
135 | 134 | | |
136 | | - | |
| 135 | + | |
137 | 136 | | |
138 | 137 | | |
139 | 138 | | |
| |||
346 | 345 | | |
347 | 346 | | |
348 | 347 | | |
349 | | - | |
| 348 | + | |
350 | 349 | | |
351 | 350 | | |
352 | 351 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
181 | | - | |
| 181 | + | |
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
| 198 | + | |
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
212 | | - | |
| 212 | + | |
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
| 230 | + | |
230 | 231 | | |
231 | | - | |
232 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
233 | 237 | | |
234 | 238 | | |
235 | 239 | | |
| |||
243 | 247 | | |
244 | 248 | | |
245 | 249 | | |
246 | | - | |
| 250 | + | |
247 | 251 | | |
248 | 252 | | |
249 | 253 | | |
| |||
258 | 262 | | |
259 | 263 | | |
260 | 264 | | |
261 | | - | |
262 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
263 | 269 | | |
264 | 270 | | |
265 | 271 | | |
| |||
273 | 279 | | |
274 | 280 | | |
275 | 281 | | |
276 | | - | |
| 282 | + | |
277 | 283 | | |
278 | 284 | | |
279 | 285 | | |
| |||
297 | 303 | | |
298 | 304 | | |
299 | 305 | | |
300 | | - | |
| 306 | + | |
301 | 307 | | |
302 | 308 | | |
303 | 309 | | |
| |||
315 | 321 | | |
316 | 322 | | |
317 | 323 | | |
318 | | - | |
| 324 | + | |
319 | 325 | | |
320 | 326 | | |
321 | 327 | | |
| |||
358 | 364 | | |
359 | 365 | | |
360 | 366 | | |
361 | | - | |
| 367 | + | |
362 | 368 | | |
363 | 369 | | |
364 | 370 | | |
| |||
392 | 398 | | |
393 | 399 | | |
394 | 400 | | |
395 | | - | |
| 401 | + | |
396 | 402 | | |
397 | 403 | | |
398 | 404 | | |
| |||
429 | 435 | | |
430 | 436 | | |
431 | 437 | | |
432 | | - | |
| 438 | + | |
433 | 439 | | |
434 | 440 | | |
435 | 441 | | |
| |||
454 | 460 | | |
455 | 461 | | |
456 | 462 | | |
457 | | - | |
| 463 | + | |
458 | 464 | | |
459 | 465 | | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
460 | 473 | | |
461 | 474 | | |
462 | 475 | | |
463 | | - | |
| 476 | + | |
464 | 477 | | |
465 | 478 | | |
466 | 479 | | |
| |||
487 | 500 | | |
488 | 501 | | |
489 | 502 | | |
490 | | - | |
| 503 | + | |
491 | 504 | | |
492 | 505 | | |
493 | 506 | | |
| |||
537 | 550 | | |
538 | 551 | | |
539 | 552 | | |
540 | | - | |
541 | | - | |
| 553 | + | |
542 | 554 | | |
543 | 555 | | |
544 | 556 | | |
545 | 557 | | |
546 | | - | |
| 558 | + | |
547 | 559 | | |
548 | 560 | | |
549 | 561 | | |
| |||
584 | 596 | | |
585 | 597 | | |
586 | 598 | | |
587 | | - | |
| 599 | + | |
588 | 600 | | |
589 | 601 | | |
590 | 602 | | |
| |||
609 | 621 | | |
610 | 622 | | |
611 | 623 | | |
612 | | - | |
| 624 | + | |
613 | 625 | | |
614 | 626 | | |
615 | 627 | | |
616 | 628 | | |
617 | 629 | | |
618 | 630 | | |
619 | | - | |
| 631 | + | |
620 | 632 | | |
621 | 633 | | |
622 | 634 | | |
| |||
627 | 639 | | |
628 | 640 | | |
629 | 641 | | |
630 | | - | |
| 642 | + | |
631 | 643 | | |
632 | 644 | | |
633 | 645 | | |
634 | 646 | | |
635 | 647 | | |
636 | | - | |
| 648 | + | |
637 | 649 | | |
638 | 650 | | |
639 | 651 | | |
| |||
659 | 671 | | |
660 | 672 | | |
661 | 673 | | |
662 | | - | |
| 674 | + | |
663 | 675 | | |
664 | 676 | | |
665 | 677 | | |
| |||
677 | 689 | | |
678 | 690 | | |
679 | 691 | | |
680 | | - | |
681 | | - | |
| 692 | + | |
| 693 | + | |
682 | 694 | | |
683 | | - | |
| 695 | + | |
684 | 696 | | |
685 | | - | |
686 | | - | |
687 | | - | |
688 | | - | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
689 | 701 | | |
690 | | - | |
691 | | - | |
692 | | - | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
693 | 705 | | |
694 | 706 | | |
695 | 707 | | |
| |||
718 | 730 | | |
719 | 731 | | |
720 | 732 | | |
721 | | - | |
| 733 | + | |
722 | 734 | | |
723 | 735 | | |
724 | 736 | | |
| |||
727 | 739 | | |
728 | 740 | | |
729 | 741 | | |
730 | | - | |
| 742 | + | |
731 | 743 | | |
732 | 744 | | |
733 | 745 | | |
| |||
739 | 751 | | |
740 | 752 | | |
741 | 753 | | |
742 | | - | |
| 754 | + | |
743 | 755 | | |
744 | 756 | | |
745 | 757 | | |
| |||
748 | 760 | | |
749 | 761 | | |
750 | 762 | | |
751 | | - | |
| 763 | + | |
752 | 764 | | |
753 | 765 | | |
754 | 766 | | |
| |||
760 | 772 | | |
761 | 773 | | |
762 | 774 | | |
763 | | - | |
| 775 | + | |
764 | 776 | | |
765 | 777 | | |
766 | 778 | | |
| |||
771 | 783 | | |
772 | 784 | | |
773 | 785 | | |
774 | | - | |
| 786 | + | |
775 | 787 | | |
776 | 788 | | |
777 | 789 | | |
| |||
0 commit comments