shoving your application code into the web serverlua - example 1 function allwords() 2 local...

48
Shoving Your Application Code Into the Web Server Using OpenResty and nginx to write high-performance web applications Nikola Pleji´ c WebCamp Zagreb, October 6th, 2018

Upload: others

Post on 01-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Shoving Your Application Code Into the Web ServerUsing OpenResty and nginx to write high-performance web applications

Nikola Plejic

WebCamp Zagreb, October 6th, 2018

Page 2: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Who Am I?

I dabbling around the web for a whileI GNU/Linux, Rust, Lua, C++, Clojure, PythonI senior engineer, TVbeat (of course we’re hiring)

I mostly HTTP

Page 3: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Who Am I?

I dabbling around the web for a whileI GNU/Linux, Rust, Lua, C++, Clojure, PythonI senior engineer, TVbeat (of course we’re hiring)I mostly HTTP

Page 4: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

web server

Page 5: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

I “c10k” problemI 30%-40% of the public internet (*)

I (*) - lies, big lies, and statistics

I modular architectureI asynchronous, event-driven model

Page 6: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

I “c10k” problem

I 30%-40% of the public internet (*)I (*) - lies, big lies, and statistics

I modular architectureI asynchronous, event-driven model

Page 7: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

I “c10k” problemI 30%-40% of the public internet (*)

I (*) - lies, big lies, and statistics

I modular architectureI asynchronous, event-driven model

Page 8: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

I “c10k” problemI 30%-40% of the public internet (*)

I (*) - lies, big lies, and statistics

I modular architectureI asynchronous, event-driven model

Page 9: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

I “c10k” problemI 30%-40% of the public internet (*)

I (*) - lies, big lies, and statistics

I modular architecture

I asynchronous, event-driven model

Page 10: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

I “c10k” problemI 30%-40% of the public internet (*)

I (*) - lies, big lies, and statistics

I modular architectureI asynchronous, event-driven model

Page 11: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

modularity

I LDAP -> PostgreSQL -> upstreamI append X-Requested-By header to every HTTP requestI REST -> Protobuf

Page 12: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

modularity

I LDAP -> PostgreSQL -> upstream

I append X-Requested-By header to every HTTP requestI REST -> Protobuf

Page 13: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

modularity

I LDAP -> PostgreSQL -> upstreamI append X-Requested-By header to every HTTP request

I REST -> Protobuf

Page 14: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

modularity

I LDAP -> PostgreSQL -> upstreamI append X-Requested-By header to every HTTP requestI REST -> Protobuf

Page 15: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

what if we could script these nginx modules?

Page 16: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua

I small, dynamic programming language

I think: JavaScript, Scheme, TclI ANSI C: highly portable and embedableI LuaJIT: probably the best dynamic language JIT compiler in the world (*)

(*) - lies, big lies, and benchmarks

Page 17: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua

I small, dynamic programming languageI think: JavaScript, Scheme, Tcl

I ANSI C: highly portable and embedableI LuaJIT: probably the best dynamic language JIT compiler in the world (*)

(*) - lies, big lies, and benchmarks

Page 18: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua

I small, dynamic programming languageI think: JavaScript, Scheme, TclI ANSI C: highly portable and embedable

I LuaJIT: probably the best dynamic language JIT compiler in the world (*)

(*) - lies, big lies, and benchmarks

Page 19: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua

I small, dynamic programming languageI think: JavaScript, Scheme, TclI ANSI C: highly portable and embedableI LuaJIT: probably the best dynamic language JIT compiler in the world (*)

(*) - lies, big lies, and benchmarks

Page 20: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua

I small, dynamic programming languageI think: JavaScript, Scheme, TclI ANSI C: highly portable and embedableI LuaJIT: probably the best dynamic language JIT compiler in the world (*)

(*) - lies, big lies, and benchmarks

Page 21: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua - example

1 function fact(n)2 if n == 1 then3 return 14 end5

6 return n * fact(n - 1)7 end8

9 print(fact(128))

Page 22: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua - example

1 function allwords ()2 local line = io.read()3 local pos = 14 return function ()5 while line do6 local s, e = string.find(line, "%w+", pos)7 if s then -- found a word?8 pos = e + 1 -- update next position9 return string.sub(line, s, e)

10 else11 line = io.read()-- word not found; try next line12 pos = 1 -- restart from first position13 end14 end15 return nil -- no more lines: end of traversal16 end17 end

“Programming in Lua”, 1st ed. §10.2

Page 23: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua - embedable in C apps...nginx - written in C...

Page 24: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Lua - embedable in C apps...nginx - written in C...

Page 25: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

ngx http lua module

Page 26: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

ngx http lua module

Page 27: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

ngx http lua module

1 worker_processes 1;2 error_log logs/error.log;3 events {4 worker_connections 1024;5 }6 http {7 server {8 listen 8080;9 location /brew {

10 default_type text/html;11 content_by_lua_block {12 -- Lua code13 }14 }15 }16 }

Page 28: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

ngx http lua module

content by lua block:1 ngx.req.read_body()2 local body = ngx.req.get_body_data()3

4 if body and (body:find("coffee") or5 body:find("beer")) then6 ngx.status = 4187 ngx.say("I’m a teapot")8 elseif body and body:find("tea") then9 ngx.status = 200

10 ngx.say("Coming right up")11 else12 ngx.status = 40013 ngx.say("Confused")14 end15

16 return ngx.exit(ngx.status)

Page 29: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

ngx http lua module

1 http {2 upstream backend {3 server 0.0.0.1;4

5 balancer_by_lua_block {6 -- Lua code7 }8 }9

10 server {11 listen 8080;12

13 location / {14 proxy_pass http://backend/;15 }16 }17 }

Page 30: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

ngx http lua module

balancer by lua block:

1 local redis = require "resty.redis"2 local balancer = require "ngx.balancer"3

4 local r = redis:new()5 local ok, err = r:connect("127.0.0.1", 6379)6

7 local host, err = r:get("my_upstream:host")8 local port, err = r:get("my_upstream:port")9

10 local ok, err = balancer.set_current_peer(host, port)11

12 if not ok then13 ngx.log(ngx.ERR, "failed to set peer: ", err)14 return ngx.exit(500)15 end

Page 31: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

OpenResty

nginx + LuaJIT + async libraries

Page 32: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

OpenResty

nginx

+ LuaJIT + async libraries

Page 33: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

OpenResty

nginx + LuaJIT

+ async libraries

Page 34: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

OpenResty

nginx + LuaJIT + async libraries

Page 35: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

OpenResty

ngx http lua module ≈ OpenResty

Page 36: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

non-trivial usage

I loading code from files: content by lua fileI LuaRocks is fair gameI testing: busted + Test::Nginx::SocketI profiler: a set of SystemTap-based scripts

Page 37: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

non-trivial usage

I loading code from files: content by lua file

I LuaRocks is fair gameI testing: busted + Test::Nginx::SocketI profiler: a set of SystemTap-based scripts

Page 38: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

non-trivial usage

I loading code from files: content by lua fileI LuaRocks is fair game

I testing: busted + Test::Nginx::SocketI profiler: a set of SystemTap-based scripts

Page 39: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

non-trivial usage

I loading code from files: content by lua fileI LuaRocks is fair gameI testing: busted + Test::Nginx::Socket

I profiler: a set of SystemTap-based scripts

Page 40: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

non-trivial usage

I loading code from files: content by lua fileI LuaRocks is fair gameI testing: busted + Test::Nginx::SocketI profiler: a set of SystemTap-based scripts

Page 41: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

what’s not to like?

I Lua fragmentation: LuaJIT vs. PUC-Lua-currentI it’s not easy being asynchronous

I use OpenResty librariesI get your hands dirty with C

Page 42: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

what’s not to like?

I Lua fragmentation: LuaJIT vs. PUC-Lua-current

I it’s not easy being asynchronousI use OpenResty librariesI get your hands dirty with C

Page 43: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

what’s not to like?

I Lua fragmentation: LuaJIT vs. PUC-Lua-currentI it’s not easy being asynchronous

I use OpenResty librariesI get your hands dirty with C

Page 44: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

what’s not to like?

I Lua fragmentation: LuaJIT vs. PUC-Lua-currentI it’s not easy being asynchronous

I use OpenResty librariesI get your hands dirty with C

Page 45: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Turing-complete web server configuration

Page 46: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Thank you!

Rate the talk!https://joind.in/talk/2de00

[email protected] — @nikolap:matrix.org

Page 47: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Thank you!Rate the talk!

https://joind.in/talk/2de00

[email protected] — @nikolap:matrix.org

Page 48: Shoving Your Application Code Into the Web ServerLua - example 1 function allwords() 2 local line=io.read() 3 local pos=1 4 returnfunction 5 while line do 6 local s,e=string.find(line,"%w+",pos)

Thank you!Rate the talk!

https://joind.in/talk/2de00

[email protected] — @nikolap:matrix.org