h2o / h2o

H2O - the optimized HTTP/1, HTTP/2, HTTP/3 server
https://h2o.examp1e.net
MIT License
10.83k stars 839 forks source link

Start adding LUA script request handling #104

Open mingodad opened 9 years ago

mingodad commented 9 years ago

Hello all !

I started playing with adding LUA scripting language request handler, it's still very basic but we can register lua handlers and server content with LUA.

If you want to contribute/fork you are welcome !

https://github.com/mingodad/h2o/tree/lua

To have an idea of how it perform (using luajit) I have two hadlers:

Machine laptop Celeron 2 cores ubuntu 14.04.

Ideally I want to have a way to handle more than one request by thread using coroutines and have somehow a way to do nonblocking calls to external/internal http urls.

Any idea on the above are welcome !

kazuho commented 9 years ago

Wow! Looks promising!

mingodad commented 9 years ago

I did a refactoring, now we can create request handlers from LUA itself in a function called on thread initialization:

--per thread initialization
function h2oOnThreadStart(ctx)
    ctx:register_handler_global("/LUA/")
    --ctx:register_handler_on_host("/LUA/", "www.example.com")
end

--per thread finalization
function h2oOnThreadEnd(ctx)
end

--per request call
function h2oManageRequest(req)
    local host = req:host() --also req:authority()
    local path = req:path()
    if path:find("/LUA/", 1, true) then
        return myLuaRequestHandler(req, host, path)
    end
    return 0
end
kazuho commented 9 years ago

@mingodad Is there any reason you chose lua over luajit?

I am considering of adding support for a script engine to rewrite the requests (mod_rewrite is a nightmare; IMO users should be able to rewrite the requests using a more manageable language), and wondering if luajit suites the case.

mingodad commented 9 years ago

Hello Kazuho !

Yes basically when I say Lua I mean Luajit, it's basically lua at light speed with extras that make it easy to drive C code.

I already have some experience with it and probably we achieve it faster if we work together !

Cheers !

On Mon, Feb 9, 2015 at 7:52 PM, Kazuho Oku notifications@github.com wrote:

@mingodad https://github.com/mingodad Is there any reason you chose lua over luajit?

I am considering of adding support for a script engine to rewrite the requests (mod_rewrite is a nightmare; IMO users should be able to rewrite the requests using a more manageable language), and wondering if luajit suites the case.

— Reply to this email directly or view it on GitHub https://github.com/h2o/h2o/issues/104#issuecomment-73640246.

mingodad commented 9 years ago

Hello again !

I also forked a scripting language that also was a kind of fork from Lua but using a C like sysntax but Lua always win on performance nornal lua 10/20% and Luajit near C speed on several situations.

And simplicity/size is also a key win quality, but my experience so far is that it is very nice for small scripts/programs, if we try to write non trivial programs with it we'll get in trouble with circular references, runtime errors and garbage collection.

So using it to drive C code, prototyping or small/simple scripts is almost fine.

Like in the case you are mentioning the simple regular expression implemented in Lua is very performant, simple and powerful.

Cheers !

On Mon, Feb 9, 2015 at 9:19 PM, Domingo Alvarez Duarte mingodad@gmail.com wrote:

Hello Kazuho !

Yes basically when I say Lua I mean Luajit, it's basically lua at light speed with extras that make it easy to drive C code.

I already have some experience with it and probably we achieve it faster if we work together !

Cheers !

On Mon, Feb 9, 2015 at 7:52 PM, Kazuho Oku notifications@github.com wrote:

@mingodad https://github.com/mingodad Is there any reason you chose lua over luajit?

I am considering of adding support for a script engine to rewrite the requests (mod_rewrite is a nightmare; IMO users should be able to rewrite the requests using a more manageable language), and wondering if luajit suites the case.

— Reply to this email directly or view it on GitHub https://github.com/h2o/h2o/issues/104#issuecomment-73640246.

nikolay commented 8 years ago

@mingodad So, what's happened? I really love to use H2O, but mruby is not an option for me personally.

mingodad commented 8 years ago

Hello Nikolay !

I could get it to work somehow as shown on this example: https://github.com/mingodad/h2o/blob/lua/h2o-on-thread-start.lua

It was based on inserting hooks into h2o src/main.c and modifying other parts to make then callable from lua : https://github.com/mingodad/h2o/blob/lua/src/h2o_lua.c

But h2o was changing a lot fast at that time and I did not have a real project to drive me continue adjusting it.

Have you looked at it ? Do you have any suggestion ? Have you tried to update it ?

Cheers !

On Sat, Dec 26, 2015 at 10:13 AM, Nikolay Kolev notifications@github.com wrote:

@mingodad https://github.com/mingodad So, what's happened? I really love to use H2O, but mruby is not an option for me personally.

— Reply to this email directly or view it on GitHub https://github.com/h2o/h2o/issues/104#issuecomment-167307037.

nikolay commented 8 years ago

@mingodad I haven't, but I will definitely try. My C/C++ skills are very dated (last time I used this was back in the 90s), so, I can't help much or not quickly enough. I think LuaJIT support in H2O can greatly transfrom the project and make it a true Nginx competitor. This is a very promising project, IMHO, and I personally can chip in some ongoing support. Why don't you sign up for https://salt.bountysource.com - I'm sure others can also give you incentive to continue your work!

007lva commented 8 years ago

+1 for bountysource

lunemec commented 7 years ago

+1 LuaJit would enable me to use H2O in production, I require Prometheus metrics capability and that can be easily achieved with lua (I already have it on nginx).

chrisUsick commented 5 years ago

I'm late to the party but I just want to gain some understanding as to why this is important. Where does this fit into the mruby scripting? For instance someone used mruby to create a prometheus metrics endpoint.

lunemec commented 5 years ago

Well, mruby might be nice, but everyone uses lua for this. And also, 25 years of code being made is very hard to beat. Sure if there is mruby prometheus plugin, it might solve my problem, but wouldnt it be nice to have Lua support?

nikolay commented 5 years ago

In the web servers space, Lua dominates. Less and less people use Ruby today, unlike Lua, which is even chosen for Kubernetes Helm 3.0. To me personally, Ruby is a better language, but, unfortunately, it has a bad reputation when it comes to performance and lightness and it's not used for such purposes. Nobody has a single doubt that LuaJIT is way faster and that's why it's a popular choice for embedding. Many projects, which require high performance, such as Fluent Bit, use LuaJIT. My son even learns Lua as Roblox uses it. I know that there's love for Ruby in Japan, but the rest of the world isn't so keep about it nowadays with many of the Ruby fans left Ruby for Crystal long ago.

chrisUsick commented 5 years ago

Excellent insights Lucáš and Nikolay. That is what I wanted to clear up. This effort is really adding a parallel feature set vs adding something completely new to h2o, and I understand the reasoning behind Lua being the dominant language. On Mon, Sep 17, 2018 at 8:19 PM Nikolay Kolev notifications@github.com wrote:

In the web servers space, Lua dominates. Less and less people use Ruby today, unlike Lua, which is even chosen for Kubernetes Helm 3.0. To me personally, Ruby is a better language, but, unfortunately, it has a bad reputation when it comes to performance and lightness and it's not used for such purposes. Nobody has a single doubt that LuaJIT is way faster and that's why it's a popular choice for embedding. Many projects, which require high performance, such as Fluent Bit, use LuaJIT. My son even learns Lua as Roblox uses it. I know that there's love for Ruby in Japan, but the rest of the world isn't so keep about it nowadays with many of the Ruby fans left Ruby for Crystal long ago.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/h2o/h2o/issues/104#issuecomment-422221644, or mute the thread https://github.com/notifications/unsubscribe-auth/AGSLtVP7eJwFtTkq4xFlDtanXEP7NDpWks5ucEodgaJpZM4DRlpE .