Skip to content

Commit

Permalink
Allow MT message send rate to be configured (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmcnulty authored Nov 14, 2024
1 parent 13f6ae1 commit cb81855
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/smpp_gateway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(
backend: Backend,
hc_worker: HealthchecksIoWorker,
submit_sm_params: dict,
mt_messages_per_second: int,
*args,
**kwargs,
):
Expand All @@ -77,6 +78,7 @@ def __init__(
self.backend = backend
self.hc_worker = hc_worker
self.submit_sm_params = submit_sm_params
self.mt_messages_per_second = mt_messages_per_second
super().__init__(*args, **kwargs)
self._pg_conn = pg_listen(self.backend.name)

Expand Down Expand Up @@ -172,7 +174,11 @@ def receive_pg_notify(self):
self.send_mt_messages()

def send_mt_messages(self):
smses = get_mt_messages_to_send(limit=100, backend=self.backend)
limit = self.mt_messages_per_second * self.timeout
smses = get_mt_messages_to_send(limit=limit, backend=self.backend)
if len(smses) == 0:
return
logger.info(f"Found {len(smses)} messages to send in {self.timeout} seconds")
submit_sm_resps = []
for sms in smses:
params = {**self.submit_sm_params, **sms["params"]}
Expand Down Expand Up @@ -222,7 +228,7 @@ def split_and_send_message(self, message, **kwargs):

def listen(self, ignore_error_codes=None, auto_send_enquire_link=True):
self.logger.info("Entering main listen loop")
# Look for and send up to 100 messages on start up
# Look for and send messages on start up
self.send_mt_messages()
while True:
# When either main socket has data or _pg_conn has data, select.select will return
Expand All @@ -233,7 +239,6 @@ def listen(self, ignore_error_codes=None, auto_send_enquire_link=True):
self.logger.debug("Socket timeout, listening again")
pdu = smpplib.smpp.make_pdu("enquire_link", client=self)
self.send_pdu(pdu)
# Look for and send up to 100 messages every 5 seconds
self.send_mt_messages()
continue
elif not rlist:
Expand Down
5 changes: 5 additions & 0 deletions src/smpp_gateway/management/commands/smpp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def add_arguments(self, parser):
"--submit-sm-params",
default=os.environ.get("SMPPLIB_SUBMIT_SM_PARAMS", r"{}"),
)
parser.add_argument(
"--mt-messages-per-second",
type=int,
default=os.environ.get("SMPPLIB_MT_MESSAGES_PER_SECOND", 20),
)
parser.add_argument(
"--database-url",
default=os.environ.get("DATABASE_URL"),
Expand Down
3 changes: 3 additions & 0 deletions src/smpp_gateway/smpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_smpplib_client(
notify_mo_channel: str,
backend: Backend,
submit_sm_params: dict,
mt_messages_per_second: int,
hc_check_uuid: str,
hc_ping_key: str,
hc_check_slug: str,
Expand All @@ -34,6 +35,7 @@ def get_smpplib_client(
backend,
hc_worker,
submit_sm_params,
mt_messages_per_second,
host,
port,
allow_unknown_opt_params=True,
Expand Down Expand Up @@ -67,6 +69,7 @@ def start_smpp_client(options):
options["notify_mo_channel"],
backend,
json.loads(options["submit_sm_params"]),
options["mt_messages_per_second"],
options["hc_check_uuid"],
options["hc_ping_key"],
options["hc_check_slug"],
Expand Down
4 changes: 4 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_received_mo_message(self):
"notify_mo_channel",
backend,
{}, # submit_sm_params
20, # mt_messages_per_second
"", # hc_check_uuid
"", # hc_ping_key
"", # hc_check_slug
Expand Down Expand Up @@ -57,6 +58,7 @@ def test_received_message_receipt(self):
"notify_mo_channel",
backend,
{}, # submit_sm_params
20, # mt_messages_per_second
"", # hc_check_uuid
"", # hc_ping_key
"", # hc_check_slug
Expand Down Expand Up @@ -98,6 +100,7 @@ def test_received_null_short_message(self):
"notify_mo_channel",
backend,
{}, # submit_sm_params
20, # mt_messages_per_second
"", # hc_check_uuid
"", # hc_ping_key
"", # hc_check_slug
Expand Down Expand Up @@ -135,6 +138,7 @@ def test_message_sent_handler():
"notify_mo_channel",
backend,
{}, # submit_sm_params
20, # mt_messages_per_second
"", # hc_check_uuid
"", # hc_ping_key
"", # hc_check_slug
Expand Down

0 comments on commit cb81855

Please sign in to comment.