Skip to content

feat(xrpc): register variable rpc_time#7040

Merged
spacewander merged 7 commits into
apache:masterfrom
tzssangglass:rpc_time
May 16, 2022
Merged

feat(xrpc): register variable rpc_time#7040
spacewander merged 7 commits into
apache:masterfrom
tzssangglass:rpc_time

Conversation

@tzssangglass

Copy link
Copy Markdown
Member

Description

Fixes # (issue)

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@spacewander spacewander left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can apply the patch:

index df6e430c..5bf3daa5 100644
--- apisix/core/ctx.lua
+++ apisix/core/ctx.lua
@@ -278,11 +278,12 @@ do
             else
                 local getter = apisix_var_names[key]
                 if getter then
+                    local ctx = t._ctx
                     if getter == true then
-                        val = ngx.ctx.api_ctx and ngx.ctx.api_ctx[key]
+                        val = ctx and ctx[key]
                     else
                         -- the getter is registered by ctx.register_var
-                        val = getter(ngx.ctx.api_ctx)
+                        val = getter(ctx)
                     end

                 else
@@ -341,6 +342,7 @@ function _M.set_vars_meta(ctx)
     end

     var._request = get_request()
+    var._ctx = ctx
     setmetatable(var, mt)
     ctx.var = var
 end
diff --git t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
index 28fa4abc..3ea0c7ea 100644
--- t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
+++ t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
@@ -30,12 +30,7 @@ local str_byte = string.byte


 core.ctx.register_var("rpc_len", function(ctx)
-    local curr_ctx = ctx.xrpc_session and ctx.xrpc_session._curr_ctx
-    if not curr_ctx then
-        core.log.warn("can't find current context")
-        return nil
-    end
-    return curr_ctx.len
+    return ctx.len
 end)

 local _M = {}

@spacewander spacewander left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can apply the patch:

index df6e430c..5bf3daa5 100644
--- apisix/core/ctx.lua
+++ apisix/core/ctx.lua
@@ -278,11 +278,12 @@ do
             else
                 local getter = apisix_var_names[key]
                 if getter then
+                    local ctx = t._ctx
                     if getter == true then
-                        val = ngx.ctx.api_ctx and ngx.ctx.api_ctx[key]
+                        val = ctx and ctx[key]
                     else
                         -- the getter is registered by ctx.register_var
-                        val = getter(ngx.ctx.api_ctx)
+                        val = getter(ctx)
                     end

                 else
@@ -341,6 +342,7 @@ function _M.set_vars_meta(ctx)
     end

     var._request = get_request()
+    var._ctx = ctx
     setmetatable(var, mt)
     ctx.var = var
 end
diff --git t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
index 28fa4abc..3ea0c7ea 100644
--- t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
+++ t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
@@ -30,12 +30,7 @@ local str_byte = string.byte


 core.ctx.register_var("rpc_len", function(ctx)
-    local curr_ctx = ctx.xrpc_session and ctx.xrpc_session._curr_ctx
-    if not curr_ctx then
-        core.log.warn("can't find current context")
-        return nil
-    end
-    return curr_ctx.len
+    return ctx.len
 end)

 local _M = {}

@tzssangglass

Copy link
Copy Markdown
Member Author

We can apply the patch:

index df6e430c..5bf3daa5 100644
--- apisix/core/ctx.lua
+++ apisix/core/ctx.lua
@@ -278,11 +278,12 @@ do
             else
                 local getter = apisix_var_names[key]
                 if getter then
+                    local ctx = t._ctx
                     if getter == true then
-                        val = ngx.ctx.api_ctx and ngx.ctx.api_ctx[key]
+                        val = ctx and ctx[key]
                     else
                         -- the getter is registered by ctx.register_var
-                        val = getter(ngx.ctx.api_ctx)
+                        val = getter(ctx)
                     end

                 else
@@ -341,6 +342,7 @@ function _M.set_vars_meta(ctx)
     end

     var._request = get_request()
+    var._ctx = ctx
     setmetatable(var, mt)
     ctx.var = var
 end
diff --git t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
index 28fa4abc..3ea0c7ea 100644
--- t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
+++ t/xrpc/apisix/stream/xrpc/protocols/pingpong/init.lua
@@ -30,12 +30,7 @@ local str_byte = string.byte


 core.ctx.register_var("rpc_len", function(ctx)
-    local curr_ctx = ctx.xrpc_session and ctx.xrpc_session._curr_ctx
-    if not curr_ctx then
-        core.log.warn("can't find current context")
-        return nil
-    end
-    return curr_ctx.len
+    return ctx.len
 end)

 local _M = {}

update, thanks for pointing out a better way.

Comment thread apisix/stream/xrpc/runner.lua Outdated
@tzssangglass
tzssangglass requested a review from spacewander May 16, 2022 02:03
@spacewander
spacewander merged commit 3c54cf5 into apache:master May 16, 2022
Liu-Junlin pushed a commit to Liu-Junlin/apisix that referenced this pull request May 20, 2022
@tzssangglass
tzssangglass deleted the rpc_time branch May 24, 2022 06:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants