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

Decide if ipv6 can work #2923

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions locust/rpc/zmqrpc.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from locust.exception import RPCError, RPCReceiveError, RPCSendError
from locust.util.exception_handler import retry

import socket as csocket
from socket import gaierror, has_dualstack_ipv6

import msgpack.exceptions as msgerr
import zmq.error as zmqerr
import zmq.green as zmq
from urllib3.util.connection import HAS_IPV6

from .protocol import Message


class BaseSocket:
def __init__(self, sock_type):
def __init__(self, sock_type, ipv4_only):
context = zmq.Context()
self.socket = context.socket(sock_type)

self.socket.setsockopt(zmq.TCP_KEEPALIVE, 1)
self.socket.setsockopt(zmq.TCP_KEEPALIVE_IDLE, 30)
if HAS_IPV6:
if has_dualstack_ipv6() and not ipv4_only:
self.socket.setsockopt(zmq.IPV6, 1)

@retry()
Expand Down Expand Up @@ -60,10 +62,19 @@ def recv_from_client(self):
def close(self, linger=None):
self.socket.close(linger=linger)

def ipv4_only(self, host, port) -> bool:
try:
if str(csocket.getaddrinfo(host, port, proto=csocket.IPPROTO_TCP)).find("Family.AF_INET6") == -1:
return True
except gaierror as e:
print(f"Error resolving address: {e}")
return False
return False


class Server(BaseSocket):
def __init__(self, host, port):
BaseSocket.__init__(self, zmq.ROUTER)
BaseSocket.__init__(self, zmq.ROUTER, self.ipv4_only(host, port))
if port == 0:
self.port = self.socket.bind_to_random_port(f"tcp://{host}")
else:
Expand All @@ -76,6 +87,6 @@ def __init__(self, host, port):

class Client(BaseSocket):
def __init__(self, host, port, identity):
BaseSocket.__init__(self, zmq.DEALER)
BaseSocket.__init__(self, zmq.DEALER, self.ipv4_only(host, port))
self.socket.setsockopt(zmq.IDENTITY, identity.encode())
self.socket.connect("tcp://%s:%i" % (host, port))