Add gateway-mvc: Spring Cloud Gateway Server MVC 5.1 on Boot 4.2.0 + virtual threads, retry, Redis rate limiting

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3W3uHbHAmM3orhn7b
This commit is contained in:
asmhatre
2026-11-26 00:55:16 +05:30
co-authored by Claude Sonnet 5.5
parent 7211b0fb80
commit 32086c1f5b
19 changed files with 821 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
$ curl -i localhost:8080/api/hello
HTTP/1.1 200
X-Gateway-Thread: virtual
Content-Type: application/json
{"service":"upstream","message":"hello from the backend"}
$ curl -i localhost:8080/yaml/hello
HTTP/1.1 200
Content-Type: application/json
{"service":"upstream","message":"hello from the backend"}
$ curl -i localhost:8080/diag/thread
HTTP/1.1 200
Content-Type: application/json
{"virtual":true,"thread":"VirtualThread[#58,tomcat-handler-5]/runnable@ForkJoinPool-1-worker-1"}
# what the backend receives by default; note a client-supplied X-Forwarded-For is stripped
$ curl -s -H 'X-Forwarded-For: 1.2.3.4' localhost:8080/api/headers
accept: */*
accept-encoding: gzip,deflate
connection: Upgrade, HTTP2-Settings
host: localhost:9091
http2-settings: AAEAAEAAAAIAAAAAAAMAAAAAAAQBAAAAAAUAAEAAAAYABgAA
upgrade: h2c
user-agent: curl/8.5.0
x-gateway-route: headers
# same request with spring.cloud.gateway.server.webmvc.trusted-proxies=.*
$ curl -s -H 'X-Forwarded-For: 1.2.3.4' localhost:8080/api/headers
forwarded: proto=http;host="localhost:8080";for="127.0.0.1:41308"
x-forwarded-for: 1.2.3.4,127.0.0.1
x-forwarded-host: localhost:8080
x-forwarded-port: 8080
x-forwarded-prefix: /api
x-forwarded-proto: http
x-gateway-route: headers
+44
View File
@@ -0,0 +1,44 @@
# the backend fails its first 2 calls with 503; the gateway route retries up to 3 times
$ curl -i localhost:9091/flaky/reset
HTTP/1.1 200 OK
Content-type: application/json
{"reset":true}
$ curl -i localhost:8080/api/flaky?failFirst=2
HTTP/1.1 200
Content-Type: application/json
{"call":3,"result":"ok"}
# how many calls did the backend actually see? (failFirst=0 just returns the counter)
$ curl -i localhost:9091/flaky?failFirst=0
HTTP/1.1 200 OK
Content-type: application/json
{"call":4,"result":"ok"}
# now a backend that never recovers: after the 3 retries the gateway gives up with a 500, not the backend's 503
$ curl -i localhost:9091/flaky/reset
HTTP/1.1 200 OK
Content-type: application/json
{"reset":true}
$ curl -i localhost:8080/api/flaky?failFirst=99
HTTP/1.1 500
Content-Type: application/json
{"timestamp":"2026-11-25T19:22:37.164Z","status":500,"error":"Internal Server Error","path":"/api/flaky"}
# the backend saw the original try plus 3 retries = 4 calls, so the counter now reads 5
$ curl -i localhost:9091/flaky?failFirst=99
HTTP/1.1 503 Service Unavailable
Content-type: application/json
{"call":5,"result":"unavailable"}
# log line behind that 500:
Request processing failed: org.springframework.core.retry.RetryException: Retry policy for operation ... (FrameworkRetryFilterFunctions lambda)
# which retry implementation did the gateway pick? (DEBUG log at startup)
Retry filter selection: Spring Retry on classpath=false, useFrameworkRetry=false, selected filter=FrameworkRetryFilterFunctions
+17
View File
@@ -0,0 +1,17 @@
# 5 tokens per 10 seconds per X-Api-Key, Caffeine store
alice #1 HTTP/1.1 200 X-RateLimit-Remaining: 4
alice #2 HTTP/1.1 200 X-RateLimit-Remaining: 3
alice #3 HTTP/1.1 200 X-RateLimit-Remaining: 2
alice #4 HTTP/1.1 200 X-RateLimit-Remaining: 1
alice #5 HTTP/1.1 200 X-RateLimit-Remaining: 0
alice #6 HTTP/1.1 429 X-RateLimit-Remaining: 0
alice #7 HTTP/1.1 429 X-RateLimit-Remaining: 0
# a different key has its own bucket
bob #1 HTTP/1.1 200 X-RateLimit-Remaining: 4
# no X-Api-Key: the key resolver returns null
$ curl -i localhost:8080/api/limited
HTTP/1.1 403
# wait 11 seconds: the bucket refills
alice #8 HTTP/1.1 200 X-RateLimit-Remaining: 4
+12
View File
@@ -0,0 +1,12 @@
# same key, alternating between two gateway instances that share Redis
carol #1 via :8081 HTTP/1.1 200 X-RateLimit-Remaining: 4
carol #2 via :8080 HTTP/1.1 200 X-RateLimit-Remaining: 3
carol #3 via :8081 HTTP/1.1 200 X-RateLimit-Remaining: 2
carol #4 via :8080 HTTP/1.1 200 X-RateLimit-Remaining: 1
carol #5 via :8081 HTTP/1.1 200 X-RateLimit-Remaining: 0
carol #6 via :8080 HTTP/1.1 429 X-RateLimit-Remaining: 0
# what Redis holds
$ redis-cli --scan
carol
$ redis-cli pttl carol (time to live in ms; the bucket expires once it would be full again)
positive TTL, as expected
+38
View File
@@ -0,0 +1,38 @@
## A. read timeout: spring.http.clients.read-timeout=1s, backend sleeps 3s
$ curl -i localhost:8080/api/slow?ms=3000
HTTP/1.1 500
Content-Type: application/json
{"timestamp":"2026-11-25T19:23:31.069Z","status":500,"error":"Internal Server Error","path":"/api/slow"}
# log line behind that 500:
Request processing failed: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:9091/slow"
## B. same timeout, route with onError(ResourceAccessException) mapped to 504
$ curl -i localhost:8080/api/slow-mapped?ms=3000
HTTP/1.1 504
Content-Type: application/json
{"error":"upstream did not answer in time"}
## C. Redis stops while the gateway is up; no timeout configured on rateLimit()
$ curl --max-time 5 -H 'X-Api-Key: dave' localhost:8080/api/limited
curl exit after 5.001396s, http_code=000
## D. same outage, with demo.rl-timeout=300ms (setTimeout on the filter)
$ curl -H 'X-Api-Key: dave' localhost:8080/api/limited
http_code=500 time_total_under_1s=yes
# log line behind that 500:
Request processing failed: java.util.concurrent.TimeoutException
# Redis back: the next request works again, no restart needed
http_code=200
## E. no AsyncProxyManager bean at all (--demo.store=none)
$ curl -i -H 'X-Api-Key: erin' localhost:8080/api/limited
HTTP/1.1 500
Content-Type: application/json
{"timestamp":"2026-11-25T19:24:09.127Z","status":500,"error":"Internal Server Error","path":"/api/limited"}
# log line behind that 500:
Request processing failed: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.github.bucket4j.distributed.proxy.AsyncProxyManager<?>' available
+20
View File
@@ -0,0 +1,20 @@
## virtual threads (server.tomcat.threads.max=10)
$ curl -i localhost:8080/diag/thread
HTTP/1.1 200
Content-Type: application/json
{"thread":"VirtualThread[#N,tomcat-handler-1]/runnable@ForkJoinPool-1-worker-1","virtual":true}
$ java scripts/LoadClient.java 'http://localhost:8080/api/slow?ms=500' 200
requests=200 statuses={200=200} elapsed_ms=2154
## platform threads (server.tomcat.threads.max=10)
$ curl -i localhost:8080/diag/thread
HTTP/1.1 200
Content-Type: application/json
{"virtual":false,"thread":"Thread[#N,http-nio-8080-exec-3,5,main]"}
$ java scripts/LoadClient.java 'http://localhost:8080/api/slow?ms=500' 200
requests=200 statuses={200=200} elapsed_ms=10903