Skip to content

Commit 3feff87

Browse files
Adds encoding docstrings
Signed-off-by: Elena Kolevska <[email protected]>
1 parent 7f197ac commit 3feff87

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

redis/client.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def __init__(
401401
Recommended values:
402402
- Production systems: True (recommended for all connections)
403403
- Connection pools: True (essential - affects all pool connections)
404-
- Development/testing: False or None (for simplicity)
404+
- Development/testing: False or None (for simplicity and catching network issues early)
405405
Trade-offs:
406406
- True: Detects dead connections but uses more network resources (only during idle periods)
407407
- False: Lower network overhead but may not detect connection failures
@@ -461,6 +461,118 @@ def __init__(
461461
- More frequent keepalive packets increase network usage
462462
- Faster dead connection detection improves reliability
463463
464+
connection_pool (Optional[ConnectionPool], default=None):
465+
Main description: Pre-configured connection pool instance.
466+
Recommended values:
467+
- None: Auto-create pool with provided parameters
468+
- Custom pool: For advanced configuration or sharing
469+
Trade-offs:
470+
- Auto-created: Simple but less control
471+
- Custom pool: Full control but more complex setup
472+
Related parameters: max_connections, single_connection_client
473+
Performance implications:
474+
- Connection reuse reduces establishment overhead
475+
- Pool size affects concurrency and memory usage
476+
- Proper sizing is critical for performance
477+
478+
unix_socket_path (Optional[str], default=None):
479+
Main description: Path to Unix domain socket for local Redis connections.
480+
Recommended values:
481+
- Local Redis: "/var/run/redis/redis.sock"
482+
- Docker: "/tmp/redis.sock" (with volume mount)
483+
- None: Use TCP connection instead
484+
Trade-offs:
485+
- Unix socket: Lower latency, higher throughput for local connections
486+
- TCP socket: Works across network but higher overhead
487+
Related parameters: host, port (mutually exclusive with unix socket)
488+
Use cases:
489+
- Same-machine deployment: Optimal performance
490+
- Container sidecar: Redis in same pod/container
491+
- High-performance applications: Minimize network stack overhead
492+
Common issues:
493+
- File permissions: Socket file must be accessible
494+
- Path doesn't exist: Redis must create socket at specified path
495+
- Container volumes: Socket path must be mounted correctly
496+
Performance implications:
497+
- Better performance than TCP for local connections
498+
- Lower CPU usage (no network stack processing)
499+
- Not available for remote connections
500+
501+
encoding (str, default="utf-8"):
502+
Main description: Character encoding for string values when encode/decode operations occur.
503+
Recommended values:
504+
- UTF-8: "utf-8" (recommended for most applications, universal Unicode support)
505+
- ASCII: "ascii" (for ASCII-only data, fails on non-ASCII characters)
506+
- Latin-1: "latin-1" (for binary data compatibility, maps bytes 0-255 directly)
507+
Accepted values: Any encoding supported by Python's codecs module (120+ encodings)
508+
- Common: utf-8, ascii, latin-1, iso-8859-1, cp1252, utf-16, utf-32
509+
- Asian: big5, gb2312, gbk, shift_jis, euc_jp, euc_kr
510+
- Cyrillic: koi8_r, koi8_u, cp1251
511+
- See: https://docs.python.org/3/library/codecs.html#standard-encodings
512+
Trade-offs:
513+
- UTF-8: Universal Unicode support, backward compatible with ASCII
514+
- ASCII: Strict 7-bit encoding, fails on characters > 127
515+
- Latin-1: Handles any byte value, but not true Unicode beyond Latin characters
516+
Related parameters: encoding_errors, decode_responses
517+
Use cases:
518+
- International apps: UTF-8 for full Unicode support
519+
- Legacy systems: ASCII or Latin-1 for compatibility
520+
- Binary data: Latin-1 to avoid encoding errors
521+
Common issues:
522+
- Encoding mismatches: Client and server using different encodings
523+
- Binary data corruption: UTF-8 encoding binary values
524+
- UnicodeEncodeError: ASCII/Latin-1 with non-compatible characters
525+
526+
encoding_errors (str, default="strict"):
527+
Main description: How to handle encoding/decoding errors when they occur.
528+
Recommended values:
529+
- "strict": Raise exception on encoding errors (recommended for data integrity)
530+
- "ignore": Skip invalid characters (data loss risk)
531+
- "replace": Replace invalid chars with ? (encoding) or � (decoding)
532+
Additional options:
533+
- "backslashreplace": Use backslash escape sequences (\\uXXXX)
534+
- "xmlcharrefreplace": Use XML character references (&#NNNN;) - encoding only
535+
- "namereplace": Use Unicode names (\\N{...}) - encoding only
536+
Accepted values: Any error handler supported by Python's codecs module
537+
- See: https://docs.python.org/3/library/codecs.html#error-handlers
538+
Trade-offs:
539+
- strict: Data integrity guaranteed but may cause failures
540+
- ignore: Continues processing but silently loses data
541+
- replace: Preserves processing flow but corrupts data
542+
- backslashreplace: Preserves information but changes data format
543+
Related parameters: encoding, decode_responses
544+
Use cases:
545+
- Production: "strict" to catch encoding issues early
546+
- Data migration: "ignore" or "replace" for dirty data
547+
- Debugging: "strict" to identify encoding problems
548+
- Logging: "backslashreplace" to preserve problematic characters
549+
Common issues:
550+
- Silent data corruption: Using "ignore" or "replace"
551+
- Application crashes: "strict" with invalid data
552+
- Inconsistent behavior: Different error handling across environments
553+
554+
decode_responses (bool, default=False):
555+
Main description: Automatically decode byte responses to strings.
556+
Recommended values:
557+
- Web applications: True (convenient string handling)
558+
- High-performance: False (avoid encoding overhead)
559+
- Mixed data: False (handle encoding per operation)
560+
Trade-offs:
561+
- True: Convenient but adds overhead and may corrupt binary data
562+
- False: More control but requires manual decoding
563+
Related parameters: encoding, encoding_errors
564+
Use cases:
565+
- String-only data: True for convenience
566+
- Binary data: False to preserve byte integrity
567+
- Performance-critical: False to minimize overhead
568+
Common issues:
569+
- Binary data corruption: Decoding binary values as strings
570+
- Type confusion: Expecting bytes but getting strings
571+
- Performance degradation: Unnecessary encoding/decoding
572+
Performance implications:
573+
- Affects all read operations
574+
- Memory usage increases for string objects
575+
464576
To specify a retry policy for specific errors, you have two options:
465577
466578
1. Set the `retry_on_error` to a list of the error/s to retry on, and

0 commit comments

Comments
 (0)