<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 變量的共享范圍 > 本章內容來自openresty討論組 [這里](https://groups.google.com/forum/#!topic/openresty/3ylMdtvUJqg) 先看兩段代碼: ~~~ -- index.lua local uri_args = ngx.req.get_uri_args() local mo = require('mo') mo.args = uri_args ~~~ ~~~ -- mo.lua local showJs = function(callback, data) local cjson = require('cjson') ngx.say(callback .. '(' .. cjson.encode(data) .. ')') end local self.jsonp = self.args.jsonp local keyList = string.split(self.args.key_list, ',') for i=1, #keyList do -- do something ngx.say(self.args.kind) end showJs(self.jsonp, valList) ~~~ 大概代碼邏輯如上,然后出現這種情況: 生產服務器中,如果沒有用戶訪問,自己幾個人測試,一切正常。 同樣生產服務器,我將大量的用戶請求接入后,我不停刷新頁面的時候會出現部分情況(概率也不低,幾分之一,大于10%),輸出的callback(也就是來源于self.jsonp,即URL參數中的jsonp變量)和url地址中不一致(我自己測試的值是?jsonp=jsonp1435220570933,而用戶的請求基本上都是?jsonp=jquery....)錯誤的情況都是會出現用戶請求才會有的jquery....這種字符串。另外URL參數中的kind是1,我在循環中輸出會有“1”或“nil”的情況。不僅這兩種參數,幾乎所有url中傳遞的參數,都有可能變成其他請求鏈接中的參數。 基于以上情況,個人判斷會不會是在生產服務器大量用戶請求中,不同請求參數串掉了,但是如果這樣,是否應該會出現我本次的獲取參數是某個其他用戶的值,那么for循環中的值也應該固定的,而不會是一會兒是我自己請求中的參數值,一會兒是其他用戶請求中的參數值。 ### 問題在哪里? Lua module 是 VM 級別共享的,見[這里](https://github.com/openresty/lua-nginx-module#data-sharing-within-an-nginx-worker)。 self.jsonp變量一不留神全局共享了,而這肯定不是作者期望的。所以導致了高并發應用場景下偶爾出現異常錯誤的情況。 每請求的數據在傳遞和存儲時須特別小心,只應通過你自己的函數參數來傳遞,或者通過 ngx.ctx 表。前者是推薦的玩法,因為效率高得多。 貼一個ngx.ctx的例子: ~~~ location /test { rewrite_by_lua ' ngx.ctx.foo = 76 '; access_by_lua ' ngx.ctx.foo = ngx.ctx.foo + 3 '; content_by_lua_block { ngx.say(ngx.ctx.foo) } } ~~~ Then GET /test will yield the output ### NGX_LUA的三種變量范圍 ##### 進程間 所有Nginx的工作進程共享變量,使用指令lua_shared_dict定義 ##### 進程內 Lua源碼中聲明為全局變量,就是聲明變量的時候不使用local關鍵字,這樣的變量在同一個進程內的所有請求都是共享的 ##### 每請求 Lua源碼中聲明變量的時候使用local關鍵字,和ngx.ctx類似,變量的生命周期只存在同一個請求中 關于進程的變量,有兩個前提條件,一是ngx_lua使用LuaJIT編譯,二是聲明全局變量的模塊是require引用。LuaJIT會緩存模塊中的全局變量,下面用一個例子來說明這個問題。 nginx.conf ~~~ location /index { content_by_lua_file conf/lua/web/index.lua; } ~~~ index.lua ~~~ local ngx = require "ngx" local var = require "var" if var.calc() == 100 then ngx.say("ok") else ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR ngx.say("error") end ~~~ var.lua ~~~ local ngx = require "ngx" count = 100 local _M = {} local function add() count = count + 1 end local function sub() ngx.update_time() ngx.sleep(ngx.time()%0.003) --模擬后端阻塞時間 count = count - 1 end function _M.calc() add() sub() return count end return _M ~~~ 測試結果 ~~~ ? web git:(master) ab -c 1 -n 10 http://127.0.0.1:/index ... HTML transferred: 30 bytes ... ? web git:(master) ab -c 3 -n 10 http://127.0.0.1:10982/index ... HTML transferred: 48 bytes ... ~~~ 并發請求等于1的時候,返回的html文件的大小為3*10bytes,并發等于3的時候,返回的html文件的大小為48bytes,說明30次請求中有多次請求失敗,返回了“error”。這個例子可以說明,如果在模塊中使用了全局變量,在高并發的情況下可能發生不可知的結果。 建議不要使用模塊中的全局變量,最好使用ngx.ctx或share dict替代。如果由于業務需求,非要使用的話,建議該變量的值在也是在一個有限集合內,比方說只有ture和false兩個狀態。 > 79
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看