Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(xrpc): register variable rpc_time #7040

Merged
merged 7 commits into from
May 16, 2022

Conversation

tzssangglass
Copy link
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)

Copy link
Member

@spacewander spacewander left a comment

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 = {}

Copy link
Member

@spacewander spacewander left a comment

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

@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