Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions modules/p2ptrust/p2ptrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def get_local_IP(self):

def get_available_port(self) -> int:
for port in range(32768, 65535):
if port == self.redis_port:
continue
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(("0.0.0.0", port))
Expand Down
33 changes: 31 additions & 2 deletions slips_files/core/database/redis_db/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SPDX-FileCopyrightText: 2021 Sebastian Garcia <[email protected]>
# SPDX-License-Identifier: GPL-2.0-only
import socket

from slips_files.common.printer import Printer
from slips_files.common.slips_utils import utils
from slips_files.common.parsers.config_parser import ConfigParser
Expand Down Expand Up @@ -252,6 +254,9 @@ def init_redis_server(cls) -> Tuple[bool, str]:
# starts the redis server using cli.
# we don't need that when using -k
cls._start_a_redis_server()
all_good, err = cls._confirm_redis_is_listening()
if not all_good:
return False, err

connected, err = cls.connect_to_redis_server()
if not connected:
Expand Down Expand Up @@ -314,11 +319,35 @@ def _connect(port: int, db: int) -> redis.StrictRedis:
health_check_interval=20,
)

@classmethod
def _confirm_redis_is_listening(cls, timeout: float = 5.0) -> (bool, str):
"""
Polls the redis port to confirm Redis is really listening
:param timeout: how long to keep polling before raising runtime error
"""
start = time.time()
while time.time() - start < timeout:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(0.2)
try:
sock.connect(("127.0.0.1", cls.redis_port))
return True, "" # Redis is up
except (ConnectionRefusedError, OSError):
time.sleep(0.2)

# If we reach here, port never opened
return False, (
f"_confirm_redis_is_listening: Redis failed to start on "
f"{cls.redis_port}"
)

@classmethod
def _start_a_redis_server(cls) -> bool:
cmd = (
f"redis-server {cls._conf_file} --port {cls.redis_port} "
f" --daemonize yes"
f"redis-server {cls._conf_file} "
f"--port {cls.redis_port} "
f"--bind 127.0.0.1 "
f"--daemonize yes"
)
process = subprocess.Popen(
cmd,
Expand Down
Loading