Skip to content

Commit 28137a0

Browse files
committed
Make ping count/delay configurable
* Adds `float` type to allowed value types * Modifies `entrypoints.run_as_ping` to use new config options * Modifies controller.send_directive` to use a new param named `expected_responses`, which allows us to stop waiting for more responses after the set # is received (defaults to 1 response)
1 parent 7f46155 commit 28137a0

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

receptor/config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ def __init__(self, args=None):
181181
value_type='path',
182182
hint='Path to control socket for controller commands.',
183183
)
184+
self.add_config_option(
185+
section='ping',
186+
key='count',
187+
default_value=0,
188+
value_type='int',
189+
hint='Number of pings to send. If unspecified here or in a config file pings will be continuously sent until interrupted.',
190+
)
191+
self.add_config_option(
192+
section='ping',
193+
key='delay',
194+
default_value=0,
195+
value_type='float',
196+
hint='The delay (in seconds) to wait between pings. If unspecified here or in a config file pings will be sent as soon as the previous response is received.',
197+
)
184198
self.add_config_option(
185199
section='ping',
186200
key='recipient',
@@ -360,6 +374,8 @@ def _enforce_value_type(self, value, value_type):
360374
return value_type(value)
361375
elif value_type == 'int' and not isinstance(value, int):
362376
return int(value)
377+
elif value_type == 'float' and not isinstance(value, float):
378+
return float(value)
363379
elif value_type == 'str' and not isinstance(value, str):
364380
return '%s' % (value,)
365381
elif value_type == 'bool' and not isinstance(value, bool):

receptor/controller.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import asyncio
22
import logging
3+
import os
34
import socket
45
import sys
5-
import os
66

77
from . import protocol
88

99
logger = logging.getLogger(__name__)
1010

1111

12-
def send_directive(directive, recipient, payload, socket_path):
12+
def send_directive(directive, recipient, payload, socket_path, expected_responses=1):
1313
if payload == '-':
1414
payload = sys.stdin.read()
1515
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1616
sock.connect(socket_path)
1717
sock.sendall(f"{recipient}\n{directive}\n{payload}".encode('utf-8') + protocol.DELIM)
1818
response = b''
19-
while True:
19+
response_count = 0
20+
while response_count < expected_responses:
2021
response = sock.recv(4096)
2122
sys.stdout.buffer.write(response + b"\n")
2223
sys.stdout.flush()
24+
response_count += 1
2325

2426
# FIXME: the socket path is in the config, it shouldn't need to be passed as an arg here
2527
def mainloop(receptor, socket_path, loop=asyncio.get_event_loop()):

receptor/entrypoints.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import logging
3+
import time
34

45
from .receptor import Receptor
56
from . import node
@@ -17,7 +18,14 @@ def run_as_controller(config):
1718
def run_as_ping(config):
1819
logger.info(f'Sending ping to {config.ping_recipient}.')
1920
now = datetime.datetime.utcnow()
20-
controller.send_directive('receptor:ping', config.ping_recipient, now.isoformat(), config.ping_socket_path)
21+
pings_sent = 0
22+
while True:
23+
controller.send_directive('receptor:ping', config.ping_recipient, now.isoformat(), config.ping_socket_path)
24+
pings_sent += 1
25+
if config.ping_count != 0 and pings_sent >= config.ping_count:
26+
break
27+
elif config.ping_delay > 0.0:
28+
time.sleep(config.ping_delay)
2129

2230

2331
def run_as_send(config):

0 commit comments

Comments
 (0)