bzp2010 commented on code in PR #13904:
URL: https://github.com/apache/apisix/pull/13904#discussion_r3910489779


##########
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:
   This was intentional. I can't have a `core` module have a dependency on 
certain code under `admin/`. A functional module should depend on the core's 
std lib, not the other way around.
   
   Maybe it really isn't the right place for it; I'll think about it some more.



-- 
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]

Reply via email to