Commit 1bc1dca
committed
change(core): keep injecting the X-Forwarded-* headers, append instead of replace
Review asked for the request-header visibility to be preserved rather than
traded away. It can be, and most of the speedup survives.
The fast path no longer returns without writing the headers. Route `vars`,
`ngx.req.get_headers()`, and the exits that copy `r->headers_in` instead of
reading `$var_x_forwarded_*` -- `@grpc_pass`, `@dubbo_pass`, the mirror
subrequest -- all observe them, and skipping the write changed what every one of
those saw. On a default install that is not an edge case: with no
`trusted_addresses` configured every peer is untrusted, so the fast path is the
common path.
What is still worth skipping is the cost of *replacing* a header. `set_header`
walks the header list to find the old entry and remove it before inserting; on
this path the entry is known absent, since that is the condition for being here,
so `add_header` is enough. The rest of the old slow path is a no-op on this
input anyway -- every `original_x_forwarded_*` value is nil, and clearing
`Forwarded` or `X-Forwarded-For` has nothing to clear -- so it stays skipped.
Eight rounds, alternating, single worker, wrk2 at saturation, worker on one
P-core and the load generator on two others (the earlier number in this PR was
taken with the worker and the load generator on hyperthread siblings and is not
comparable):
| variant | mean | range |
|---------------------------|--------|---------------|
| master | 79,345 | 77,772-80,806 |
| this commit | 86,145 | 84,368-87,417 |
| previous commit (skipped) | 89,726 | 87,945-91,423 |
+8.6% instead of +13.1%, with the three ranges disjoint across all eight rounds.
The only behaviour left that differs from master is the one that was always a
fix: a plugin-set X-Forwarded-* now reaches the upstream, because
`set_upstream_x_forwarded_headers` reads `ngx.var` instead of the `ctx.var`
entry that a stale invalidation left behind. `core.request.set_header`
invalidates `ctx.var` under the hyphenated header name while `core/ctx.lua`
caches the underscored one; that cause deserves its own fix.
`t/plugin/proxy-mirror4.t` TEST 1 pinned the behaviour being given up here and
now asserts the values master produces. `t/core/trusted-addresses.t` TEST 17 and
TEST 18 pin the two visibility consumers that have no other coverage: a plugin
reading the whole header table, and a route matching on `http_x_forwarded_proto`.1 parent 990aef5 commit 1bc1dca
3 files changed
Lines changed: 96 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
706 | 706 | | |
707 | 707 | | |
708 | 708 | | |
709 | | - | |
710 | | - | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
716 | | - | |
717 | | - | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
718 | 721 | | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
729 | | - | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
730 | 735 | | |
731 | 736 | | |
732 | 737 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
641 | 641 | | |
642 | 642 | | |
643 | 643 | | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| |||
0 commit comments