openresty / openresty/lua-nginx-module

Libuv event loop

Open
#2,396 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

I'm wondering if there is a way to use libuv in the background. I've tried the following in init_worker_by_file, and it appears that while the timers fire and the thread is started, subsequent requests never get control:

local uv = require('luv')
local ngx = require('ngx')
print("Initializing worker (hopefully)")
local function my_loop_init_timer(premature)
  -- This timer is run in init context and (hopefully) will let us start a
  -- light thread in the worker context
  local function my_loop_thread()
    -- this hopefully persists for the lifetime of the worker
    print("UV thread executing, uv FD is: " .. uv.backend_fd())
    while ngx.worker.exiting() == false do
      -- I know this is busy waiting, but I don't know another way to tell lua-nginx to poll the fd
      while uv.run("nowait") do
        coroutine.yield()
      end
      coroutine.yield()
      uv.sleep(1)
    end
  end
  ngx.thread.spawn(my_loop_thread)
end
print("Main interpreter is running, uv FD is: " .. uv.backend_fd())

ngx.timer.at(0, my_loop_init_timer)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the init_worker_by_file sequence shown in the issue, especially ngx.timer.at, ngx.thread.spawn, and luv.run. Determine whether libuv can be polled in the background without blocking subsequent requests; done means UV timers continue running while worker requests retain control.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.