aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojciech Kosior <kwojtus@protonmail.com>2020-06-15 16:25:00 +0200
committerWojciech Kosior <kwojtus@protonmail.com>2020-06-15 16:25:00 +0200
commit01464c698df4df619a192474d1de8ff62e4e5d7a (patch)
tree5ae2ad9f0010252b6417ea524b13202b5f0e8c59
parenteaf411175906eebaf8bb4411c9232b054f7222bd (diff)
download0tdns-01464c698df4df619a192474d1de8ff62e4e5d7a.tar.gz
0tdns-01464c698df4df619a192474d1de8ff62e4e5d7a.zip
specify number of simultaneous vpn connections in config file
-rw-r--r--db_connection_config.yml3
-rwxr-xr-xsrc/hourly.py23
2 files changed, 18 insertions, 8 deletions
diff --git a/db_connection_config.yml b/db_connection_config.yml
index 5e23380..680584c 100644
--- a/db_connection_config.yml
+++ b/db_connection_config.yml
@@ -4,4 +4,5 @@ host: "127.0.0.1"
port: "5432"
database: "ztdns"
enabled: no
-handled_vpns: [1, 2] \ No newline at end of file
+handled_vpns: [1, 2]
+parallel_vpns: 20
diff --git a/src/hourly.py b/src/hourly.py
index 3aa821d..155113e 100755
--- a/src/hourly.py
+++ b/src/hourly.py
@@ -2,7 +2,7 @@
from sys import argv
import subprocess
-from os import path
+from os import path, waitpid
from time import gmtime, strftime, sleep
# our own module used by several scripts in the project
@@ -70,9 +70,19 @@ with open("/var/log/0tdns.log", "w") as logfile:
cursor.close()
connection.close()
- subprocesses = []
+ parallel_vpns = ztdns_config['parallel_vpns']
+ vpn_wrapper_pids = set()
for vpn_id, config_hash in vpns:
+ if len(vpn_wrapper_pids) == parallel_vpns:
+ while True:
+ pid, exit_status = waitpid(0, 0)
+ if pid in vpn_wrapper_pids:
+ break
+ if exit_status != 0:
+ logfile.write("one of vpn connections failed")
+ vpn_wrapper_pids.remove(pid)
+
config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash)
physical_ip = get_default_host_address(ztdns_config['host'])
route_through_veth = ztdns_config['host'] + "/32"
@@ -81,9 +91,8 @@ with open("/var/log/0tdns.log", "w") as logfile:
p = subprocess.Popen([wrapper, config_path, physical_ip,
route_through_veth] + command_in_namespace)
-
- subprocesses.append(p)
- sleep(5)
- for p in subprocesses:
- p.wait()
+ vpn_wrapper_pids.add(p.pid)
+
+ for pid in vpn_wrapper_pids:
+ waitpid(pid, 0)