nic-6443 commented on code in PR #13904:
URL: https://github.com/apache/apisix/pull/13904#discussion_r3910430846
##########
apisix/core/config_yaml.lua:
##########
@@ -62,6 +69,37 @@ local _M = {
"cannot be restored from other workers and shared dict"
}
+-- the "config" key in the standalone-config shared dict stores
+-- "<digest length>\n<digest><json body>" instead of plain JSON, so a worker
+-- can compare digests without decoding the (potentially large) JSON on
+-- every poll
+-- the reason for combining them into a single string and writing them to
+-- only one key is to ensure concurrent safety for updates by multiple
+-- workers
+local CONFIG_DIGEST_LENGTH_SEPARATOR = "\n"
+
+function _M.encode_config(digest, raw)
+ digest = digest or ""
+ return #digest .. CONFIG_DIGEST_LENGTH_SEPARATOR .. digest .. raw
+end
+
+
+function _M.decode_config(stored)
+ local idx = find_str(stored, CONFIG_DIGEST_LENGTH_SEPARATOR, 1, true)
+ if not idx then
+ return nil, nil, "missing digest length prefix"
+ end
+
+ local digest_len = tonumber(sub_str(stored, 1, idx - 1))
+ if not digest_len then
+ return nil, nil, "invalid digest length prefix"
+ end
+
+ local digest_start = idx + 1
+ local digest_end = digest_start + digest_len - 1
+ return sub_str(stored, digest_start, digest_end), sub_str(stored,
digest_end + 1)
+end
Review Comment:
I feel it's somewhat inappropriate to put this piece of code in
`config_yaml.lua`. When I saw `config_yaml.decode_config` and
`config_yaml.encode_config` at the call site, I thought they were performing
YAML decode and encode operations, but they actually handle a custom data
structure.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]