Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IPv6 #258

Merged
merged 1 commit into from
Jan 8, 2025
Merged

Support IPv6 #258

merged 1 commit into from
Jan 8, 2025

Conversation

keisku
Copy link
Contributor

@keisku keisku commented Jan 7, 2025

Overview

This PR replaces manual socket creation with socket.create_connection() to support both IPv4 and IPv6 seamlessly, and updates the split_host_port() to support IPv6 address.

Motivation

IPv6 Support.

Previously, the code forced AF_INET for socket connections, making IPv6 usage either impossible or unreliable.

For example, empty response from client.stats().

python -c "import bmemcached; client = bmemcached.Client('2001:db8::2:11211', '', ''); print(client.stats())"
{'2001:db8::2:11211': {}}

By switching to socket.create_connection((self.host, self.port), self.socket_timeout), Python automatically selects the appropriate address family (IPv4 or IPv6).

Testing

Using docker-compose.

services:
  memcached:
    container_name: memcached
    image: bitnami/memcached:latest
    environment:
    - MEMCACHED_LISTEN_ADDRESS=[::]
    ports:
      - "11211:11211"
    networks:
      - ip6net
networks:
  ip6net:
    enable_ipv6: true
    ipam:
      config:
        - subnet: 2001:db8::/64
docker inspect memcached | grep GlobalIPv6Address
            "GlobalIPv6Address": "",
                    "GlobalIPv6Address": "2001:db8::2",

Make a memcached client container image.

docker build --no-cache -t memcached-client --quiet -<<-EOD
FROM python:3.9
WORKDIR /src
RUN <<RUNEND
git clone --branch support-ipv6 --depth=1 https://github.com/keisku/python-binary-memcached.git
cd python-binary-memcached
pip install .
RUNEND
EOD
docker run --rm --network host memcached-client python -c "import bmemcached; client = bmemcached.Client('[2001:db8::2]', '', ''); print(client.stats()); client.disconnect_all()"
{'[2001:db8::2]': {'pid': b'1', 'uptime': b'61798', 'time': b'1736215951', 'version': b'1.6.34', 'libevent': b'2.1.12-stable', 'pointer_size': b'64', 'rusage_user': b'4.650069', 'rusage_system': b'1.189757', 'max_connections': b'1024', 'curr_connections': b'1', 'total_connections': b'7', 'rejected_connections': b'0', 'connection_structures': b'2', 'response_obj_oom': b'0', 'response_obj_count': b'1', 'response_obj_bytes': b'65536', 'read_buf_count': b'8', 'read_buf_bytes': b'131072', 'read_buf_bytes_free': b'49152', 'read_buf_oom': b'0', 'reserved_fds': b'20', 'cmd_get': b'0', 'cmd_set': b'0', 'cmd_flush': b'0', 'cmd_touch': b'0', 'cmd_meta': b'0', 'get_hits': b'0', 'get_misses': b'0', 'get_expired': b'0', 'get_flushed': b'0', 'delete_misses': b'0', 'delete_hits': b'0', 'incr_misses': b'0', 'incr_hits': b'0', 'decr_misses': b'0', 'decr_hits': b'0', 'cas_misses': b'0', 'cas_hits': b'0', 'cas_badval': b'0', 'touch_hits': b'0', 'touch_misses': b'0', 'store_too_large': b'0', 'store_no_memory': b'0', 'auth_cmds': b'0', 'auth_errors': b'0', 'bytes_read': b'144', 'bytes_written': b'18501', 'limit_maxbytes': b'67108864', 'accepting_conns': b'1', 'listen_disabled_num': b'0', 'time_in_listen_disabled_us': b'0', 'threads': b'4', 'conn_yields': b'0', 'hash_power_level': b'16', 'hash_bytes': b'524288', 'hash_is_expanding': b'0', 'slab_reassign_rescues': b'0', 'slab_reassign_chunk_rescues': b'0', 'slab_reassign_inline_reclaim': b'0', 'slab_reassign_busy_items': b'0', 'slab_reassign_busy_deletes': b'0', 'slab_reassign_busy_nomem': b'0', 'slab_reassign_running': b'0', 'slabs_moved': b'0', 'lru_crawler_running': b'0', 'lru_crawler_starts': b'45', 'lru_maintainer_juggles': b'61841', 'malloc_fails': b'0', 'log_worker_dropped': b'0', 'log_worker_written': b'0', 'log_watcher_skipped': b'0', 'log_watcher_sent': b'0', 'log_watchers': b'0', 'unexpected_napi_ids': b'0', 'round_robin_fallback': b'0', 'bytes': b'0', 'curr_items': b'0', 'total_items': b'0', 'slab_global_page_pool': b'0', 'expired_unfetched': b'0', 'evicted_unfetched': b'0', 'evicted_active': b'0', 'evictions': b'0', 'reclaimed': b'0', 'crawler_reclaimed': b'0', 'crawler_items_checked': b'0', 'lrutail_reflocked': b'0', 'moves_to_cold': b'0', 'moves_to_warm': b'0', 'moves_within_lru': b'0', 'direct_reclaims': b'0', 'lru_bumps_dropped': b'0'}}

docker run --rm --network host memcached-client python -c "import bmemcached; client = bmemcached.Client('[2001:db8::2]:11211', '', ''); print(client.stats()); client.disconnect_all()"
{'[2001:db8::2]:11211': {'pid': b'1', 'uptime': b'61811', 'time': b'1736215964', 'version': b'1.6.34', 'libevent': b'2.1.12-stable', 'pointer_size': b'64', 'rusage_user': b'4.651855', 'rusage_system': b'1.189757', 'max_connections': b'1024', 'curr_connections': b'1', 'total_connections': b'9', 'rejected_connections': b'0', 'connection_structures': b'2', 'response_obj_oom': b'0', 'response_obj_count': b'1', 'response_obj_bytes': b'65536', 'read_buf_count': b'8', 'read_buf_bytes': b'131072', 'read_buf_bytes_free': b'49152', 'read_buf_oom': b'0', 'reserved_fds': b'20', 'cmd_get': b'0', 'cmd_set': b'0', 'cmd_flush': b'0', 'cmd_touch': b'0', 'cmd_meta': b'0', 'get_hits': b'0', 'get_misses': b'0', 'get_expired': b'0', 'get_flushed': b'0', 'delete_misses': b'0', 'delete_hits': b'0', 'incr_misses': b'0', 'incr_hits': b'0', 'decr_misses': b'0', 'decr_hits': b'0', 'cas_misses': b'0', 'cas_hits': b'0', 'cas_badval': b'0', 'touch_hits': b'0', 'touch_misses': b'0', 'store_too_large': b'0', 'store_no_memory': b'0', 'auth_cmds': b'0', 'auth_errors': b'0', 'bytes_read': b'192', 'bytes_written': b'25911', 'limit_maxbytes': b'67108864', 'accepting_conns': b'1', 'listen_disabled_num': b'0', 'time_in_listen_disabled_us': b'0', 'threads': b'4', 'conn_yields': b'0', 'hash_power_level': b'16', 'hash_bytes': b'524288', 'hash_is_expanding': b'0', 'slab_reassign_rescues': b'0', 'slab_reassign_chunk_rescues': b'0', 'slab_reassign_inline_reclaim': b'0', 'slab_reassign_busy_items': b'0', 'slab_reassign_busy_deletes': b'0', 'slab_reassign_busy_nomem': b'0', 'slab_reassign_running': b'0', 'slabs_moved': b'0', 'lru_crawler_running': b'0', 'lru_crawler_starts': b'45', 'lru_maintainer_juggles': b'61855', 'malloc_fails': b'0', 'log_worker_dropped': b'0', 'log_worker_written': b'0', 'log_watcher_skipped': b'0', 'log_watcher_sent': b'0', 'log_watchers': b'0', 'unexpected_napi_ids': b'0', 'round_robin_fallback': b'0', 'bytes': b'0', 'curr_items': b'0', 'total_items': b'0', 'slab_global_page_pool': b'0', 'expired_unfetched': b'0', 'evicted_unfetched': b'0', 'evicted_active': b'0', 'evictions': b'0', 'reclaimed': b'0', 'crawler_reclaimed': b'0', 'crawler_items_checked': b'0', 'lrutail_reflocked': b'0', 'moves_to_cold': b'0', 'moves_to_warm': b'0', 'moves_within_lru': b'0', 'direct_reclaims': b'0', 'lru_bumps_dropped': b'0'}}

# Without brackets.
docker run --rm --network host memcached-client python -c "import bmemcached; client = bmemcached.Client('2001:db8::2', '', ''); print(client.stats()); client.disconnect_all()"
{'2001:db8::2': {'pid': b'1', 'uptime': b'61801', 'time': b'1736215954', 'version': b'1.6.34', 'libevent': b'2.1.12-stable', 'pointer_size': b'64', 'rusage_user': b'4.650573', 'rusage_system': b'1.189757', 'max_connections': b'1024', 'curr_connections': b'1', 'total_connections': b'8', 'rejected_connections': b'0', 'connection_structures': b'2', 'response_obj_oom': b'0', 'response_obj_count': b'1', 'response_obj_bytes': b'65536', 'read_buf_count': b'8', 'read_buf_bytes': b'131072', 'read_buf_bytes_free': b'49152', 'read_buf_oom': b'0', 'reserved_fds': b'20', 'cmd_get': b'0', 'cmd_set': b'0', 'cmd_flush': b'0', 'cmd_touch': b'0', 'cmd_meta': b'0', 'get_hits': b'0', 'get_misses': b'0', 'get_expired': b'0', 'get_flushed': b'0', 'delete_misses': b'0', 'delete_hits': b'0', 'incr_misses': b'0', 'incr_hits': b'0', 'decr_misses': b'0', 'decr_hits': b'0', 'cas_misses': b'0', 'cas_hits': b'0', 'cas_badval': b'0', 'touch_hits': b'0', 'touch_misses': b'0', 'store_too_large': b'0', 'store_no_memory': b'0', 'auth_cmds': b'0', 'auth_errors': b'0', 'bytes_read': b'168', 'bytes_written': b'22206', 'limit_maxbytes': b'67108864', 'accepting_conns': b'1', 'listen_disabled_num': b'0', 'time_in_listen_disabled_us': b'0', 'threads': b'4', 'conn_yields': b'0', 'hash_power_level': b'16', 'hash_bytes': b'524288', 'hash_is_expanding': b'0', 'slab_reassign_rescues': b'0', 'slab_reassign_chunk_rescues': b'0', 'slab_reassign_inline_reclaim': b'0', 'slab_reassign_busy_items': b'0', 'slab_reassign_busy_deletes': b'0', 'slab_reassign_busy_nomem': b'0', 'slab_reassign_running': b'0', 'slabs_moved': b'0', 'lru_crawler_running': b'0', 'lru_crawler_starts': b'45', 'lru_maintainer_juggles': b'61844', 'malloc_fails': b'0', 'log_worker_dropped': b'0', 'log_worker_written': b'0', 'log_watcher_skipped': b'0', 'log_watcher_sent': b'0', 'log_watchers': b'0', 'unexpected_napi_ids': b'0', 'round_robin_fallback': b'0', 'bytes': b'0', 'curr_items': b'0', 'total_items': b'0', 'slab_global_page_pool': b'0', 'expired_unfetched': b'0', 'evicted_unfetched': b'0', 'evicted_active': b'0', 'evictions': b'0', 'reclaimed': b'0', 'crawler_reclaimed': b'0', 'crawler_items_checked': b'0', 'lrutail_reflocked': b'0', 'moves_to_cold': b'0', 'moves_to_warm': b'0', 'moves_within_lru': b'0', 'direct_reclaims': b'0', 'lru_bumps_dropped': b'0'}}

Regression tests

services:
  memcached:
    container_name: memcached
    image: bitnami/memcached:latest
docker inspect memcached | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.20.0.2",
docker run --rm --network host memcached-client python -c "import bmemcached; client = bmemcached.Client('172.20.0.2:11211', '', ''); print(client.stats()); client.disconnect_all()"
{'172.20.0.2:11211': {'pid': b'1', 'uptime': b'29', 'time': b'1736216159', 'version': b'1.6.34', 'libevent': b'2.1.12-stable', 'pointer_size': b'64', 'rusage_user': b'0.036675', 'rusage_system': b'0.017052', 'max_connections': b'1024', 'curr_connections': b'2', 'total_connections': b'3', 'rejected_connections': b'0', 'connection_structures': b'3', 'response_obj_oom': b'0', 'response_obj_count': b'1', 'response_obj_bytes': b'16384', 'read_buf_count': b'2', 'read_buf_bytes': b'32768', 'read_buf_bytes_free': b'0', 'read_buf_oom': b'0', 'reserved_fds': b'20', 'cmd_get': b'0', 'cmd_set': b'0', 'cmd_flush': b'0', 'cmd_touch': b'0', 'cmd_meta': b'0', 'get_hits': b'0', 'get_misses': b'0', 'get_expired': b'0', 'get_flushed': b'0', 'delete_misses': b'0', 'delete_hits': b'0', 'incr_misses': b'0', 'incr_hits': b'0', 'decr_misses': b'0', 'decr_hits': b'0', 'cas_misses': b'0', 'cas_hits': b'0', 'cas_badval': b'0', 'touch_hits': b'0', 'touch_misses': b'0', 'store_too_large': b'0', 'store_no_memory': b'0', 'auth_cmds': b'0', 'auth_errors': b'0', 'bytes_read': b'24', 'bytes_written': b'0', 'limit_maxbytes': b'67108864', 'accepting_conns': b'1', 'listen_disabled_num': b'0', 'time_in_listen_disabled_us': b'0', 'threads': b'4', 'conn_yields': b'0', 'hash_power_level': b'16', 'hash_bytes': b'524288', 'hash_is_expanding': b'0', 'slab_reassign_rescues': b'0', 'slab_reassign_chunk_rescues': b'0', 'slab_reassign_inline_reclaim': b'0', 'slab_reassign_busy_items': b'0', 'slab_reassign_busy_deletes': b'0', 'slab_reassign_busy_nomem': b'0', 'slab_reassign_running': b'0', 'slabs_moved': b'0', 'lru_crawler_running': b'0', 'lru_crawler_starts': b'1', 'lru_maintainer_juggles': b'79', 'malloc_fails': b'0', 'log_worker_dropped': b'0', 'log_worker_written': b'0', 'log_watcher_skipped': b'0', 'log_watcher_sent': b'0', 'log_watchers': b'0', 'unexpected_napi_ids': b'0', 'round_robin_fallback': b'0', 'bytes': b'0', 'curr_items': b'0', 'total_items': b'0', 'slab_global_page_pool': b'0', 'expired_unfetched': b'0', 'evicted_unfetched': b'0', 'evicted_active': b'0', 'evictions': b'0', 'reclaimed': b'0', 'crawler_reclaimed': b'0', 'crawler_items_checked': b'0', 'lrutail_reflocked': b'0', 'moves_to_cold': b'0', 'moves_to_warm': b'0', 'moves_within_lru': b'0', 'direct_reclaims': b'0', 'lru_bumps_dropped': b'0'}}

docker run --rm --network host memcached-client python -c "import bmemcached; client = bmemcached.Client('172.20.0.2', '', ''); print(client.stats()); client.disconnect_all()"
{'172.20.0.2': {'pid': b'1', 'uptime': b'35', 'time': b'1736216165', 'version': b'1.6.34', 'libevent': b'2.1.12-stable', 'pointer_size': b'64', 'rusage_user': b'0.037426', 'rusage_system': b'0.017109', 'max_connections': b'1024', 'curr_connections': b'2', 'total_connections': b'4', 'rejected_connections': b'0', 'connection_structures': b'3', 'response_obj_oom': b'0', 'response_obj_count': b'1', 'response_obj_bytes': b'32768', 'read_buf_count': b'4', 'read_buf_bytes': b'65536', 'read_buf_bytes_free': b'16384', 'read_buf_oom': b'0', 'reserved_fds': b'20', 'cmd_get': b'0', 'cmd_set': b'0', 'cmd_flush': b'0', 'cmd_touch': b'0', 'cmd_meta': b'0', 'get_hits': b'0', 'get_misses': b'0', 'get_expired': b'0', 'get_flushed': b'0', 'delete_misses': b'0', 'delete_hits': b'0', 'incr_misses': b'0', 'incr_hits': b'0', 'decr_misses': b'0', 'decr_hits': b'0', 'cas_misses': b'0', 'cas_hits': b'0', 'cas_badval': b'0', 'touch_hits': b'0', 'touch_misses': b'0', 'store_too_large': b'0', 'store_no_memory': b'0', 'auth_cmds': b'0', 'auth_errors': b'0', 'bytes_read': b'48', 'bytes_written': b'3688', 'limit_maxbytes': b'67108864', 'accepting_conns': b'1', 'listen_disabled_num': b'0', 'time_in_listen_disabled_us': b'0', 'threads': b'4', 'conn_yields': b'0', 'hash_power_level': b'16', 'hash_bytes': b'524288', 'hash_is_expanding': b'0', 'slab_reassign_rescues': b'0', 'slab_reassign_chunk_rescues': b'0', 'slab_reassign_inline_reclaim': b'0', 'slab_reassign_busy_items': b'0', 'slab_reassign_busy_deletes': b'0', 'slab_reassign_busy_nomem': b'0', 'slab_reassign_running': b'0', 'slabs_moved': b'0', 'lru_crawler_running': b'0', 'lru_crawler_starts': b'1', 'lru_maintainer_juggles': b'84', 'malloc_fails': b'0', 'log_worker_dropped': b'0', 'log_worker_written': b'0', 'log_watcher_skipped': b'0', 'log_watcher_sent': b'0', 'log_watchers': b'0', 'unexpected_napi_ids': b'0', 'round_robin_fallback': b'0', 'bytes': b'0', 'curr_items': b'0', 'total_items': b'0', 'slab_global_page_pool': b'0', 'expired_unfetched': b'0', 'evicted_unfetched': b'0', 'evicted_active': b'0', 'evictions': b'0', 'reclaimed': b'0', 'crawler_reclaimed': b'0', 'crawler_items_checked': b'0', 'lrutail_reflocked': b'0', 'moves_to_cold': b'0', 'moves_to_warm': b'0', 'moves_within_lru': b'0', 'direct_reclaims': b'0', 'lru_bumps_dropped': b'0'}}

@keisku keisku marked this pull request as ready for review January 7, 2025 02:17
@@ -9,6 +9,7 @@
from urllib.parse import SplitResult # type: ignore[import-not-found]

import zlib
from ipaddress import ip_address
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me just check with someone if it is ok to drop support for python2 and I'll review your pr, thanks for that!

@jaysonsantos jaysonsantos merged commit efeb037 into jaysonsantos:master Jan 8, 2025
@jaysonsantos
Copy link
Owner

Thank you so much for the help @keisku !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants