@@ -148,32 +148,46 @@ function _M:communicate(premature)
148
148
149
149
local ping_immediately
150
150
local config_exit
151
+ local next_data
151
152
152
153
local config_thread = ngx .thread .spawn (function ()
153
154
while not exiting () and not config_exit do
154
155
local ok , err = config_semaphore :wait (1 )
155
156
if ok then
156
- local config_table = self .next_config
157
- if config_table then
158
- local config_hash = self .next_hash
159
- local hashes = self .next_hashes
160
-
161
- local pok , res
162
- pok , res , err = pcall (config_helper .update ,
163
- self .declarative_config , config_table , config_hash , hashes )
164
- if pok then
165
- if not res then
166
- ngx_log (ngx_ERR , _log_prefix , " unable to update running config: " , err )
157
+ local data = next_data
158
+ if data then
159
+ local msg = assert (inflate_gzip (data ))
160
+ yield ()
161
+ msg = assert (cjson_decode (msg ))
162
+ yield ()
163
+
164
+ if msg .type == " reconfigure" then
165
+ if msg .timestamp then
166
+ ngx_log (ngx_DEBUG , _log_prefix , " received reconfigure frame from control plane with timestamp: " ,
167
+ msg .timestamp , log_suffix )
168
+
169
+ else
170
+ ngx_log (ngx_DEBUG , _log_prefix , " received reconfigure frame from control plane" , log_suffix )
167
171
end
168
172
169
- ping_immediately = true
173
+ local config_table = assert (msg .config_table )
174
+ local pok , res
175
+ pok , res , err = pcall (config_helper .update , self .declarative_config ,
176
+ config_table , msg .config_hash , msg .hashes )
177
+ if pok then
178
+ if not res then
179
+ ngx_log (ngx_ERR , _log_prefix , " unable to update running config: " , err )
180
+ end
170
181
171
- else
172
- ngx_log (ngx_ERR , _log_prefix , " unable to update running config: " , res )
173
- end
182
+ ping_immediately = true
183
+
184
+ else
185
+ ngx_log (ngx_ERR , _log_prefix , " unable to update running config: " , res )
186
+ end
174
187
175
- if self .next_config == config_table then
176
- self .next_config = nil
188
+ if next_data == data then
189
+ next_data = nil
190
+ end
177
191
end
178
192
end
179
193
@@ -223,31 +237,12 @@ function _M:communicate(premature)
223
237
last_seen = ngx_time ()
224
238
225
239
if typ == " binary" then
226
- data = assert (inflate_gzip (data ))
227
- yield ()
228
-
229
- local msg = assert (cjson_decode (data ))
230
- yield ()
231
-
232
- if msg .type == " reconfigure" then
233
- if msg .timestamp then
234
- ngx_log (ngx_DEBUG , _log_prefix , " received reconfigure frame from control plane with timestamp: " ,
235
- msg .timestamp , log_suffix )
236
-
237
- else
238
- ngx_log (ngx_DEBUG , _log_prefix , " received reconfigure frame from control plane" , log_suffix )
239
- end
240
-
241
- self .next_config = assert (msg .config_table )
242
- self .next_hash = msg .config_hash
243
- self .next_hashes = msg .hashes
244
-
245
- if config_semaphore :count () <= 0 then
246
- -- the following line always executes immediately after the `if` check
247
- -- because `:count` will never yield, end result is that the semaphore
248
- -- count is guaranteed to not exceed 1
249
- config_semaphore :post ()
250
- end
240
+ next_data = data
241
+ if config_semaphore :count () <= 0 then
242
+ -- the following line always executes immediately after the `if` check
243
+ -- because `:count` will never yield, end result is that the semaphore
244
+ -- count is guaranteed to not exceed 1
245
+ config_semaphore :post ()
251
246
end
252
247
253
248
elseif typ == " pong" then
@@ -277,8 +272,8 @@ function _M:communicate(premature)
277
272
-- the config thread might be holding a lock if it's in the middle of an
278
273
-- update, so we need to give it a chance to terminate gracefully
279
274
config_exit = true
280
- ok , err , perr = ngx .thread .wait (config_thread )
281
275
276
+ ok , err , perr = ngx .thread .wait (config_thread )
282
277
if not ok then
283
278
ngx_log (ngx_ERR , _log_prefix , err , log_suffix )
284
279
0 commit comments