textmachine/platform/docs/p8-review/axis2-auth/sse-outlives-absolute-ceiling.sh

28 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Shows that an OPEN /v0/books/{id}/events stream outlives the ABSOLUTE session ceiling.
# Start the daemon with e.g. TM_PLATFORM_SESSION_IDLE=5s TM_PLATFORM_SESSION_MAX_AGE=10s.
# Usage: sse-outlives-absolute-ceiling.sh <base-url> <db-name> [cookie-name] [watch-seconds]
set -u
B=${1:-http://127.0.0.1:8102}
DB=${2:-tmp8rev_a2_dev}
N=${3:-__Host-tm_session}
W=${4:-45}
PSQL=${PSQL:-$HOME/.local/pgsql/bin/psql}
export PGHOST=/tmp PGPORT=55433 PGUSER=postgres
BOOK=$("$PSQL" -d "$DB" -Atc "select id from books limit 1")
"$PSQL" -d "$DB" -Atc "update books set status='parsing' where id='$BOOK'" >/dev/null
TOK=$(curl -s --noproxy '*' -i -X POST "$B/auth/dev-login" | sed -n "s/^Set-Cookie: ${N}=\([^;]*\).*/\1/p")
"$PSQL" -d "$DB" -Atc "select 'row clocks: idle='||idle_expires_at||' absolute='||absolute_expires_at||' now='||now() from sessions where revoked_at is null order by created_at desc limit 1"
OUT=$(mktemp)
( curl -s --noproxy '*' -N --max-time "$W" -H "Cookie: ${N}=${TOK}" "$B/v0/books/$BOOK/events" \
| while IFS= read -r l; do echo "+$(( $(date +%s) - T0 ))s $l"; done > "$OUT" ) &
T0=$(date +%s); export T0
PUMP=$!
for i in 5 10 15 20 30 40; do
while [ $(( $(date +%s) - T0 )) -lt "$i" ]; do sleep 1; done
echo "+${i}s a NEW request with the same cookie -> $(curl -s --noproxy '*' -o /dev/null -w '%{http_code}' -H "Cookie: ${N}=${TOK}" "$B/v0/books")"
done
wait $PUMP
echo "--- transcript of the connection opened at +0s ---"
awk -v t0="$T0" '{print}' "$OUT"
rm -f "$OUT"