Skip to content

Conversation

mlboy
Copy link

@mlboy mlboy commented Aug 30, 2017

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

@agentzh
Copy link
Member

agentzh commented Aug 30, 2017

@mlboy Will you please explain your use cases for this API?

Copy link
Member

@agentzh agentzh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing fd directly may break the internal I/O scheduler for cosockets since it breaks the low level I/O encapsulation. So I wonder why you need this in the first place.

ngx_http_lua_socket_tcp_getsockfd(lua_State *L)
{
ngx_connection_t *c;
ngx_http_lua_socket_tcp_upstream_t *u;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: variable identifiers must be vertically aligned up. See other similar places for examples.

}

c = u->peer.connection;
lua_pushinteger(L,(int) c->fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a space after the comma character.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, need to test if the fd is -1 before returning it.

@@ -782,6 +786,29 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
return lua_yield(L, 0);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: we use 2 blank lines to separate successive C function definitions. See other similar places for examples.

lua_pushinteger(L,(int) c->fd);

return 1;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@@ -265,6 +266,9 @@ ngx_http_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
lua_pushcfunction(L, ngx_http_lua_socket_tcp_settimeouts);
lua_setfield(L, -2, "settimeouts"); /* ngx socket mt */

lua_pushcfunction(L, ngx_http_lua_socket_tcp_getsockfd);
lua_setfield(L, -2, "getsockfd"); /* ngx socket mt */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sock word in the method name is redundant. It cannot really be something else. It can only be called on cosocket objects anyway. Or are you following LuaSocket's Lua API here? (I couldn't find such an API in LuaSocket).

@mlboy
Copy link
Author

mlboy commented Aug 31, 2017

step 2 i will add a method like sock:send_fd(fd,...)
and then can send a socket to fd which is you want
case :one for one chatroom

@agentzh
Copy link
Member

agentzh commented Aug 31, 2017

@mlboy Sending fds across process boundary can be tricky in that you would also have to pass all the associated state over and rebuild all the data structures in the other process. All this work may incur significant overhead and is very tricky to get exactly right given the complexity of the problem.

For your use case, I think it's better to just pass "control flow" and "data" across the process boundary instead of passing fds and cosockets. We already have production users who use ngx.semaphore + lua_shared_dict + ngx.sleep to do such kind of things, achieving 1.5 million ~ 2 million connections in a single box. We could make this approach even more efficient when we add listen() and accept() methods to the cosocket objects so that we can sync data and control flow on the nginx worker process level more efficiently than the lua_shared_dict + ngx.sleep approach (the intra-worker level request dispatch is already very efficient with ngx.semaphore).

@mergify
Copy link

mergify bot commented Oct 13, 2021

This pull request is now in conflict :(

@mergify mergify bot added the conflict label Oct 13, 2021
@zhuizhuhaomeng
Copy link
Contributor

If anyone needs this API, should reimplement it via ffi.

@gustavosbarreto
Copy link

gustavosbarreto commented Mar 19, 2025

I’d like to reimplement this via FFI, but I’m not sure about the best approach within the lua-nginx-module. Are there any existing examples or recommendations on how to properly work using FFI in this context?

I need getfd because I’m working on a solution where Nginx needs to pass an open socket descriptor to another process using SCM_RIGHTS. In this case, simply creating a new connection is not sufficient. I need to transfer an already open file descriptor from Nginx to another process.

If you have any guidance or references, I’d really appreciate it.

@zhuizhuhaomeng
Copy link
Contributor

here is an example: ngx_http_lua_ffi_socket_tcp_sslhandshake

@linsite
Copy link

linsite commented Apr 27, 2025

I’d like to reimplement this via FFI, but I’m not sure about the best approach within the lua-nginx-module. Are there any existing examples or recommendations on how to properly work using FFI in this context?

I need getfd because I’m working on a solution where Nginx needs to pass an open socket descriptor to another process using SCM_RIGHTS. In this case, simply creating a new connection is not sufficient. I need to transfer an already open file descriptor from Nginx to another process.

If you have any guidance or references, I’d really appreciate it.

Hi, are you still working on this? I share the same need.

@gustavosbarreto
Copy link

Hi, are you still working on this? I share the same need.

@linsite https://github.com/gustavosbarreto/websocket-fd-handoff

@linsite
Copy link

linsite commented Apr 28, 2025

Hi, are you still working on this? I share the same need.

@linsite https://github.com/gustavosbarreto/websocket-fd-handoff

Thanks a lot.

@zhuizhuhaomeng
Copy link
Contributor

Have added the getfd() function.
This api will be available in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants