Hiroshi Shibata

HTTP Programming with mruby
Hiroshi Shibata • June 05, 2015 • Singapore

HTTP Programming with mruby by Hiroshi Shibata

mruby is the lightweight implementation of the Ruby language and was released about a year ago. Can we use mruby to write web services?

This answer is YES - our company used mruby in large scaled web services. Even with mruby, we were able to create web services with tests and gems, and it also helped to solve some problems using Ruby code outside of a Rails application. In essence, mruby also provides programming features like HTTP to us web programmers.

Red Dot Ruby Conference 2015

00:00:19.480 hello everyone so I started to talk to
00:00:22.960 our middle as cod with MV so I changed
00:00:27.640 my talk title after creating impation
00:00:30.759 slides so I speak a slowly slowly
00:00:38.440 English so uh my name is uh shata hirosi
00:00:42.840 so please call me hiros so my internet
00:00:46.160 nickname is uh
00:00:48.039 hsbt so it is used at Twitter and giab
00:00:52.840 too my job title is chief engineer at
00:00:57.120 GMO
00:00:58.199 paperall I have many com bits such as
00:01:01.879 Ruby and rake and Ruby
00:01:04.760 James RA
00:01:08.400 Etc so uh I maintain this website ruang
00:01:13.640 and Ru and
00:01:15.920 R so if you have question and request to
00:01:21.680 this software and this website uh please
00:01:25.159 pick up after this talk
00:01:30.320 so I'm from Tokyo this is Tokyo so M
00:01:34.640 came from M so yeah it's a Ruby's BS
00:01:39.880 price so I arrived every R the Ruby come
00:01:45.520 from 201 something so it's a three times
00:01:49.960 for me so thanks our
00:01:53.159 Winston I'm from asex Arby so asex Arby
00:01:58.920 is
00:02:03.640 oops is a most active rues meet up in
00:02:07.000 Tokyo Japan so every
00:02:10.560 Tuesday if you visit Tokyo Japan at
00:02:15.360 Tuesday so please call me please mention
00:02:20.440 to these members on Twitter uh it's a
00:02:24.239 founder of our meet up Matan he is a
00:02:28.239 Ruan raise committee and Ruby Kai is
00:02:31.120 organizer and kakanis kisan and
00:02:35.519 me so we will pick up uh near that our
00:02:40.040 meet up
00:02:42.920 place so uh it's a rest adverti slides
00:02:47.440 so we are working
00:02:51.720 on developing to Ru 2.3 now but we do
00:02:57.080 not have the big fature of rby 2.3 yet
00:03:01.959 cly so we removed some obser and
00:03:07.040 duplicate Ries like rake and teret from
00:03:11.799 standard
00:03:13.400 rival but don't worry so we already
00:03:18.120 implemented bandle G mechanism you can
00:03:21.400 get these rival via Ruby JS find you
00:03:24.959 install
00:03:26.360 Ruby and if you have an issue press
00:03:30.319 submit uh our truck car sorry it's a red
00:03:33.959 m
00:03:35.799 so moreover we schedule the C developer
00:03:39.680 meeting every month
00:03:41.599 is please read this link for uh
00:03:46.319 details so okay today I talk middle code
00:03:51.920 with
00:03:53.360 every it's they took Target first web
00:03:56.720 engineer in good R programmer and
00:03:59.840 operation engineer and QA testing
00:04:02.760 engineer and MV committer is m committer
00:04:07.439 in R
00:04:10.239 Ric but
00:04:12.239 only so I hope to show this presentation
00:04:16.160 to
00:04:17.600 mat so at first I need to introduce
00:04:21.040 memor
00:04:22.479 bases is there any person who uses a
00:04:25.479 movie in
00:04:27.320 production wow
00:04:31.080 what mrv so it is a opal message so mrv
00:04:35.720 is a right weight implementation of the
00:04:38.240 Ruby longest comparing to the iso
00:04:41.479 standard its syntax is Ruby 1.9
00:04:46.880 comparable however M has some
00:04:50.280 differences compared C I listed
00:04:54.440 there uh important things uh MB is a
00:04:58.080 single binary with a native extension
00:05:00.919 and pure
00:05:03.240 Ries and it doesn't provide a require
00:05:07.560 method so we can't we couldn't use
00:05:11.440 require in EM
00:05:13.600 world it is uh confus many of
00:05:18.520 RS em has many
00:05:21.680 advantages I
00:05:23.400 think first the single binary like goang
00:05:27.720 it is very portable with that Ruby James
00:05:32.120 dependencies second it can be Ed to some
00:05:36.240 middleware like HP
00:05:39.120 server uh R it's the most important
00:05:42.080 things so MB is fun fun provides
00:05:46.000 productivity to
00:05:53.360 programmers
00:05:54.960 so yeah that do mrv B mrv buy is contr
00:05:59.520 by by build conf RV I figure out this
00:06:03.240 mechanism this part is the rubby script
00:06:08.560 if you want to customize them binary you
00:06:11.800 can put your library or S party Library
00:06:15.080 via conf gem syntax or like a bandra the
00:06:19.240 gem
00:06:21.199 f it passes GitHub
00:06:24.560 directory you can use local fire system
00:06:27.599 so like this
00:06:31.479 mjam mechanism provides
00:06:34.360 confusion please read this link for a
00:06:38.639 details mrb G mainly component is a mrb
00:06:43.080 g r and mrb Dory and SRC
00:06:48.360 Dory mrv gam is our end point of mrv
00:06:52.800 jam and mrv is a primary source for mrv
00:06:56.960 G it includes our Pure V
00:07:00.639 FES SSC is native extension
00:07:05.039 or
00:07:07.080 MV so again I will live coding so it is
00:07:14.319 on my first time for R
00:07:17.800 coding okay
00:07:35.039 M so how so mrb I couldn't know or hero
00:07:42.759 crass so I tried to create uh mrb her
00:07:48.280 mrb gam so it's empty for
00:07:57.120 the and do
00:08:03.879 M
00:08:17.960 of
00:08:19.639 spec
00:08:21.919 spec I
00:08:25.000 sense
00:08:27.199 MIT spec also
00:08:34.159 me so uh MB like uh the specification
00:08:39.039 needs to are two alabes so it's a rense
00:08:42.680 and alsoa
00:08:48.800 section and uh I write uh hero
00:08:55.080 Jam it's uh same as C
00:09:00.560 def
00:09:02.240 world but
00:09:07.760 rdrc yeah so it's a our first
00:09:12.399 implementation of mrv
00:09:14.480 G so I need to put our m h into our
00:09:20.839 build
00:09:23.200 config so like a conf
00:09:28.200 gem uh re
00:09:35.320 pass like and the
00:09:48.480 B oh so sa
00:09:52.600 so oh R they RC yeah so I'm now mrb Jam
00:09:58.079 alsoa so
00:10:00.760 it's so
00:10:02.120 easy so and this
00:10:06.240 binary uh REM move to home
00:10:16.240 holder it includes our mrv G so it's a
00:10:20.200 single
00:10:25.959 binary okay
00:10:39.959 okay so it's a mrb jam
00:10:43.399 mechanism so next topic is a m m
00:10:48.360 MV so MV has a a medable mechanism for
00:10:53.399 Middle rare So em provides a ruby
00:10:56.440 runtime and the syntax to Middle rare it
00:11:00.040 provides fun for
00:11:02.959 us I introduce
00:11:06.959 andm
00:11:08.519 matot who works J Pap made
00:11:12.519 the so he is my colag we can run Ruby
00:11:17.120 code via engine XM on engx process so it
00:11:21.560 is a engx configuration file it's not
00:11:24.399 the Ruby fire
00:11:30.279 you can play nxm on 10 or re boxes this
00:11:35.680 list is a minimum instruction for a
00:11:38.800 playing
00:11:40.639 nxm so it's
00:11:43.600 a copy and paste
00:11:46.800 building
00:11:48.600 nxm have many hook point for invoking
00:11:52.720 Ruby code on
00:11:54.360 engine first Ruby content Handler
00:11:59.399 it is a fundamental usage ofm like
00:12:03.320 this so it invs the her RV file with a
00:12:08.839 cach mechanism so cach mechanism is uh
00:12:13.519 this Ruby file is
00:12:16.600 casting
00:12:18.639 and you can write inline Rubik code into
00:12:23.120 our engine
00:12:24.760 configuration with a Cod
00:12:27.639 suffix mrv content
00:12:30.360 handore
00:12:33.760 code
00:12:35.440 m n in this handw every request it's
00:12:41.680 important every
00:12:44.720 request next is a MV set it sets the
00:12:49.920 return value from MB code
00:12:53.519 evaluation
00:12:55.120 into it Set uh it res to uh inex
00:13:01.320 variables like this uh back end variable
00:13:06.120 get uh proxy proxy
00:13:12.480 RV Cod suffix is the same as before
00:13:15.720 slides uh M set code and in line
00:13:22.399 code and every in it every in it is ined
00:13:27.560 once when Engine next M process
00:13:30.839 launches it is not Embark every request
00:13:35.040 like this uh
00:13:37.040 HTTP section and mbit some init
00:13:42.560 initializer file and location section is
00:13:46.720 MB content H it in our every
00:13:50.639 request MB init
00:13:53.440 work like M need Behavior but it invoke
00:13:58.079 the find one process
00:14:01.880 launched uh this uh mainly a function of
00:14:06.480 engine
00:14:08.399 xmrv so it is a sample cor of engine
00:14:11.399 xmrv engine xmrv creates our request and
00:14:16.639 connection cross and instances we can
00:14:21.040 use these instance for HTP HTTP
00:14:26.040 programming this production called cross
00:14:29.600 handles this
00:14:31.839 instance across the
00:14:35.160 initialize uh request and connection get
00:14:38.680 connection and it detects all RP from a
00:14:43.199 remote IP address like this connection
00:14:46.800 do remote IP
00:14:49.040 method
00:14:53.720 and some the some the the
00:14:59.639 request request instance have uh some
00:15:03.959 header and header attributes so X or X
00:15:10.279 RP so this
00:15:12.800 program uh handles this attribute and AR
00:15:19.399 IPI address if uh inject requests have a
00:15:26.000 and
00:15:27.600 connection IP address is a local host
00:15:31.680 address so this production code pass
00:15:36.040 through the it
00:15:39.759 request I put this code using uh mrv set
00:15:43.480 H like
00:15:45.000 this and handles the value of MV and
00:15:49.000 access control like this before
00:15:52.560 R before Ro middle aray on ra so we can
00:15:59.600 and access
00:16:01.480 controls
00:16:05.800 uh so our company GMO paperall uses
00:16:09.839 engine exm for these use
00:16:13.040 cases example for our calculation of
00:16:16.199 digest H for authentication and data
00:16:19.440 sharing with ra application
00:16:23.279 and
00:16:25.000 engx and to replace a complex
00:16:29.480 configuration to test our r
00:16:32.800 c so we said middleware as called as
00:16:36.199 this concept using engine
00:16:39.920 XM I show more details on our use
00:16:45.319 cases so I introduced data sharing and
00:16:49.360 resected access with engine xmv we have
00:16:53.279 first sharing service named the sday
00:16:56.120 AR I introduced this service
00:16:59.440 authenticate this service architecture
00:17:01.959 at the past
00:17:04.400 R so okay this figure show resected
00:17:08.880 access mechanism without enex M so we
00:17:12.760 use en and Rise application and M Cy and
00:17:19.360 par par is right weight for middleware
00:17:23.799 written by
00:17:25.280 par this figure is a complex
00:17:27.959 architecture because
00:17:30.039 it have much middleware and different
00:17:33.200 languages
00:17:36.880 so I replaced power bar to nxm
00:17:40.760 RV so I integrated integrated power
00:17:45.400 function into our NM Ruby so we can
00:17:49.720 develop with the only Ruby code now
00:17:57.120 so I introduce our code of data sharing
00:18:00.600 using M casd like this it is a pure R
00:18:05.039 Cod it is not specified mrv
00:18:12.880 code we have collection R issue at our
00:18:16.559 first
00:18:17.480 implementation so I put MD connection
00:18:21.880 into M handra section we Face the
00:18:25.960 corlection overl after that I discussed
00:18:29.840 this issue with mosan before he joined
00:18:34.039 to our company he soled this issue in
00:18:37.720 our few days
00:18:39.760 implemented by MV in it work we can
00:18:44.159 share the M casd connection use the MV
00:18:47.679 in it work
00:18:49.200 hand so NX M and the MV is the open
00:18:55.640 source
00:18:56.720 software so if you have uh any issue and
00:19:02.640 request so please submit uh giab or ESS
00:19:10.320 so we are developing
00:19:15.120 together it is a final version of our
00:19:18.159 sharing code we put connection process
00:19:22.120 into in work HRA like this and
00:19:26.720 disconnect it like this
00:19:30.559 mrb user
00:19:32.200 data is a user data class provides a
00:19:35.559 user data and user data
00:19:38.400 class it provides a global object in
00:19:42.080 engine XMR Vehicles H
00:19:45.159 hand we can share the connection via
00:19:47.880 this
00:19:51.280 code we resect some request with this
00:19:54.960 code it is simple Ruby code
00:19:59.159 we evalate request request PA and R
00:20:03.200 session
00:20:05.159 data like
00:20:08.120 this
00:20:12.440 so sharing with uh this session ID
00:20:20.440 key it is power comparison power and
00:20:25.480 XM XM is faster 10 to 20% J
00:20:31.520 power we got more Ruby code and high
00:20:35.080 performance with
00:20:38.640 nxv it's so
00:20:40.880 fast I prud it so I M said uh this
00:20:49.480 slide so next topic is testing so
00:20:53.679 testing is important so someone say test
00:20:57.080 is redundant
00:21:02.799 really yeah we should test every
00:21:05.960 production code in real
00:21:09.080 work I cannot find mrb use case with
00:21:12.559 testing code in result of Google
00:21:17.760 search so proty concept my first
00:21:21.120 implementation is these
00:21:23.600 concept we use CV without MV and and use
00:21:29.360 a test unit simple x unit TOs and so we
00:21:35.320 focus only ru's syntax not it is not 's
00:21:42.039 Behavior it is a target called over MV
00:21:45.520 it is the same as before
00:21:48.320 slides so we need to put mat D CR
00:21:52.200 because sh Doesn't Know M is wor so nest
00:21:58.679 and connection and all of damy attribute
00:22:02.760 and dammy
00:22:05.760 cross and it is a dam CR of mrb user
00:22:09.720 data and mrb
00:22:12.080 M so mrb M Cas D class name is a m Cas D
00:22:18.760 but sh is m d class is M Cas so it is
00:22:24.400 too Comfort to
00:22:26.520 me and so test go the is here it is
00:22:30.559 simple test case so setup context
00:22:33.400 creates uh di of request and connection
00:22:38.960 instance and require the target code and
00:22:44.480 Asser our production
00:22:46.919 cod with uh setup
00:22:52.799 context it is a test case of M casd
00:22:56.640 Class M casd sear so same as uh before
00:23:01.840 slide and uh I create a
00:23:06.400 MD instance and set a data
00:23:10.720 and moing
00:23:13.320 out uh cookie
00:23:16.520 data with uh session
00:23:22.960 key we can run test we can run this test
00:23:28.120 code with this one ler simple because so
00:23:32.919 we can test it test it back so we can
00:23:36.679 test MB
00:23:38.600 Cod however I have some concerns about C
00:23:43.480 testing sh testing is not real real
00:23:47.159 Behavior so many Mark and Stu so it's a
00:23:52.000 Ru tests the back
00:23:54.400 side so we try testing code over MV
00:23:59.039 using
00:24:00.559 MV I found unique test liary named MV m
00:24:05.159 test because it is M Gem we can put unit
00:24:10.400 test tool into M binaries so like this
00:24:14.200 we I put uh like this mrbm test and the
00:24:20.000 building binary and we can run uh this
00:24:24.840 this test
00:24:27.360 C I added test case into inline
00:24:31.039 production code for using MBM tests if
00:24:36.120 Mr binary has mrv mest class it invoke
00:24:41.520 the this
00:24:43.600 box it is a test
00:24:46.320 course if MV didn't have M class it
00:24:51.399 invokes our production C like
00:24:56.440 this we need need to separate MB binary
00:25:00.640 from NX MB this instruction shows only
00:25:04.200 build MB binary before ulated
00:25:10.279 enject I wrote this task gr for mrv
00:25:13.880 testing it is a simple R
00:25:16.720 task I'm going to upload this slide
00:25:19.520 after this presentation and please check
00:25:22.120 instruction
00:25:24.080 details so I can evoke this task rner
00:25:28.399 that are here mrb testing is three times
00:25:32.679 faster than C testing all of time is uh
00:25:37.679 7 microc seconds it's so
00:25:41.559 fast last topic is
00:25:45.520 deployment we need to prepare nxm binary
00:25:49.520 for production
00:25:53.240 use we use bu engine XML binary with DOA
00:25:58.320 container like this so
00:26:02.760 please show that my just
00:26:07.600 files you can get uh this complex do
00:26:12.919 file a copy and
00:26:15.360 pest this instruction are simple Docker
00:26:18.919 commands you can get nxm binary with you
00:26:23.120 have build a config via Docker easily
00:26:26.559 only two command like
00:26:30.960 this we put this binary into offal
00:26:34.320 engine packages so we replace only NXX
00:26:38.520 binary for NXX MV
00:26:41.159 department or replace only Eng
00:26:45.720 binary we are widely using puppet for
00:26:49.880 production Department in my
00:26:51.840 company this marest provides engine X
00:26:54.840 replacement for official engine X
00:26:57.200 packages
00:27:01.440 if you use a shi or an uh please write
00:27:07.919 uh like this I introduce production use
00:27:12.480 case over nxm RV and the test and the
00:27:17.360 deployment however we have many
00:27:20.760 challenges related
00:27:23.440 and we use binary with
00:27:27.000 mest it is different from production
00:27:30.880 binary I hope to use same binary test
00:27:35.399 and
00:27:37.039 production second we have no CS
00:27:41.240 over I think it is possible to prepare
00:27:44.279 cross compilation
00:27:46.200 issue we found
00:27:50.799 mrv rest we found the mvs solve no test
00:27:55.320 issue of NX configuration
00:27:58.240 I think NX MB provides the testable
00:28:01.279 world for to
00:28:04.919 us it mrb has a cross compilation cross
00:28:09.519 compile
00:28:10.880 configuration we are trying to build mrv
00:28:14.440 cross compare directory now it I think
00:28:18.919 it is a evalation
00:28:26.080 status I hope to to build uh more easy
00:28:32.279 to M binary like a
00:28:35.760 Goan so I show MV in the
00:28:39.880 future so MV ipbs is a IP buger solver
00:28:44.360 for MV we can use IP voice function
00:28:48.080 using a simple r c like
00:28:53.080 this next is MV C group motos makes it
00:29:00.120 it controls L group function via MV so
00:29:04.840 we can handle rower Revel function of
00:29:07.559 relux via MV and Ruby code even if you
00:29:12.679 can only write Ruby code you can control
00:29:16.320 the Reax
00:29:18.799 well so we should use em for expanding
00:29:23.720 Ruby bars thanks that's all
Explore all talks recorded at Red Dot Ruby Conference 2015
+18