Add rate-limit module: Bucket4j 8.21 token buckets on Redis in a Spring Boot 4.1 filter, per-user/per-key/global limits, 429 with Retry-After, two instances sharing one bucket
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7rnC7jppGGdHGESvy
This commit is contained in:
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
# Two real application JVMs on ports 8081 and 8082, one real redis-server on 6411, driven with curl.
|
||||
# Writes output/08-two-jvms.txt. Needs a JDK 25, Maven 3.9 and redis-server on the PATH.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
OUT=output/08-two-jvms.txt
|
||||
mkdir -p output
|
||||
mvn -B -q -DskipTests package
|
||||
JAR=$(ls target/rate-limit-*.jar | grep -v original | head -1)
|
||||
|
||||
redis-server --port 6411 --save "" --appendonly no --bind 127.0.0.1 >/dev/null 2>&1 &
|
||||
REDIS_PID=$!
|
||||
java -Xmx256m -jar "$JAR" --server.port=8081 --instance.name=A --spring.data.redis.port=6411 \
|
||||
--logging.level.root=WARN --spring.main.banner-mode=off >/tmp/rate-limit-A.log 2>&1 &
|
||||
A_PID=$!
|
||||
java -Xmx256m -jar "$JAR" --server.port=8082 --instance.name=B --spring.data.redis.port=6411 \
|
||||
--logging.level.root=WARN --spring.main.banner-mode=off >/tmp/rate-limit-B.log 2>&1 &
|
||||
B_PID=$!
|
||||
trap 'kill $A_PID $B_PID $REDIS_PID 2>/dev/null || true' EXIT
|
||||
|
||||
for port in 8081 8082; do
|
||||
for _ in $(seq 1 60); do
|
||||
curl -s -o /dev/null "http://localhost:$port/health" && break
|
||||
sleep 1
|
||||
done
|
||||
done
|
||||
|
||||
{
|
||||
echo "== two JVMs (A on 8081, B on 8082), one redis-server on 6411; free-key-123 allows 5 per minute"
|
||||
for i in 1 2 3 4 5 6; do
|
||||
if (( i % 2 )); then port=8081; else port=8082; fi
|
||||
printf 'request %d to :%s -> ' "$i" "$port"
|
||||
curl -s -o /dev/null -w '%{http_code}' -H 'X-API-Key: free-key-123' "http://localhost:$port/api/hello"
|
||||
echo
|
||||
done
|
||||
echo
|
||||
echo "$ curl -si -H 'X-API-Key: free-key-123' http://localhost:8081/api/hello"
|
||||
curl -si -H 'X-API-Key: free-key-123' http://localhost:8081/api/hello | tr -d '\r' | grep -v -E '^(Date|Connection|Keep-Alive|Content-Length):'
|
||||
echo
|
||||
echo
|
||||
echo "$ redis-cli -p 6411 --scan"
|
||||
redis-cli -p 6411 --scan | sort
|
||||
} | tee "$OUT"
|
||||
Reference in New Issue
Block a user