Yichun Zhang (agentzh) wrote on Tuesday, October 13, 2015 11:07 AM
>
> On Tue, Oct 13, 2015 at 12:29 AM, Nelson, Erik - 2 wrote:
> > lua-resty-lock has a good example of updating a cache from a
> > synchronous service like redis, something like
> >
> [...]
> >
> > I have a slightly different twist on this problem- when I need a
> cache
> > update, the light thread can only initiate the cache update process,
> > and the updated information will be returned asynchronously through a
> > different HTTP call. My current solution is polling, something like
> > this pseudocode, adapted from the lua-resty-lock example starting at
> > fetch_redis()
> >
>
> Sorry, I don't understand. If your main light thread *has to* wait for
> the result of the asynchronous operation, why not just do a synchronous
> operation? Is that asynchronous operation runs outside the current
> context? Like in another request handler or per-worker timer thread?
Yes, exactly- an external program actually needs to do the operation, and calls another request handler when it's done.
>
> In the context of OpenResty, synchronous operations are doing
> nonblocking operations per se as long as you're stick with OpenResty's
> APIs. Thus you are safe with I/O performance even with synchronous
> paradigm (actually that's one of the selling points of OpenResty).
>
> > begin_async_update()
> >
> > while true do
> > -- check to see if async process called in to location
> '/update_cache?value=new_cache_value', possibly in a different worker
> > if async_update_happened() then break end
> > ngx.sleep(1)
> > end
> >
>
> I wonder how you check if an update has happened? Polling a remote
> endpoint via something like HTTP anyway?
>
I'm checking the value in a lua_shared_dict that is only updated by the /update_cache request handler.
> > begin_async_update()
> > local result = condition_var.wait(timeout) if result then --wait was
> > successful, new value is available ...
> > end
>
> I wonder who will be responsible to update the condition_var above for
> you. That's why I ask how you check for updates.
>
In this case, the /update_cache handler would update the condition_var, similar to the way that it updates the lua_shared_dict currently. The main advantage of the condition_var is that the waiting thread can run immediately when the condition_var is triggered.
> FWIW, there will be a new semaphore API added soon to ngx_lua, which
> allows efficient synchronizations between light threads running in the
> same nginx worker process (not necessarily be in the same request
> context though). Not sure if you want something like that. My gut
> feeling is that you can just do nonblocking synchronous update anyway
> if that is possible.
I think that won't help with this particular problem but would still be quite a useful tool, as it sometimes could be useful to allow light threads to have more insight into each others' operations.
----------------------------------------------------------------------
This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer. If you are not the intended recipient, please delete this message.