News for hackers. Stuff that matters.
Re:Nginx C function by drenvuk on Sunday October 14, 2018 @01:40AM (#18211549)
There is something that should be said about nginx's third party modules. There are many and they add quite a bit of functionality, especially when they're upstream modules.

https://www.nginx.com/resources/wiki/modules/

It's great because you don't even need a web framework, really. If you're comfortable and confident in nginx you can get quite a bit of performance out of the server while adding your application logic to it. I'm not sure why more people don't and just use it as a reverse proxy.

Re:Nginx C function by cagenut on Sunday October 14, 2018 @02:24AM (#18211637)
because you could do all that and more with apache modules, the reason people switched was because nginx didnt get bogged down in all that.

imho its keyword engineering, but you can see given the history why nginx adopters aren't thinking that way.

Re:Nginx C function by eddieroger on Sunday October 14, 2018 @09:17AM (#18212738)
I don't know - I switched from Apache to NGINX because I preferred it's config formatting over writing XML when I stopped using things like CPanel. Many of the NGINX servers under my current role run the NGINX Lua module performing logic on over 1B requests per day. I do try to reduce dependency on modules, but that's not because it makes it too Apache-like, it's for simplicity's sake. If you don't want NGINX to have these features, don't compile it in, then boom. no longer bogged down.
Re:Nginx C function by yjftsjthsd-h on Sunday October 14, 2018 @11:11AM (#18213268)
Separation of concerns? Might be worth it, but there are tradeoffs in binding everything together.
Re:Nginx C function by fcambus on Sunday October 14, 2018 @02:44AM (#18211688)
For a more mature implementation of this concept, I would highly recommend having a look at Kore: https://kore.io

It has a sane architecture, and is fully privilege separated by default, with private keys isolated in a separate process. I've been using it lately to write REST APIs in C, and the experience has been awesome, it has great APIs to parse requests and easily construct responses.

Re:Nginx C function by setquk on Sunday October 14, 2018 @03:06AM (#18211742)
Genuine question here but why would you want to write REST APIs in C?

I’ve been writing C for 25 years and I wouldn’t trust myself not to do something stupid.

Re:Nginx C function by annchin_peh on Monday October 15, 2018 @12:23AM (#18217368)
I preferred nginx over kore.io. The nginx can do a lot of things and been recognize by a lot of popular company
Re:Nginx C function by chx on Saturday October 13, 2018 @11:09PM (#18211155)
Could someone explain to me why would I choose this over https://github.com/openresty/lua-nginx-module ? It supports LuaJIT and while I suppose it is possible to write faster C code by hand than JIT translated Lua it'd be hard and I'd fear the maintainability of hand optimized C code.
Re:Nginx C function by comex on Sunday October 14, 2018 @12:38AM (#18211396)
For one thing, you don't have to use this module with C. Any language that supports exporting C-compatible functions will do – there's C++ of course, but also Rust, Go, D, Nim, and many many others.
Re:Nginx C function by shakna on Sunday October 14, 2018 @01:31AM (#18211525)
LuaJIT also supports C-functions. It's FFI [0] will allow you to declare any C-compatible function or data structure, and then use it in normal Lua code.

[0] https://luajit.org/ext_ffi.html

Re:Nginx C function by guipsp on Sunday October 14, 2018 @04:32AM (#18211954)
Why would you ever opt to go thru Lua in this case?
Re:Nginx C function by shakna on Sunday October 14, 2018 @08:07AM (#18212516)
It's well supported, well maintained, and around for the foreseeable future. (OpenResty)

The language implementation is fast, and allows you to tap into any C-compatible library.

The question isn't why you would go through Lua, but why you would choose something with less support?

Re:Nginx C function by Hello71 on Sunday October 14, 2018 @09:25AM (#18212773)
This doesn't seem to support actually exporting C-compatible functions.
Re:Nginx C function by shakna on Sunday October 14, 2018 @06:12PM (#18215793)
Create some Lua code that runs within C? Rather than just passing and converting to C values when needed?

That's the role of the C API [0]. But usually you'd just be passing C up to Lua to bind it together.

[0] https://luajit.org/ext_c_api.html

Re:Nginx C function by annchin_peh on Friday October 19, 2018 @04:47AM (#18255167)
it's complicated
Re:Nginx C function by legohead on Sunday October 14, 2018 @12:14AM (#18211324)
It's faster like you said. but probably the main benefit are people who have already developed a large program in C.

C isn't hard to maintain -- it's not some mystical language..

Re:Nginx C function by Something1234 on Sunday October 14, 2018 @12:45AM (#18211420)
It just takes discipline. The discipline to not haphazardly throw macros all over the place, and keep them pure(no side effects).
Re:Nginx C function by abstractbeliefs on Sunday October 14, 2018 @01:40AM (#18211548)
Sadly, the reason that discipline is such a vaunted virtue is because it's hard. Even the most well meaning and diligent C developers still make mistakes.

Tools should be ergonomic, they should reduce the need for diligence, care, discipline, and intelligence.

I'm not saying that the above aren't all important to programming, or that we should abandon anything that's not easy, but we're humans and make human mistakes and tools that are unforgiving when we do are ripe to be replaced by those that nudge us back on track.

Re:Nginx C function by Something1234 on Sunday October 14, 2018 @01:56AM (#18211572)
Mostly the compiler just needs to slap your hand harder, and tell you no when you do something stupid or depend on undefined behavior. Also better static analysis would be nice.

I completely agree with you on all points. Languages exist to make our lives easier.

Re:Nginx C function by abstractbeliefs on Sunday October 14, 2018 @02:05AM (#18211592)
Well, there's a little more than that, as we actually have told compiler devs we don't want that.

When developers demand that compilers compete with each other on fractions of a percent of runtime performance, we set ourselves up for things like UB acting in unintuitive ways. When people give us the choice between safer but slower systems, and fast but unsafe ones, until recently people have overwhelmingly chosen fast and unsafe and pretended they're superhuman enough to not write bad code.

Re:Nginx C function by anonymoushn on Sunday October 14, 2018 @02:54AM (#18211717)
This is probably too much to ask for. I recently fixed an issue caused by UB in openresty of the form "nginx uses a NULL pointer and a length of 0 to signify a null string" + "nginx sometimes searches for strings in other strings using a function that takes start and end pointers for the haystack". This function works fine on lots of 0-length haystack strings (probably returning no match), but as you probably know, NULL + 0 is allowed to evaluate to anything. This seems pretty hard to detect statically short of banning programs that do arithmetic on any pointer without first NULL checking it.
Re:Nginx C function by sureaboutthis on Sunday October 14, 2018 @09:31AM (#18212792)
That's why we choose to use C and do the other things, not because it is easy, but because it is hard, because that goal will serve to organize and measure the best of our energies and skills.
Re:Nginx C function by kbenson on Saturday October 13, 2018 @11:50PM (#18211261)
It might be easier to tie in a few different external libraries if you're using C than with Lua FFI, depending on the libraries.
Re:Nginx C function by annchin_peh on Sunday October 14, 2018 @10:58PM (#18217117)
Hmm.. I've tried luaJit module and I've given up half way when building as it seemed a lot of compatibility issue(maybe not professional enough to build), sincerely I don't know lua and I prefer go for c module over any other language mixed together since nginx is build from C

For nginx-c-function, it's easy build and I can build this as dynamic module and use it with other servers which has same version of nginx.

It might be good to use lua if the person not good in c/c++.

Re:Nginx C function by ksec on Sunday October 14, 2018 @05:42AM (#18212145)
Or something "lighter" like H20 with mRuby?

https://h2o.examp1e.net/index.html

Re:Nginx C function by writepub on Sunday October 14, 2018 @02:38AM (#18211669)
We're huge fans of C interfaces on the sever side. https://kore.io is our preferred stack.

It's primary interface is C. In our opinion, it's lighter weight, and simpler to integrate with your app.

Re:Nginx C function by noway421 on Sunday October 14, 2018 @12:01AM (#18211288)
Is the function blocking or asynchronous? Cant quite find it in the README.
Re:Nginx C function by drenvuk on Sunday October 14, 2018 @01:16AM (#18211488)
It works with nginx threads using aio. It's in one of the issues there if you look deep enough.

https://github.com/Taymindis/nginx-c-function/issues/2

I haven't figured out how to get rid of the drop in performance though. Lets say you just return a 200 from the nginx location like so:

  location /a {
        return 200 "{\"result\": \"Hello World!\"}";
    }
vs

    location / {
        ngx_http_c_func_call 'getRoute';
    }

The requests per second will drop by about 40% of what you'd otherwise get. This is with 4 workers, the worker_rlimit_nofile set to 262144 etc, etc.

I'm not sure what I'm doing wrong. It is pretty nice given that you also get all of the other options like upstream handling and reverse proxying and pretty damned good speed. The flexibility this adds is beyond awesome. But if I only need max speed for an api that only talks to postgres, redis, and a few other things, I'd just use H2o.

Re:Nginx C function by int0x80 on Sunday October 14, 2018 @07:43AM (#18212445)
what is the src of the C function?
Re:Nginx C function by drenvuk on Monday October 15, 2018 @05:41PM (#18224069)
Check this file out, it's in the example that's provided: https://github.com/Taymindis/sample-ngx-cfunc-project/blob/m... I've stripped out everything of 'getRoute' and only provide a response of a global variable 'thing' that never changes.

  std::string thing = "{\"result\": \"Hello World!\"}";
  void getRoute(ngx_http_c_func_ctx_t* ctx) {
    ngx_http_c_func_write_resp(
                ctx,
                200,
                NULL,
                ngx_http_c_func_content_type_json,
                ::thing.c_str(),
                ::thing.length()
    );
  }
Re:Nginx C function by annchin_peh on Monday October 15, 2018 @12:17AM (#18217353)
Re:Nginx C function by annchin_peh on Sunday October 14, 2018 @10:31PM (#18217029)
Can anyone explain to me what is the different between

ngx_http_c_func_call "my_app_simple_get_greeting";

vs

ngx_http_c_func_call "my_app_simple_get_greeting" respTo=myResponseVariable;

I didn't see any different, just create a variable.

Re:Nginx C function by annchin_peh on Sunday October 14, 2018 @10:50PM (#18217095)
Re:Nginx C function by kyberias on Sunday October 14, 2018 @01:58AM (#18211577)
cgi-bin all over again?
Re:Nginx C function by viraptor on Sunday October 14, 2018 @05:32AM (#18212110)
Not really. Cgi-bin processes were executed from scratch. This is a single function call from an dynamic lib.
Re:Nginx C function by andyhoang on Sunday October 14, 2018 @01:07AM (#18211464)
Is this like lambda(aws) for C?
Re:Nginx C function by smadge on Sunday October 14, 2018 @02:03AM (#18211588)
I suppose a procedure that responds to an http request is now called a lambda.
Re:Nginx C function by tylermauthe on Sunday October 14, 2018 @02:47AM (#18211695)
No...?
Re:Nginx C function by rocky1138 on Sunday October 14, 2018 @01:29AM (#18211519)
That's what it reads like, yeah.
Re:Nginx C function by another-cuppa on Sunday October 14, 2018 @05:26AM (#18212092)
Why does it keep mentioning .so? Nobody refers to it like that. Just say dynamically link.
Re:Nginx C function by rntksi on Sunday October 14, 2018 @12:35AM (#18211391)
Any idea why sharing this link to Pocket on Android 9 does not work? ("this webpage does not contain a valid link")