SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Lua & co - @LDuboeuf - Novembre 2016
Lua & co
Lua & co - @LDuboeuf - Novembre 2016
About me
network & sys admin (far away)
programmer
And more recently,
Trainer/Facilitator @Afpa (adult training)
Lua & co - @LDuboeuf - Novembre 2016
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.
● C<->Lua bindings
● MIT License
Lua & co - @LDuboeuf - Novembre 2016
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...
Lua & co - @LDuboeuf - Novembre 2016
First step
https://gist.github.com/lduboeuf/66e87d2d18e26d0f48ec70539534e574
Lua & co - @LDuboeuf - Novembre 2016
First step...
https://gist.github.com/lduboeuf/13a8f75b1701e9204c1202d9bab7a482
Lua & co - @LDuboeuf - Novembre 2016
Metatable concept...
– Allows you to modify tables behavior
● Override « +, -, /, * » 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...
Lua & co - @LDuboeuf - Novembre 2016
OOP
Module pattern : Class like pattern :
Lua & co - @LDuboeuf - Novembre 2016
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
–
Lua & co - @LDuboeuf - Novembre 2016
Luajit
● JIT compiler for lua (mainly created by Mike
Pall )
● Super fast! : 3 to 100x faster
● Small : < 100 KB
● Easy Native C library integration thanks to ffi
(foreign function include)
Lua & co - @LDuboeuf - Novembre 2016
Luajit : ffi - example
Lua & co - @LDuboeuf - Novembre 2016
Support & tools
● Tools:
– Package management : Luarocks
– Unit tests : « busted » or « luaunit » and more…
– Debugger :IDE ZeroBrane Studio
● Support
– Mailing list
– irc.freenode.net #lua
– Workshops
Lua & co - @LDuboeuf - Novembre 2016
Lua for the web
● Choose between :
– 100 % Lua :
● Xavante, Pegasus
– existing server extension for :
● LightHttpd, Apache, Nginx, Tornado...
– nodeJs like :
● Luvit
Lua & co - @LDuboeuf - Novembre 2016
Openresty
Openresty = Nginx + Luajit + Modules
Lua & co - @LDuboeuf - Novembre 2016
Nginx
● HTTP server, reverse proxy, mail proxy
● Asynchronous
● Low memory footprint
● Modular
● Extensible
Lua & co - @LDuboeuf - Novembre 2016
Nginx model
Source : http://www.aosabook.org/en/nginx.html
Lua & co - @LDuboeuf - Novembre 2016
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
Lua & co - @LDuboeuf - Novembre 2016
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..
Lua & co - @LDuboeuf - Novembre 2016
Openresty
● Allows you to scripts in Lua during Nginx phases
– Init phase
– Rewrite phase.
– Access phase.
– Content phase.
– Log phase
– And more…
● Lua API to access Ngninx environment
Lua & co - @LDuboeuf - Novembre 2016
Openresty
● Lua code is executed within workers
Lua & co - @LDuboeuf - Novembre 2016
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
Lua & co - @LDuboeuf - Novembre 2016
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
Lua & co - @LDuboeuf - Novembre 2016
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;
}
Lua & co - @LDuboeuf - Novembre 2016
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.
Lua & co - @LDuboeuf - Novembre 2016
Openresty : TCP client
location /memcached {
content_by_lua_block {'
local sock = ngx.socket.connect("127.0.0.1", 11211)
sock:send("SET foo bar 3600rn")
local line = sock:receive()
if line then
ngx.say(line)
end
sock:setkeepalive()
};
}
$> curl http://localhost/memcached
STORED
Lua & co - @LDuboeuf - Novembre 2016
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))
} ;
}
Lua & co - @LDuboeuf - Novembre 2016
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
Lua & co - @LDuboeuf - Novembre 2016
Openresty - tools
● Real time profiling
● Test suite
● Package manager (OPM)
● Mailing list
Lua & co - @LDuboeuf - Novembre 2016
Openresty : built with
● Lapis ( http://leafo.net/lapis/ ) = openresty +
libs utils + templating html + sessions
● Sailor ( http://sailorproject.org/ ) = MVC, routing,
templating, model generator, etc.
Lua & co - @LDuboeuf - Novembre 2016
Bonus !
Lua & co - @LDuboeuf - Novembre 2016
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.
Lua & co - @LDuboeuf - Novembre 2016
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 git@github.com:creationix/hexes.git
cd hexes
lit install
lit make
./hexes
lit make lit://creationix/hexes
./hexes
When published to repository, this is simple as :
Example : compile and execute
Lua & co - @LDuboeuf - Novembre 2016
Luvit : example
https://github.com/creationix/hexes
Lua & co - @LDuboeuf - Novembre 2016
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
Lua & co - @LDuboeuf - Novembre 2016
Voilà, merci !
http://openresty.org/en/
http://luajit.org/
https://luvit.io/
https://www.lua.org/

Weitere ähnliche Inhalte

Was ist angesagt?

Madrid .NET Meetup: Microsoft open sources .NET!
Madrid .NET Meetup: Microsoft open sources .NET!Madrid .NET Meetup: Microsoft open sources .NET!
Madrid .NET Meetup: Microsoft open sources .NET!
Alfonso Garcia-Caro
 
Asynchronous Io Programming
Asynchronous Io ProgrammingAsynchronous Io Programming
Asynchronous Io Programming
l xf
 

Was ist angesagt? (20)

Cache in Chromium: Disk Cache
Cache in Chromium: Disk CacheCache in Chromium: Disk Cache
Cache in Chromium: Disk Cache
 
Gdb basics for my sql db as (percona live europe 2019)
Gdb basics for my sql db as (percona live europe 2019)Gdb basics for my sql db as (percona live europe 2019)
Gdb basics for my sql db as (percona live europe 2019)
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
Vagrant - Team Development made easy
Vagrant - Team Development made easyVagrant - Team Development made easy
Vagrant - Team Development made easy
 
FreeBSD hosting
FreeBSD hostingFreeBSD hosting
FreeBSD hosting
 
Porting Puppet to OpenBSD
Porting Puppet to OpenBSD Porting Puppet to OpenBSD
Porting Puppet to OpenBSD
 
Waylandifying Chromium: From downstream to shipping (ELCE 2020)
Waylandifying Chromium: From downstream to shipping (ELCE 2020)Waylandifying Chromium: From downstream to shipping (ELCE 2020)
Waylandifying Chromium: From downstream to shipping (ELCE 2020)
 
Experimental dtrace
Experimental dtraceExperimental dtrace
Experimental dtrace
 
Madrid .NET Meetup: Microsoft open sources .NET!
Madrid .NET Meetup: Microsoft open sources .NET!Madrid .NET Meetup: Microsoft open sources .NET!
Madrid .NET Meetup: Microsoft open sources .NET!
 
Doctrine Project
Doctrine ProjectDoctrine Project
Doctrine Project
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成
 
Infrastructure as Code with Chef / Puppet
Infrastructure as Code with Chef / PuppetInfrastructure as Code with Chef / Puppet
Infrastructure as Code with Chef / Puppet
 
Drush for humans - SANDcamp 2013
Drush for humans - SANDcamp 2013Drush for humans - SANDcamp 2013
Drush for humans - SANDcamp 2013
 
Fosdem i inne konferencje
Fosdem i inne konferencjeFosdem i inne konferencje
Fosdem i inne konferencje
 
Leveraging zeromq for node.js
Leveraging zeromq for node.jsLeveraging zeromq for node.js
Leveraging zeromq for node.js
 
Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1
 
Asynchronous Io Programming
Asynchronous Io ProgrammingAsynchronous Io Programming
Asynchronous Io Programming
 
Dev/Stage/Prod Parity with Vagrant
Dev/Stage/Prod Parity with VagrantDev/Stage/Prod Parity with Vagrant
Dev/Stage/Prod Parity with Vagrant
 
OpenZFS at LinuxCon
OpenZFS at LinuxConOpenZFS at LinuxCon
OpenZFS at LinuxCon
 
Happy birthday "monUPMC": 9 years of Portal at UPMC
Happy birthday "monUPMC": 9 years of Portal at UPMCHappy birthday "monUPMC": 9 years of Portal at UPMC
Happy birthday "monUPMC": 9 years of Portal at UPMC
 

Ähnlich wie Introduction to Lua Luajit Openresty Luvit

Openstack overview thomas-goirand
Openstack overview thomas-goirandOpenstack overview thomas-goirand
Openstack overview thomas-goirand
OpenCity Community
 

Ähnlich wie Introduction to Lua Luajit Openresty Luvit (20)

Introduction to Docker, December 2014 "Tour de France" Bordeaux Special Edition
Introduction to Docker, December 2014 "Tour de France" Bordeaux Special EditionIntroduction to Docker, December 2014 "Tour de France" Bordeaux Special Edition
Introduction to Docker, December 2014 "Tour de France" Bordeaux Special Edition
 
Docker and-containers-for-development-and-deployment-scale12x
Docker and-containers-for-development-and-deployment-scale12xDocker and-containers-for-development-and-deployment-scale12x
Docker and-containers-for-development-and-deployment-scale12x
 
Instant developer onboarding with self contained repositories
Instant developer onboarding with self contained repositoriesInstant developer onboarding with self contained repositories
Instant developer onboarding with self contained repositories
 
[HKOSCON][20180616][Containerized High Availability Virtual Hosting Deploymen...
[HKOSCON][20180616][Containerized High Availability Virtual Hosting Deploymen...[HKOSCON][20180616][Containerized High Availability Virtual Hosting Deploymen...
[HKOSCON][20180616][Containerized High Availability Virtual Hosting Deploymen...
 
Openstack overview thomas-goirand
Openstack overview thomas-goirandOpenstack overview thomas-goirand
Openstack overview thomas-goirand
 
Type safe, versioned, and rewindable stream processing with Apache {Avro, K...
Type safe, versioned, and rewindable stream processing  with  Apache {Avro, K...Type safe, versioned, and rewindable stream processing  with  Apache {Avro, K...
Type safe, versioned, and rewindable stream processing with Apache {Avro, K...
 
Node.js an Exectutive View
Node.js an Exectutive ViewNode.js an Exectutive View
Node.js an Exectutive View
 
Introduction to Docker at the Azure Meet-up in New York
Introduction to Docker at the Azure Meet-up in New YorkIntroduction to Docker at the Azure Meet-up in New York
Introduction to Docker at the Azure Meet-up in New York
 
Nodejs
NodejsNodejs
Nodejs
 
Leonid Vasilyev "Building, deploying and running production code at Dropbox"
Leonid Vasilyev  "Building, deploying and running production code at Dropbox"Leonid Vasilyev  "Building, deploying and running production code at Dropbox"
Leonid Vasilyev "Building, deploying and running production code at Dropbox"
 
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
 
[HKOSCON][20220611][AlviStack: Hong Kong Based Kubernetes Distribution]
[HKOSCON][20220611][AlviStack: Hong Kong Based Kubernetes Distribution][HKOSCON][20220611][AlviStack: Hong Kong Based Kubernetes Distribution]
[HKOSCON][20220611][AlviStack: Hong Kong Based Kubernetes Distribution]
 
Do you know what your Drupal is doing Observe it! (DrupalCon Prague 2022)
Do you know what your Drupal is doing Observe it! (DrupalCon Prague 2022)Do you know what your Drupal is doing Observe it! (DrupalCon Prague 2022)
Do you know what your Drupal is doing Observe it! (DrupalCon Prague 2022)
 
Modern web technologies
Modern web technologiesModern web technologies
Modern web technologies
 
Docker introduction
Docker introductionDocker introduction
Docker introduction
 
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)
 
Lua and its Ecosystem
Lua and its EcosystemLua and its Ecosystem
Lua and its Ecosystem
 
Java EE with NetBeans on OpenShift
Java EE with NetBeans on OpenShiftJava EE with NetBeans on OpenShift
Java EE with NetBeans on OpenShift
 
NodeJS
NodeJSNodeJS
NodeJS
 
Introduction to Docker (and a bit more) at LSPE meetup Sunnyvale
Introduction to Docker (and a bit more) at LSPE meetup SunnyvaleIntroduction to Docker (and a bit more) at LSPE meetup Sunnyvale
Introduction to Docker (and a bit more) at LSPE meetup Sunnyvale
 

Kürzlich hochgeladen

Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Kürzlich hochgeladen (20)

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

Introduction to Lua Luajit Openresty Luvit

  • 1. Lua & co - @LDuboeuf - Novembre 2016 Lua & co
  • 2. Lua & co - @LDuboeuf - Novembre 2016 About me network & sys admin (far away) programmer And more recently, Trainer/Facilitator @Afpa (adult training)
  • 3. Lua & co - @LDuboeuf - Novembre 2016 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. ● C<->Lua bindings ● MIT License
  • 4. Lua & co - @LDuboeuf - Novembre 2016 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...
  • 5. Lua & co - @LDuboeuf - Novembre 2016 First step https://gist.github.com/lduboeuf/66e87d2d18e26d0f48ec70539534e574
  • 6. Lua & co - @LDuboeuf - Novembre 2016 First step... https://gist.github.com/lduboeuf/13a8f75b1701e9204c1202d9bab7a482
  • 7. Lua & co - @LDuboeuf - Novembre 2016 Metatable concept... – Allows you to modify tables behavior ● Override « +, -, /, * » 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...
  • 8. Lua & co - @LDuboeuf - Novembre 2016 OOP Module pattern : Class like pattern :
  • 9. Lua & co - @LDuboeuf - Novembre 2016 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 –
  • 10. Lua & co - @LDuboeuf - Novembre 2016 Luajit ● JIT compiler for lua (mainly created by Mike Pall ) ● Super fast! : 3 to 100x faster ● Small : < 100 KB ● Easy Native C library integration thanks to ffi (foreign function include)
  • 11. Lua & co - @LDuboeuf - Novembre 2016 Luajit : ffi - example
  • 12. Lua & co - @LDuboeuf - Novembre 2016 Support & tools ● Tools: – Package management : Luarocks – Unit tests : « busted » or « luaunit » and more… – Debugger :IDE ZeroBrane Studio ● Support – Mailing list – irc.freenode.net #lua – Workshops
  • 13. Lua & co - @LDuboeuf - Novembre 2016 Lua for the web ● Choose between : – 100 % Lua : ● Xavante, Pegasus – existing server extension for : ● LightHttpd, Apache, Nginx, Tornado... – nodeJs like : ● Luvit
  • 14. Lua & co - @LDuboeuf - Novembre 2016 Openresty Openresty = Nginx + Luajit + Modules
  • 15. Lua & co - @LDuboeuf - Novembre 2016 Nginx ● HTTP server, reverse proxy, mail proxy ● Asynchronous ● Low memory footprint ● Modular ● Extensible
  • 16. Lua & co - @LDuboeuf - Novembre 2016 Nginx model Source : http://www.aosabook.org/en/nginx.html
  • 17. Lua & co - @LDuboeuf - Novembre 2016 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
  • 18. Lua & co - @LDuboeuf - Novembre 2016 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..
  • 19. Lua & co - @LDuboeuf - Novembre 2016 Openresty ● Allows you to scripts in Lua during Nginx phases – Init phase – Rewrite phase. – Access phase. – Content phase. – Log phase – And more… ● Lua API to access Ngninx environment
  • 20. Lua & co - @LDuboeuf - Novembre 2016 Openresty ● Lua code is executed within workers
  • 21. Lua & co - @LDuboeuf - Novembre 2016 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
  • 22. Lua & co - @LDuboeuf - Novembre 2016 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
  • 23. Lua & co - @LDuboeuf - Novembre 2016 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; }
  • 24. Lua & co - @LDuboeuf - Novembre 2016 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.
  • 25. Lua & co - @LDuboeuf - Novembre 2016 Openresty : TCP client location /memcached { content_by_lua_block {' local sock = ngx.socket.connect("127.0.0.1", 11211) sock:send("SET foo bar 3600rn") local line = sock:receive() if line then ngx.say(line) end sock:setkeepalive() }; } $> curl http://localhost/memcached STORED
  • 26. Lua & co - @LDuboeuf - Novembre 2016 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)) } ; }
  • 27. Lua & co - @LDuboeuf - Novembre 2016 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
  • 28. Lua & co - @LDuboeuf - Novembre 2016 Openresty - tools ● Real time profiling ● Test suite ● Package manager (OPM) ● Mailing list
  • 29. Lua & co - @LDuboeuf - Novembre 2016 Openresty : built with ● Lapis ( http://leafo.net/lapis/ ) = openresty + libs utils + templating html + sessions ● Sailor ( http://sailorproject.org/ ) = MVC, routing, templating, model generator, etc.
  • 30. Lua & co - @LDuboeuf - Novembre 2016 Bonus !
  • 31. Lua & co - @LDuboeuf - Novembre 2016 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.
  • 32. Lua & co - @LDuboeuf - Novembre 2016 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 git@github.com:creationix/hexes.git cd hexes lit install lit make ./hexes lit make lit://creationix/hexes ./hexes When published to repository, this is simple as : Example : compile and execute
  • 33. Lua & co - @LDuboeuf - Novembre 2016 Luvit : example https://github.com/creationix/hexes
  • 34. Lua & co - @LDuboeuf - Novembre 2016 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
  • 35. Lua & co - @LDuboeuf - Novembre 2016 Voilà, merci ! http://openresty.org/en/ http://luajit.org/ https://luvit.io/ https://www.lua.org/