Skip to content

Commit

Permalink
Use ThreadingTCPServer
Browse files Browse the repository at this point in the history
Use socket.ThreadingTCPServer for RedirectServer. Allows
multiple simultaneous requests.

Add manual test helper script tools/requests-timer.sh

Issue #9
  • Loading branch information
jtmoon79 committed Dec 8, 2019
1 parent 1ff289c commit 32353d5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
12 changes: 10 additions & 2 deletions goto_http_redirect_server/goto_http_redirect_server.py
Expand Up @@ -52,10 +52,11 @@
IP_LOCALHOST = '127.0.0.1'
HOSTNAME = socket.gethostname()

# RedirectServer class things
SOCKET_LISTEN_BACKLOG = 30 # eventually passed to socket.listen
# HTTP Status Code used for redirects (among several possible redirect codes)
REDIRECT_CODE_DEFAULT = http.HTTPStatus.PERMANENT_REDIRECT
Redirect_Code = REDIRECT_CODE_DEFAULT

SIGNAL_RELOAD_UNIX = 'SIGUSR1'
SIGNAL_RELOAD_WINDOWS = 'SIGBREAK'
# signal to cause --redirects file reload
Expand Down Expand Up @@ -700,12 +701,19 @@ def load_redirects(from_to: FromTo_List,
return entrys_files


class RedirectServer(socketserver.TCPServer):
class RedirectServer(socketserver.ThreadingTCPServer):
"""
Custom Server to allow reloading redirects while serve_forever.
"""
field_delimiter = FIELD_DELMITER_DEFAULT

def __init__(self, *args):
"""adjust parameters of the Parent class"""
super().__init__(*args)
self.block_on_close = False
self.request_queue_size = SOCKET_LISTEN_BACKLOG
self.timeout = 5

def __enter__(self):
"""Python version <= 3.5 does not implement BaseServer.__enter__"""
if hasattr(socketserver.TCPServer, '__enter__'):
Expand Down
73 changes: 73 additions & 0 deletions tools/requests-timer.sh
@@ -0,0 +1,73 @@
#!/usr/bin/env bash

set -e
set -u

if [[ ${#} != 1 ]] && [[ ${#} != 2 ]]; then
echo "usage:
$(basename -- "${0}") URL [COUNT]
URL of the request
COUNT of request processes
This script is to aid manual testing of many requests and the time elapsed.
It starts COUNT child processes that loop curl request of URL.
For each request, it prints:
milliseconds for request,HTTP return code,[PASS|FAIL] of curl request
For example:
913,200,PASS
" >&2
exit 1
fi


readonly url=${1}
declare -a PIDs=()

function time_ms() {
# epoch time in milliseconds
echo -n "$(($(date '+%s%N') / 1000000))"
}

declare -i reqs=${2:-5} # requestors
declare tspl=0.1 # time sleep per launch, per request

# create ${reqs} number of child process of loops of curl requests
for i in $(seq 1 ${reqs}); do
(
set +e
sleep $(python -c "print(${reqs} * ${tspl} + 2);")
declare mesg=
while sleep ${tspl}; do
start=$(time_ms)
if out=$(curl -v "${url}" 2>&1); then
mesg='PASS'
else
mesg='FAIL'
fi
end=$(time_ms)
code=$(echo "${out}" | grep -Fe '< HTTP/1.0 ' | grep -oEe ' [[:digit:]]+ ' | tr -d ' ')
echo "$((${end} - ${start})),${code},${mesg}"
done
) &
PIDs[${#PIDs[@]}]=$!
sleep ${tspl}
done

function exit_() {
(
set -x
#kill "${PIDs[@]}"
)
}
trap exit_ EXIT

echo "Script PID is $$" >&2
echo "Child PIDs is ${PIDs[*]}" >&2
echo "exit via Ctrl+c" >&2
wait

0 comments on commit 32353d5

Please sign in to comment.