Introduction to Lua Luajit Openresty Luvit

Embed Size (px)

Citation preview

Lua & co

About me

network & sys admin (far away)programmerAnd more recently,Trainer/Facilitator @Afpa (adult training)

Introduction to Lua

Built in 1993 by Roberto Lerusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo Brazil

Lightweigth ( 24000 loc written in ANSI C), fast and easy to integrate scripting language.

Multi-paradigm: procedural, OO, fonctionnal

Run on every Unix et Windows flavors, smartphones (Android, iOS, Symbian, Windows Phone) , on embedded ( ARM, Rabbit), IBM mainframes, etc.

CLua bindings

MIT License

Where it is used and what for ?

redis

Used as:Extension

Monitoring

Scripting (DSL)

Proxy Load balancing

Cache management

Web Application Firewall

Robotic

Game play

Etc...

First step

https://gist.github.com/lduboeuf/66e87d2d18e26d0f48ec70539534e574

First step...

https://gist.github.com/lduboeuf/13a8f75b1701e9204c1202d9bab7a482

Metatable concept...

Allows you to modify tables behaviorOverride +, -, /, * operators (example: table1 + table2 )

Comparisons

Inheritance, interface (OOP)

Allows you to hook tables:When a new elements is added,

When a table element is accessed,

custom concatenation

etc...

OOP

Module pattern:

Class like pattern:

More about...

Multithreading: coroutines:Not as OS multithreading, non pre-emptive. Allows you non I/O blocking operations.

Weirdness:Array indices start at 1!

Not equals: if (i~=1) then end

No ternary operator ( x>3 ? 1 : 0; )

No increment shortcut: i = i + 1

Luajit

JIT compiler for lua (mainly created by Mike Pall )

Super fast!: 3 to 100x faster

Small: < 100 KB

Easy Native C library integrationthanks to ffi (foreign function include)

Luajit: ffi - example

Support & tools

Tools:Package management: Luarocks

Unit tests: busted or luaunit and more

Debugger :IDE ZeroBrane Studio

SupportMailing list

irc.freenode.net #lua

Workshops

Lua for the web

Choose between: 100% Lua: Xavante, Pegasus

existing server extension for:LightHttpd, Apache, Nginx, Tornado...

nodeJs like: Luvit

Openresty

Openresty = Nginx + Luajit + Modules

Nginx

HTTP server, reverse proxy, mail proxy

Asynchronous

Low memory footprint

Modular

Extensible

Nginx model

Source: http://www.aosabook.org/en/nginx.html

Nginx min server

worker_processes 1;error_log logs/error.log;events { worker_connections 1024;}http { server { listen 8000; location / {

index index.html; root public/; }}

$> curl http://0.0.0.0:8000/index.html

Openresty

Created and maintained mainly by Yichun Zhang (@agentzh) .( Taobao.com then Clouflare )

In production on high trafic site (Alibaba group, Clouflare,...)

Non blocking I/O modules for:

Memcached

Redis

MySQL / Drizzle

PostgreSQL

Websockets, CLI, Json, etc..

Openresty

Allows you to scripts in Lua during Nginx phasesInit phase

Rewrite phase.

Access phase.

Content phase.

Log phase

And more

Lua API to access Ngninx environment

Openresty

Lua code is executed within workers

Openresty Hello world

location /hellolua {

default_type 'text/plain';

content_by_lua_block { local name = ngx.var.arg_name or "Anonymous" ngx.say("Hello, ", name, "!") }; }

$> curl http://localhost/hellolua?name=Lua

Hello, Lua

Openresty: load external module

location /xxxx { rewrite_by_lua_file /path/to/rewrite.lua; access_by_lua_file /path/to/access.lua; content_by_lua_file /path/to/content.lua;}

Modules are loaded once per worker at first request

Openresty: sub request

location / { content_by_lua ' local res = ngx.location.capture("/memcached", { args = { cmd = "incr", key = ngx.var.uri } } ) ';}

location /memcached { set $memc_cmd $arg_cmd; set $memc_key $arg_key; memc_pass 127.0.0.1:11211;}

Openresty cosocket

Non blocking I/O thanks to Cosocket library:TCP or Unix Domain sockets

Allows you to write asynchronous sequential code! (bye bye callback hell)

Keepalive for reuse connections.

Openresty: TCP client

location /memcached { content_by_lua_block {' local sock = ngx.socket.connect("127.0.0.1", 11211) sock:send("SET foo bar 3600\r\n") local line = sock:receive() if line then ngx.say(line) end sock:setkeepalive() };}

$> curl http://localhost/memcached

STORED

Openresty: mysql

location /todos { content_by_lua_block { local mysql = require "resty.mysql" local db, err = mysql:new() if not db then ngx.say("failed to instantiate mysql: ", err) return end

db:set_timeout(1000) -- 1 sec --connect local res, err, errcode, sqlstate = db:query("select * from tasks") if not res then ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".") return end

local cjson = require "cjson" ngx.say("result: ", cjson.encode(res)) };}

Openresty: mini app

location /api/v0.1/todos { default_type application/json; content_by_lua_block {

local todo_api = require "todo-app"

--get location after /api/v0.1/todos local sub_loc = string.sub(ngx.var.request_uri, string.len(ngx.var.location)+1) if sub_loc == "" then sub_loc = "/" end

local router = require "router" local r = router.new()

r:get("/", todo_api.getAll) r:post("/", todo_api.create) r:delete("/:id", todo_api.delete) r:put("/:id", todo_api.update)

--execute routes r:execute(ngx.var.request_method, sub_loc, ngx.req.get_uri_args())

} }

https://github.com/lduboeuf/openresty-todo-app

Openresty - tools

Real time profiling

Test suite

Package manager (OPM)

Mailing list

Openresty: built with

Lapis ( http://leafo.net/lapis/ ) = openresty + libs utils + templating html + sessions

Sailor ( http://sailorproject.org/ ) = MVC, routing, templating, model generator, etc.

Bonus!

Protocol agnostic: Luvit

Built by Tim Caswell (@creationix) , a former NodeJs contributor.

In production within Rackspace for their monitoring agents.

Primary goal was to port Node to Lua: Memory gain: 20x!

Built on top of LibUV (same as Node) : event loop, Asynchronous I/O,

Use of OpenSSL, zlib libs.

Allows you to build self-executable apps, fast and lightweight.

Luvit

We can create apps nodeJs like (call-back based) or with sequential code (thanks to coroutine)

Lit: Package manager, repository and compiler

git clone [email protected]:creationix/hexes.gitcd hexeslit installlit make./hexes

lit make lit://creationix/hexes./hexes

When published to repository, this is simple as:

Example: compile and execute

Luvit: example

https://github.com/creationix/hexes

Luvit

Really cool if:Not afraid of sporadic docs

Not afraid with programing your own connector ;-) ( a connector for Prostgresql, Redis exist )

Community: freenode IRC #luvit , mailing list https://luvit.io

Voil, merci!

http://openresty.org/en/

http://luajit.org/

https://luvit.io/

https://www.lua.org/