Skip to content

Commit 8ea562f

Browse files
committed
Try to reduce TIME_WAIT
1 parent 51ee18c commit 8ea562f

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

client/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def post_mgmt_stop():
107107
utils.delete_pid_file("client", _CLIENT.name)
108108
# TODO: Process close listening socket when receive SIGTERM
109109
os.kill(os.getpid(), signal.SIGTERM)
110-
return {"result": "Hub stopped"}
110+
return {"result": "Client stopped"}
111111

112112

113113
def main():

hub/__main__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"""
44

55
import argparse
6-
import os
7-
import signal
86
import fastapi
97
import pydantic
108
import uvicorn
@@ -156,11 +154,8 @@ async def post_mgmt_stop():
156154
"""
157155
Management: Post stop.
158156
"""
159-
# TODO: Can we delete the PID file later, when the process actually terminates?
160-
utils.delete_pid_file("hub", _HUB.name)
161-
os.kill(os.getpid(), signal.SIGTERM)
162-
# TODO: Does this result actually get returned
163-
return {"result": "Hub stopped"}
157+
_HUB.initiate_stop()
158+
return {"result": "Hub stop initiated"}
164159

165160

166161
def main():

hub/hub.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
A DSKE security hub, or DSKE hub, or just hub for short.
33
"""
44

5+
import asyncio
6+
import os
7+
import signal
58
from uuid import UUID
69
import fastapi
710
from common import exceptions
11+
from common import utils
812
from common.allocation import Allocation
913
from common.block import Block
1014
from common.encryption_key import EncryptionKey
@@ -23,11 +27,13 @@ class Hub:
2327
_name: str
2428
_peer_clients: dict[str, PeerClient] # Indexed by client name
2529
_shares: dict[UUID, Share] # Indexed by key UUID
30+
_stop_task: asyncio.Task | None
2631

2732
def __init__(self, name: str):
2833
self._name = name
2934
self._peer_clients = {}
3035
self._shares = {}
36+
self._stop_task = None
3137

3238
@property
3339
def name(self):
@@ -139,3 +145,18 @@ async def get_share_requested_by_client(
139145
)
140146
peer_client.add_dske_signing_key_header_to_response(headers_temp_response)
141147
return response
148+
149+
def initiate_stop(self):
150+
"""
151+
Initiate stopping the hub.
152+
"""
153+
self._stop_task = asyncio.create_task(self._stop_after_delay())
154+
155+
async def _stop_after_delay(self):
156+
"""
157+
Stop the hub after a short delay to allow the HTTP response to be sent and to avoid
158+
TIME_WAIT states on the server.
159+
"""
160+
await asyncio.sleep(0.5)
161+
utils.delete_pid_file("hub", self._name)
162+
os.kill(os.getpid(), signal.SIGTERM)

0 commit comments

Comments
 (0)