Created
February 19, 2022 14:28
-
-
Save iandol/a0cd594d790230435d4f3d343830da9f to your computer and use it in GitHub Desktop.
Lazypirate server / client test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Lazy Pirate client | |
# Use zmq_poll to do a safe request-reply | |
# To run, start lpserver and then randomly kill/restart it | |
# | |
# Author: Daniel Lundin <dln(at)eintr(dot)org> | |
# | |
import itertools | |
import logging | |
import sys | |
import zmq | |
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) | |
REQUEST_TIMEOUT = 2500 | |
REQUEST_RETRIES = 3 | |
SERVER_ENDPOINT = "tcp://192.168.1.44:5454" | |
try: | |
context = zmq.Context() | |
logging.info("Connecting to server…") | |
client = context.socket(zmq.REQ) | |
client.connect(SERVER_ENDPOINT) | |
for sequence in itertools.count(): | |
request = str(sequence).encode() | |
logging.info("Sending (%s)", request) | |
client.send(request) | |
retries_left = REQUEST_RETRIES | |
while True: | |
if (client.poll(REQUEST_TIMEOUT) & zmq.POLLIN) != 0: | |
reply = client.recv() | |
if int(reply) == sequence: | |
logging.info("Server replied OK (%s)", reply) | |
retries_left = REQUEST_RETRIES | |
break | |
else: | |
logging.error("Malformed reply from server: %s", reply) | |
continue | |
retries_left -= 1 | |
logging.warning("No response from server") | |
# Socket is confused. Close and remove it. | |
client.setsockopt(zmq.LINGER, 0) | |
client.close() | |
if retries_left == 0: | |
logging.error("Server seems to be offline, abandoning") | |
sys.exit() | |
logging.info("Reconnecting to server…") | |
# Create new connection | |
client = context.socket(zmq.REQ) | |
client.connect(SERVER_ENDPOINT) | |
logging.info("Resending (%s)", request) | |
client.send(request) | |
except KeyboardInterrupt: | |
print('> User forced exit!') | |
finally: | |
context.term() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Lazy Pirate server | |
# Binds REQ socket to tcp://*:5555 | |
# Like hwserver except: | |
# - echoes request as-is | |
# - randomly runs slowly, or exits to simulate a crash. | |
# | |
# Author: Daniel Lundin <dln(at)eintr(dot)org> | |
# | |
from random import randint | |
import itertools | |
import logging | |
import time | |
import zmq | |
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) | |
try: | |
context = zmq.Context() | |
server = context.socket(zmq.REP) | |
server.bind("tcp://*:5454") | |
for cycles in itertools.count(): | |
request = server.recv() | |
# Simulate various problems, after a few cycles | |
if cycles > 30 and randint(0, 3) == 0: | |
logging.info("Simulating a crash") | |
break | |
elif cycles > 3 and randint(0, 3) == 0: | |
logging.info("Simulating CPU overload") | |
time.sleep(2) | |
logging.info("Normal request (%s)", request) | |
time.sleep(0.25) # Do some heavy work | |
server.send(request) | |
except KeyboardInterrupt: | |
print('> User forced exit!') | |
finally: | |
context.term() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment