perf: Implement 2-stage map routing for faster domain lookup

Split domain routing into two stages for improved performance:
- Stage 1: map_str for exact domains (O(log n) using ebtree)
- Stage 2: map_dom for wildcards only (O(n) but small set)

Wildcards now stored in separate wildcards.map file.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
kaffa
2026-02-03 11:44:54 +09:00
parent 46c86b62f2
commit 18ce812920
5 changed files with 92 additions and 19 deletions

View File

@@ -58,8 +58,11 @@ frontend http_front
# use_backend acme_backend if is_acme
# http-request redirect scheme https unless is_acme
# Map-based dynamic routing (same as HTTPS)
use_backend %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domains.map)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domains.map) -m found }
# 2-stage map-based routing for performance:
# Stage 1: Exact match with map_str (O(log n) - fast, uses ebtree)
use_backend %[req.hdr(host),lower,map_str(/usr/local/etc/haproxy/domains.map)] if { req.hdr(host),lower,map_str(/usr/local/etc/haproxy/domains.map) -m found }
# Stage 2: Wildcard fallback with map_dom (O(n) - slower, but only for wildcards)
use_backend %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/wildcards.map)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/wildcards.map) -m found }
default_backend default_backend
@@ -75,8 +78,11 @@ frontend https_front
acl is_tailscale src 100.64.0.0/10
http-request deny deny_status 401 if is_mcp !valid_token !is_tailscale
# Map-based dynamic routing (no reload needed for domain changes)
use_backend %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domains.map)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domains.map) -m found }
# 2-stage map-based routing for performance:
# Stage 1: Exact match with map_str (O(log n) - fast, uses ebtree)
use_backend %[req.hdr(host),lower,map_str(/usr/local/etc/haproxy/domains.map)] if { req.hdr(host),lower,map_str(/usr/local/etc/haproxy/domains.map) -m found }
# Stage 2: Wildcard fallback with map_dom (O(n) - slower, but only for wildcards)
use_backend %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/wildcards.map)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/wildcards.map) -m found }
default_backend default_backend

9
conf/wildcards.map Normal file
View File

@@ -0,0 +1,9 @@
# Wildcard Domain to Backend mapping (for map_dom)
# Format: .domain.com backend_name (matches *.domain.com)
# Uses map_dom for suffix matching
.actions.it.com pool_3
.anvil.it.com pool_4
.bench.inouter.com pool_5
.mcp.inouter.com pool_2
.nas.inouter.com pool_1