Commit ec7cff2
Yury Kirsanov
rest_client: optional response cache
Adds an opt-in cache for the HTTP responses this module fetches. When
cachedb_url is set, repeated queries are served from a cachedb backend
instead of going back to the origin, in both the blocking and the
asynchronous flavour. Nothing changes unless it is configured.
The module had no caching of any kind, so every rest_get() was a fresh
round trip even when the same worker had fetched the same URL moments
earlier. For the usual OpenSIPS pattern - a per-call lookup against a
rating, routing or authorization API - that round trip is the most
expensive thing in the request path.
The module replicates nothing itself, so the backend URL alone decides
whether the cache is private to one instance or shared by a cluster.
Both are useful: an in-process backend gives the cheapest possible hit
and each instance warms its own cache, while a shared redis lets one
node's fetch serve the whole cluster and cuts the load on the origin, at
a network round trip per hit. The rule is not local versus remote, only
that the backend be cheaper than the call it replaces.
Freshness does not rely on the backend acting on the TTL it is handed.
cachedb_mongodb, for one, takes the expires argument of its set() and
ignores it, so an entry written there would never expire; each entry
therefore carries an absolute expiry of its own and a hit past it is
treated as a miss. That backend remains unsuitable regardless, since
nothing ever reclaims the space.
By default the origin decides. A response is stored only if its own
Cache-Control and Expires headers permit it, and only for as long as
they allow; no-store, no-cache, private, Set-Cookie and Vary are all
refused, as are non-200 replies and bodies over cache_max_body. Because
many internal APIs send no cache headers at all, cache_policy offers
heuristic (supply a lifetime only where the origin was silent) and force
(ignore origin directives), both rejected at startup without a positive
cache_ttl. rest_cache_ctl() overrides a single call instead, which is
usually what is wanted - loosening the policy globally reinterprets
every endpoint, including the ones that were already right.
The cache key is MD5(method, URL, appended header list). The headers
matter: rest_append_hf() queues a process-global list consumed by the
next request, so the same URL is routinely fetched with different
Authorization or tenant headers and legitimately returns different
bodies. A URL-only key would hand one subscriber's response to another
and would pass any single-credential test. Header names are lower-cased
and sorted so append order is irrelevant, and values are hashed rather
than stored, so a bearer token never reaches the backend's key space.
The key does not cover the request body, which is why only GET responses
are stored; rest_post() and rest_put() consult the cache and report
miss:method but never write to it.
Each function takes an optional trailing output variable reporting where
the answer came from - hit, miss, miss:<reason> or bypass - alongside
the rest_cache_hits/misses/stores/skipped statistics and a computed hit
rate. Both exist because a cache that stores nothing, because every
origin forbids it, is otherwise indistinguishable from a working one.
Where the core provides the zero-copy get_buf cachedb endpoint and the
configured backend advertises it, it is used automatically; otherwise
get() is. The dependency is soft, so this builds and works as is.1 parent b043fe2 commit ec7cff2
9 files changed
Lines changed: 2131 additions & 130 deletions
File tree
- modules/rest_client
- doc
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
| 83 | + | |
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
86 | 95 | | |
87 | 96 | | |
88 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
43 | 57 | | |
44 | 58 | | |
45 | 59 | | |
| |||
0 commit comments