Commit 57bc1c8
committed
Add per-issuer lock and negative caching to prevent thundering herd
This PR addresses the thundering herd problem when multiple threads
simultaneously try to validate tokens from the same issuer:
Per-issuer locking:
- Added a per-issuer mutex map with shared_ptr ownership
- Threads acquire a lock for an issuer before fetching keys from web
- Other threads wait on the lock, then find keys in cache
- Lock ownership transfers through async status for proper lifecycle
- Limited to 1000 cached mutexes to prevent resource exhaustion
Negative caching:
- On web fetch failure (e.g., 404), store empty keys in cache
- Uses same TTL as successful lookups (get_next_update_delta)
- Subsequent lookups hit cache and fail fast without web requests
- Prevents repeated web requests for known-bad issuers
SQLite busy timeout:
- Added 5-second busy timeout to handle concurrent DB access
- Applied to all database operations (init, read, write)
Stress tests:
- StressTestValidToken: 10 threads, 5 seconds, valid token
- StressTestInvalidIssuer: 10 threads, 5 seconds, 404 issuer
- ConcurrentNewIssuerLookup: Verifies only ONE web fetch occurs
Verified behavior:
- Valid issuer: ONE key lookup for thousands of validations
- Invalid issuer: ONE web request (OIDC + OAuth fallback), then cached1 parent ae3fc37 commit 57bc1c8
File tree
4 files changed
+597
-47
lines changed- src
- test
4 files changed
+597
-47
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| |||
27 | 31 | | |
28 | 32 | | |
29 | 33 | | |
| 34 | + | |
| 35 | + | |
30 | 36 | | |
31 | 37 | | |
32 | 38 | | |
| |||
161 | 167 | | |
162 | 168 | | |
163 | 169 | | |
| 170 | + | |
| 171 | + | |
164 | 172 | | |
165 | 173 | | |
166 | 174 | | |
| |||
260 | 268 | | |
261 | 269 | | |
262 | 270 | | |
| 271 | + | |
| 272 | + | |
263 | 273 | | |
264 | 274 | | |
265 | 275 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
904 | 904 | | |
905 | 905 | | |
906 | 906 | | |
907 | | - | |
| 907 | + | |
908 | 908 | | |
909 | 909 | | |
910 | | - | |
| 910 | + | |
911 | 911 | | |
912 | 912 | | |
913 | 913 | | |
914 | 914 | | |
915 | 915 | | |
916 | 916 | | |
| 917 | + | |
917 | 918 | | |
918 | 919 | | |
919 | 920 | | |
920 | 921 | | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
921 | 928 | | |
922 | 929 | | |
923 | 930 | | |
| |||
934 | 941 | | |
935 | 942 | | |
936 | 943 | | |
937 | | - | |
938 | | - | |
939 | | - | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
940 | 978 | | |
941 | 979 | | |
942 | 980 | | |
943 | 981 | | |
944 | | - | |
945 | | - | |
946 | | - | |
947 | | - | |
948 | | - | |
949 | | - | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
950 | 987 | | |
951 | 988 | | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
952 | 994 | | |
953 | 995 | | |
954 | 996 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
445 | 445 | | |
446 | 446 | | |
447 | 447 | | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
448 | 452 | | |
449 | 453 | | |
450 | 454 | | |
| |||
666 | 670 | | |
667 | 671 | | |
668 | 672 | | |
| 673 | + | |
| 674 | + | |
669 | 675 | | |
670 | 676 | | |
671 | 677 | | |
| |||
724 | 730 | | |
725 | 731 | | |
726 | 732 | | |
727 | | - | |
| 733 | + | |
| 734 | + | |
728 | 735 | | |
729 | 736 | | |
730 | 737 | | |
| |||
771 | 778 | | |
772 | 779 | | |
773 | 780 | | |
| 781 | + | |
| 782 | + | |
774 | 783 | | |
775 | 784 | | |
776 | 785 | | |
| |||
782 | 791 | | |
783 | 792 | | |
784 | 793 | | |
785 | | - | |
| 794 | + | |
| 795 | + | |
786 | 796 | | |
787 | 797 | | |
788 | 798 | | |
| |||
886 | 896 | | |
887 | 897 | | |
888 | 898 | | |
| 899 | + | |
889 | 900 | | |
890 | 901 | | |
891 | 902 | | |
| |||
1063 | 1074 | | |
1064 | 1075 | | |
1065 | 1076 | | |
1066 | | - | |
1067 | | - | |
1068 | | - | |
| 1077 | + | |
| 1078 | + | |
1069 | 1079 | | |
1070 | 1080 | | |
1071 | 1081 | | |
| |||
1077 | 1087 | | |
1078 | 1088 | | |
1079 | 1089 | | |
| 1090 | + | |
1080 | 1091 | | |
1081 | 1092 | | |
| 1093 | + | |
| 1094 | + | |
1082 | 1095 | | |
1083 | 1096 | | |
1084 | 1097 | | |
| |||
0 commit comments