Thursday, October 06, 2016

Aloha Ruby Conf 2012 - Keynote - Rails 4 and the Future of Web by Aaron Patterson

look around our Twitter account and the what was going on I saw some speakers
doing some last-minute beach time slide updating
I talked to some speakers they hadn't updated any other slides are looked at
it and our slides
attacked him on speaker who freaked out that he thought a session with 30
minutes a heartbeat
and didn't know what to do and apparently instead of cutting outside
view that a more slides
I'm not sure that really works out very well but I did run into
something special from our keynote speaker mister patterson
so was wondering what this meant and what
you could really do about this so
I'm guessing he must really want to spam
or he's a really big fan of spare so
what what was the situation here did you get to eat them yet mississippi
no that that's great because we have one for you right here
yes I thank you up
know if you can come on up
and I'll give you the spam a civic awesome
thank you and here time avoid II
thank you yes
span I this is the way we can let you come here and not be without spare
who's ready for they won here want me to get off stage. let this guy talk
I the ask you that
okay soul I'm going to talk about rails for and to the future
or as I like to call rails for for you and me
a I was told so
I was told that you're not supposed to introduce yourself when you're giving
I'm not very good at at speaking so I'll
I'm not gonna tell you my name is Aaron Patterson and
if you want to follow me on Twitter you can so I'm just for the record I'm not
telling you this stuff
okay I'm not introducing myself I just wanna say hi
everyone I'll first I got a star
and say thanks to some people well
I gotta say thanks to my employer AT&T without them I wouldn't be here
so thank you I also want to thank the conference organizers for
having a conference in a really awesome place I've never been to Hawaii before
this is my first time some
I'm super excited and the reason I'm really excited about it cuz I love
I love spam summer 30
thank you I'll also my my favorite TV shows are here like
a Dog the Bounty Hunter I really
like I was looking around for Dog the Bounty Hunter last night confined him
a and also like Magnum PI I love Magnum PI
a and you can see by my moustache I
and if you don't believe me I love Magnum PI you can feel like I name my
computers after
extent I care picked up
alone so I i want to tell you
I was so I was flying down now flying
here to why and I and go to the bathroom I was on a plane and the guy
in the same row on my left also to go to the bathroom so he went
went first a and then he came back and I went over to the bathroom and I
I got there and I look down on the floor there is a there is
a five-dollar bill on the floor migraine from the bathroom so I was like well he
must drop this money so pick up the bill go back to see
given the money and I like a must you must drop
so I go to the bathroom and a
come back and they're collecting donations on the aircraft for early
breast cancer awareness and if you donate money and then
they enter you in a row into a raffle and you can win like
you can win some prizes on the airplane so i sat down and the guy says to me
says he
a this isn't mine I didn't drop this
as like okay well it's not mine either and he's like 10 donated to the
you know 10 you donated to the
raffle thanks I said okay that's that's a great idea
don't know who's minuses will put it to a good cause so I donate it figuring
well I'm not gonna win any prizes
it's just going to get donated so that'll be great and then of course
i win and
had I was also the first one to choose the prize like I could have chosen a
bottle champagne
or lay chocolates are all the stuff that I didn't know what to do because it was
in my mind it
it I picked I think chocolates but now I feel like there's an imbalance in the
world like I have news
illegal chocolates I shouldn't have them so I don't know what to do with the
i think im just gonna I mean why hotel room
and not tell anybody that I watched I
but I guess it's too late now anyway
um I don't know if you can tell us but I'm insanely nervous on stage
and one other things that I have to do to a come for myself is
a I I told a friend of mine well I get super nervous what should I do
love speaking but I'm so nervous on stage what to do about it
and he said to me well when you're on stage you just need to think to yourself
what when Freddie Mercury do so every time I give a talk
I put this up and I try to think to myself or Freddie Mercury do
now most speakers like most speakers think well
the it's common knowledge just imagine the entire
imagine the entire audiences in their underwear well actually for me it's the
I imagine I'm on stage in my underwear a figure that out for a team-record
I anyway up
I also want to tell you have a cat and his name is
governments have puff puff Thunder Horse that this whole legal name
a we call it one column corby pop
though and I love him a lot like I love him
a lot and I folic he's the first Katona
pollak owning a cat you know it's gonna be awake ninety-nine percent Fun in the
Sun we're gonna go like riding bikes together
a gear ice cream you know
swing and the go swing in the park and to all stuff but it turns out that
basically ninety-nine percent
time he's sleeping so I try and take pictures of him but the only pictures I
can ever get
early him donning like he's about to go to sleep all the time
and just kills me ally I
so we never get to go bike riding together by anyway if you wanna see more
pictures of him donning you can follow them
patent quip so
alright so we're going to talk about rails for
for you and me a we're going to look at some features the rails for we're gonna
talk a little bit about
the future of the web now a
despite my once I'm not a television psychic
so I can't tell you exactly what's gonna happen in the future but I can talk
about where I think it's going and basically the point in those pockets to
get you guys
it get ideas flowing in you know among you guys talk about where I think we're
and then hopefully have you guys you know
pick up the ball and run with it so
know what we're gonna do yes we're gonna look at some behaviors in Ruby
and then we're going to look at some changes in Rails
I and then we're gonna look at how these changes in Rails interact with the web
so rest are
pretty close to the server and then move our way out to the client so
the first thing I want to talk about is concurrency in Ruby
are or parallelization and I liked
I like the shortness down but I'm not super good at spelling so
I have its P-fifty six then that's how I shorten it up
possibly misspelled I'm not sir a and
I know most have you mosey probably know about the serve at least
least heard about this the MRI hours a gill
or what is known as a global interpreter lock
and this lock prevents
I concurrent CPU execution so what that means practically is that we can
schedule to threads onto
to run on two different CP is the same time so if you want to have
an interpreter that able to do that I suggest you look at alternatives such as
J Ruby or rabin yes these are guilt-free
Gill three alternatives up
but I wanna share some good news and where's
where's Charley here know is not jerk
aka anyway
up I wanna share some good news with you the gills removed and one
okay so we can be we can be super happy about that
but up I also wanna share some bad news with you next
is that the deal was replaced with the GVL which is exactly the same thing just
has to be instructed in
I tell if you go arian the source code 219 you'll see many references to GVL
and that's what I did so you know
this this leads to the question well is
MRI useless for pain 32 n and
I mean obviously obviously gets its useless
I your programs to actually work it just seems like they work
I mean I don't like I don't know if there's really two much
too much of a difference to be made like if if your program seems like it's
working does that mean if actually working i think is the question probably
left for philosophers
people smarter than me but a anyway
wealth I want what I want to do I want to take a look at the impact on the
gmail on MRI
and see you like see what that means to us in our day-to-day routine basis
and the thing that I like to use for
a looking at how the GVL impacts
MRI is used to the Feb not to think once and the reason I like to do this is
because I used to work in
I am used to work in online advertising I don't know if any of you guys have
worked in online advertising but online advertising is basically all about doing
feminazis sequence calculations
so that's like that's how they decide
what ads to show to you so of course
how many lives have a health benefits for concert tickets
and I a so course I like to use this in all my benchmarks because I'm online
advertising so
alright run this on my machine noticed that those Higgins
I can't I'm not lying about Magnum PI I run the third thinks about
5.7 seconds on my machine okay it's like oh this sucks
I need to be able to deliver ads faster
so I mean to calculate my for the nineteen sequences in threads so
I've got about 4 CPU so my machines all
throw up four threads here in calculated from a notch sequence
a hopefully all them in parallel
and then I wrote this and it takes
exactly them out hot I
just about the same amount of time and the reason is because time spent in the
VM can be done in parallel
so hensons called GBL or global VM walk
so anytime whenever we're spending time in the virtual machine that can be
executed in parallel
so what do we know about us
a things to fix us our people use Jeremy Erben yes if they want to actually have
for unscheduled on multiple same views
but very common as what people do it has his multiple processors and this is what
you're doing you're running a Rails application would say like
unicorn right you're running a whole bunch of different roomie processors
so you can actually handle concur class on multiple CPU use on your machine
soldiers run multiple multiple repressed so
I want to look at another thing in this is a as a as an example using a slow
a slow web server are slow web service
we have a slow web service here because again like basically online advertising
is all about 7.2 sequences and slow web services
up so if you look at this is just super simple just prints of hello world
but the important thing is like we have a sleep here for half a second
so each request takes at least half a second to complete
and we're on our quiet here we have a client would just say okay well go
you know go fetch some data from the server and we run this
and it takes a little over two seconds and this makes sense because we are
we're doing for a classy just half a second
takes two seconds not surprising so
quite well we forgot
we totally forgot about the previous thing the previous
knowledge about the GBL like we're just somehow we forgot about it
haven't had a coffee suicide throws in the threads and then we remember all
this isn't gonna work out for us because
you know we can execute anything in parallel
so was appointed a doing threads
well we run in any way but it actually takes half a second this time so
we are able to execute this make requests for this web service even an
using roads and decrease the amount of time spent
now how did this or like how this work inside overby
all those different things there's different methods inside everywhere
we know where the interpreter knows well okay
you know nothing can actually happen inside the room TV and what we're trying
to read of the socket
can actually execute anything right there right there so what we're gonna do
in a release the GPL let other threads
execute on the CPU while we're waiting for day two of the socket
and then once we actually contained on the socket we're going to acquire the
GBL again
and enter back into the review
so there are certain things that we can do this we can do this trick with which
is like
I O any site any type of I'll operations
and for those of you who are writing see extensions or
looking into MRI's internals the threat the function you actually want to look
for is this function
RB thread blocking region this is the thing that
actually unlocks the GBL so you given a
you give it a function pointer it'll on what the GVL call your function execute
some code
and then require the Gmail you can actually use this function when you
doing things like
for example I crypto graffiti we're doing things that are like CPU intensive
but you're never actually gonna be
in the roomie virtual machine you're just doing
computations and see then you can use this function as well
so what does this mean
what does it mean that we can actually run things in parallel on MRI
well I mean from an advertising perspective obviously it means that we
need to build something I G is a service
or what I like to call at five as which is next big thing
yes i'm looking for investors now
so come talk to me after I
I promise to use your VCU wisely
I'll at but really what it means is it means two things:
it means that number one a block 2 p.m.
is a walk to me a it's always a mock me a
wat tam is what p.m. so if you find some livor that's like well we're using
fibers to make your stuff like super duper fast and in parallel
it's actually probably a lie it's just adding complexity
your code the VM can actually magically s
which out when using five so
the more important thing is that friends matter
if you're doing I on MRI threads matter
I'm guessing like many have you so
I'm guessing many of you have webapps probably or early summer
your code does Ohio I'm guessing
I be nice we didn't have to but
you know so a
even even entrepreneurs that have a gmail using threads important
and I think that web server is like
thread in web servers like Puma are going to become more important
because well we're doing i/o operations we should be able to do
to serve up CPU operations at the same time
so having solutions like
I multithreaded servers that are running a multi-process
should become more popular I would hope or using
guilt-free solutions like Jeremy with just a straight up thread in server
up would also do as well
so the next thing I want to talk about is thread safety in Rails
and I'm gonna talk about some other changes that
we've made in Rails to make it
more thread-safe I and what it means to you as developers
and what we had to do I want to talk about common problems we ran into
and how you can fix those things in your applications
so the first thing that we did in Rails was we deleted config not thread-safe
so when actually remove its its actually still there
it's just a no up see you can still call
and it'll probably in output a message saying something like cool story bro
there are different if a
so the question is well why don't why should we delete this
why should we delete this configuration option
like what what is the plan to leave that's why we wanted to leave this well
like in my opinion we should probably just always be writing
programs that are thread-safe so if that's the case then
it boggles my mind why we would have some particular flag looks like okay
now you should run thread-safe or no you shouldn't run thread-safe
and if you think about it so does that mean like are their branches in the code
words checking okay
is a thread-safe now are we thread safe if we are then let's do it in a
thread-safe matter
otherwise let's do it in a totally crazy man the could blow up
just doesn't like it just doesn't make sense to me so
this is my opinion why we should remove why we should remove this configuration
flag is just because
it's ridiculous up
but the other thing is that it simplifies the rails code base so we can
actually find those branches were saying okay well now let's do this in a
thread-safe where let's not this author and safe way we can eliminate those
branches and actually simplify the code
so is it is it safe to remove thread safe
the next question might be asking and in order to figure out the answer this
question we need to understand what what in turn things to do
so let's take a look at that what's worth a visit in
its etnies four different configuration options insider
rails and look at what you to those are up
but the first thing that I want to say is that I'll loading code is a
and I put a star there cuz that's not actually true a
require is now thread safe on trunk I think requires been thread-safe in
jeremie for a while
I'm not totally sure about that %uh but the thing is it doesn't actually matter
because well I'm sure many of you have seen warnings that are like
iight circular require is considered
dangerous you might have seen this might have seen it somewhere
the problem with that is that well if we decide to take out a lock
and do this do these requires a circular choir can lead to a deadlock
right and we don't actually want lead you not be very fun
if when we're bringing our Rails application just deadlocks
I wouldn't be too excited about that so
in Rails we just treat all code loading as not thread-safe
we say okay we're not going to do any any sort of threading now we're just
gonna load it all in the same thread
get it on so
configuration options the first one we enable
preloading frameworks and what this does says okay we're gonna load all over
rails into
reload up all over else now I'm not sure
like this rails typically lazy lazily loaded if you don't have this
set which means like okay I'm gonna reference why reference Active Record
than it actually gonna load up Active Record and
it also does this for all your code so on actually load your model files and
tell you actually reference icon Stan
and why the framework in your code is treated differently I don't actually
know why but this is one of the configuration options as
okay do it this way the next thing we do is we enable caching classes
this makes sense because well for gonna load up all over cold we don't want to
be reloading we know of
we know that it's an axiom that loading code is not thread-safe
so we don't want to be reloading code in production I would not be a good thing
will lead to deadlock are up tires not super excited about that
next thing we we do is we disable dependency loading and this is the
option that says okay when you reference a user constant like an application
constant like your user model or whatever we go lower up that user model
so we disable that because we're
hopefully preloading all your code so doesn't make sense to go on findings
confidence because
hopefully we've we've already found all or done
now the next thing the next option is we enable
concurrency we allow concurrency I am
this is my life any over it option
a oh and thou's allied to its actual
I hate this option so what is often does actually removes the middleware called
rap clock
or iraq clock does that wraps up your requests
so it says OK when request comes in
we're going to obtain a lock so thread one comes and get the lock
it reads from a thought the the socket and we enter your Rails application we
try to process stuff
both say we have a second thread that comes along its price to pay not locked
thread one already has a lock taken out
rate so throw tried to just sits there and health red one is done
right for soccer releases its lock and then threatened to
can say okay it's my turn so go through the entire process
and releases the lock finally so
with things like a multi-process set-ups like if you're running unicorn
a rack clock doesn't make any sense because you only have one thread in each
the only have one thread processing request so what's the point of taking
out a lock
if no other thread can acquire that lock then what is the point of the middleware
so if you're using a multi-process set up this simply in Joss
ads overhead to your application
so the other problem is that if you noticed in that
in the way that we process request their
it only allows one requested the time
so if you're running in a multithreaded server all the sudden
you can only do one request the time so what is the point of running a
multithreaded server
you can only do one at a time Bluetooth up you boot up your server and you're
like why is it only your
why is it only running one request some time I guess I need to start multiple
processes of my thread in server and nearly
wines up well there's a bit this is what
so in the best case
extra overhead is our best case scenario with rack lock
in our worst case we can only process one request that I'll
so I think that I think that options like this are default configurations
like this
is one of the reasons why nobody chooses
thread-safe servers they start the thread say server and they're like why I
can only process one request the time this
this web server sucks I'm gonna move on to something else
so what is the impact
I've removing thread-safe back we're just gonna say all right we're gonna
enable us for everything
it doesn't it doesn't matter anymore
the impact is that boot time will probably increase
so but i'm in production probably increase because we're gonna preload
alder Co
but the thing is you actually had to pay all that time before it's just that you
over the course of many or a few requests as your server warmed up
now it just happens all print
here middleware so you should have a slightly smaller stock size maybe
wondered to stack frame
but it is slightly smaller a multiprocessor as
should stay about the same so after you boot up paciencia
about the same the profile like
speed wise and threaded servers will just work
so now we don't have that anymore all that reminds me
a there is a there was any
survey out that was like who
rail surveilling what web server to use
you know it go who is that who has the most popular web server
and what I think is funny is that
don't think web Rick was listed anywhere in there
I don't know if you guys know web rick is but it's the web server that comes
with Ruby
I'll and if you don't specify the web server that you use when you deploy to
thoroughgoing you use WebWork
I think many people don't know that and the other interesting thing about web
is the it is a threat and web server
so it's I I'm pretty sure it's like the first
threaded Ruby web server anyway
so fun fact: that survey is probably wrong cuz I'm guessing there are many
people who have pushed
applications to broker without specifying web server and they're using
web reckon production
fabric good
anyway removing thread-safe was the only thing that we had to do to fix
are the only thing that we needed in Rails is a poor I am
threaded applications we actually had to fix bugs
where we're doing on say things and what I want to do is look at
some the common scenarios where we're having bugs in Rails all we had to do to
fix them
and hopefully you can use this look for these types of issues in your
application to infect spreading
threading bugs in your rails out so your jams
now it I guess we're kind of lucky because a hundred percent of our bones
were race conditions around cashing so we can actually have any deadlock
situations which was pretty nice
a so what we're gonna do
is look at a few different cashing race conditions and what you have to do to
fix them
people don't seem to notice this but or equals a former cashing
so you're catching the right hand side the statement you do some calculations
you or equals an assignment to some instance variable were lethally
instance variable and the way that this is a the way that this is a problem is
we say okay we have a check enact
race condition thread comes in and says okay is that instance variable male
if so let's calculate said it and return it
but the problem is well we're calculating that well we're calculating
that value
another thread come along and be like hey is this instance variable militants
as a yes
it is nil so now you have two different threads doing the same calculation twice
now one thing to note is that this particular operation is only dangerous
when you're sharing it among friends
so if you see this happening if you're sharing with data among threads at home
really where you have to worry about it and what you can do
and as you can eagerly initializer and of course I'm using Fibonacci sequence
as I said to not think once very important up
we eagerly initialize this this in San variable on the class
and we know that booting up booting application requiring files
is considered to be not thread-safe so we're guaranteed that this is only gonna
happen inside of one threat
so we print calculators Cash Store on the class and then we're going to go
the other fix a week and newsweek unlock so this requires that we add a new tax
I say okay we're gonna go synchronize on that new tax
calculate the Fibonacci sequence and then return it
so the other thing we can do is we can move this the instance methods
class methods are going to be shared among friends so depending on the
particular problem that you're trying to solve moving to an instance method may
be a better
may be a better solution see say well I'm not gonna share this instant among
threads we can actually
generate a new instance Perth right and we just do the calculation error
initializing we have a cash there
we can also do you know I'll lazy initialization if we want to using a
synchronized block
and initializing there now if you really really really need this to me in class
method another thing that you can do is
create a singleton store that on store that has a
constant and use that and actually we found that to be
fairly handy throughout the rail source because we can actually
instantiate that object and test it so dealing with singletons is kind of a
pain because maybe
if they stories typeof state you have to reset
that and it's just much easier to test against an instance or something
now the %uh another problem that we ran into was passed out new blocks
and this is this is kind of an insidious problem because
you don't notice that the method level that well this isn't gonna be
you just look at the method you're like well I'm pulling a I'm pulling a value
on the hatch
totally fine gotta be fine but actually what's happening as the hashes
doing the same the same issue that we had me or equals
the Orton all section and saying okay we have to check
then act do we have this key if we don't have the key then we need to go
calculate a value for
one thing we can do is synchronized around synchronize around key fetching
the other thing you can do is which
I recommend is getting this thread safe jam from
iight Charles and
I it like I have to say a
I wish that the type of stuff that Charles house in this Jan
was in Ruby standard library because I feel like
that's another problem with a thread safety in Ruby itself as we don't have a
lot on the permit is available to us
in a standard library like thread-safe hashes
for in safer areas I even some things like
I don't know future as lapses barriers those types of
those types love concurrency data structures just
are available to us in a spare library so I feel that
that is something that's keeping roomie developers from writing Francisco
so this is well angry but what about at the Apple
like I'm talking about all the stuff from the perspective of somebody working
on the framework itself
and I think suspect most people are actually working on Rails application so
what do we do it the rails
ap level well the answers actually very easy
actually really easy and you shouldn't be afraid of making your
your code thread-safe the main thing you need to do is avoid share data
want to learn how to spot where share data is happening in your application
actually pretty easy to eliminate that were put locks around
and the reason I say that this is the most important thing you need to
remember is because most people don't
actually pipe thread dot new in their applications is very rare
so if you're not actually spent spooling up new threads
then mostly what you need to do is watch out for shared a need to look for things
are global
or a look at a few things are global like this obviously if you using global
it is a global and probably shared among friends another global that you need to
watch out for Constance
conference are going to be shared among threads what kinda annoying as if you do
if you stop the constant twice are going to get a warning criminal complaints you
but you can actually
on mutated constant value like down here on the bottom and you won't get a
warning about that
you're modifying global data but you don't know
and I think the one the one that most common
our Class A level maths class methods like this you have to remember that
all these classes these classes are all shared among friends and when you set a
method on that class
that method is also shared among all your friends you need to be careful
about these
so like I said avoid avoid global data add
and avelox next thing I want to talk about
and streaming and to be honest this isn't
a from a new feature in Rails fourth actually we just trying to make
easier to use so we're gonna look at
will look up screaming and then will look at
temple rendering how temple rendering work so they will look at streaming and
look at
are some features be on rails for
so I want to look at Temple rendering from a high-level hi
very very high level perspective only process are
BRB templates results are buffered and stored into memory
and as such all the rails internals are built around
offering up this template and thence spitting it out the sock
spinning out to acquire now what sucks about this
is it means that clients are blocked well rails is working
so when somebody makes a request your web service and give me the index page
and rails like okay we're going camping at the index page
and sitting there churning way calculating the index page while the
client is just sitting there going
okee what am I gonna get some data when am I gonna get some data
when you could be sending data down to the client and letting them fax like
assets or process JavaScript in advance
so the client is blocked well rails is working
the other knowing thing about the way that temple processing is handles that
we have to fit the entire member
in the entire page into memory before we spend it out to the choir
usually this is fine but it means that were constantly resizing strings
and we'll see the process growing as a as it
a built up the paint and then hopefully reducing
and hopefully and then spitting out to the client
so most people
expect that they a have to return something from rack here
the effort turn the entire page I think iraq encouraged by offering if you look
at the rack API this is a rack application
that third parameter in the array is actually having a paint ball for itself
and the simplest in its simplicity %uh this API
makes it seem like well after at the buffer up the entire page before I send
it off to
I sent it off to acquire as we have actually return this.value up the stack
so think it's annoying about this as we know that even an MRI
even an MRI we can do I owe and CPU in parallel
so why are we both bring up this page what is the point about bringing up we
could be sending data down to the client actually getting some parallelization
p 37 and Alice
so this is where Afghan control alive comes in and
what this is a model that makes sense here controller and it gives you
and I O type and Ohio like
API to send data down to the point
and here's an example the the reason we the reason we stick with those models
today people are expecting the everything kids offered
and we don't want to break that assumption rails for we don't wanna
break your applications when you're operating so this is an
often feature so here's an example using at
just makes an action controller live and then you get this
stream object on your response object that you can write to it act like an IL
that means you have to close at like an i/o
it acts like an eye out so it's natural for us to do computations
with Ohio's so action control I've can see an object that quacks like an i/o
this I O API is important to me because
on UNIX the thumbs everything is a file
everything is a file so why are we training are output like a file as well
so I want to look at how this works how does that work
where a look at some of the internals and how to actually build this so this
is the API while we want something where we set the status
me something hatter's and we write out or stream and then we closed
that's all we wanna ideally that's what we would have a retail look like
now the problem is if we look at the rack API
a we don't we don't have that with iraq API so happen we accomplish this
X whatever people's heart well what we need to do is we need to wrap this up
a response object and have people right
right so where me wrap it up in response object now looks like this like here's
our response
but the problem is you know down there at the bottom that iraq application
sets a response on the controller then a call index and then returns back up the
but the problem is that this still doesn't stream
as doesn't solve our problem all were calling in to the controller waiting for
the controller to return and then returning backup
back so how do we fix like what do we do
well we can call the action inside a threat
I now we're seeing wire thread safety in Rails is important
we can call the action inside a thread and then return back up the stack
outside of that threat but this still isn't good enough because
you're looking at this carefully earthing yourself well home and
this could return back up the stock before
anything is actually happened inside the controller may be no means that the
response status maybe nobody said hatteras what are we gonna do so how do
we deal with this
ideally what we want to do. is wanna wait
right here all say okay we're gonna wait on the stream wait until something is
actually been written
and the way that we do this as we have a buffer class and this buffer class has
al- action at
and we we call a weight on the bar for class and not just blocks there and tell
somebody is actually written and you'll see me down here in the right method
that releases the latch and then we return back up the stack
okay cool so there is our there is our internal implementation and this is an
exact code
but it's a very similar inside the rail source code single look for less than it
should seem
familiar so cool but what can we do with us
what can we do with this streaming stuff well I wanna say like this
really excites me from a rails in terms perspective because we can use this to
build streaming your be
we already have streaming Erbe but this greatly simplifies the process
and we can see this by taking a look at how your business processing
so here we have in your be template output the source
and this is what the actual source of the Year be templates
so we see you thinking caps a bunch of stuff about what school is that we can
we can control the way that your beer right things out
and we can control the variable their rights to so in this example we're doing
is we're saying okay I want you to use the right method
and I want you to call the right method on standard out
so now we take a look at the temple source were actually writing standard
this is awesome because we can re factor rails internal some more
easily produce streams temple output this is the result of that
that template so house is cool and wet like
thats awesome for reals internals but how's the school for web applications
refactoring rails internal that fights me but maybe it doesn't excite you guys
very much
so let's take a look at how we can use it so bad web applications
one thing we can do is we can build infinite stream API server somewhere to
but I don't actually think that sucks as exciting as the other things that we can
do with us
most interesting thing to me now is using server sentiments
I don't know if you've seen service and events but they're basically infinite
where the browser whole will fire a javascript function every time
in Cesena an event the JavaScript API
so this is this is one that's the responsible plan to have this content
type of tax the vent stream
and every time the browser receives a particular event like this one
it'll fire a javascript function and pass this data payload
that javascript function so
this is the source this is the JavaScript
source to set up I NSIC
you can see here they this actually makes their quest back to your server so
it calls
makes her quest to slash control on a server and actually keeps up Sokoto
now we add an event listener on the reload events every time we get an event
named reload
the browser will execute a javascript function and we can have the javascript
function do whatever we want to
and in this particular instance I'm having just reload the
reload the page and I think this is cool because
we can have a the server notify the browser about events and actually have a
real time brother communication
so I wanna show a video for example this
we execute fire per server
well the pair's users yeah
and every time we change a file we can actually have that notify the server
salt and it's a well I say OMG I think
you know actually notify the browser hey you need to reload
some material we can have a watch assets to so few. changer CSIS
it'll automatically reloads change it down there
we can see the user's use background change the way that
L go away and
the other cool thing is we can do it from other processes to
so I can say well let's fire up the rails console or going to modify the
database so creating user here
and as soon as we create the user in notifies browser hey
dan change she need to reload or do whatever you want to do whatever we
define that javascript function to do
so how does this work
when the files that contain we use as a fence and when the file system changes
the file system notifies notifies our web server sank a
adverse events gems as hey something changed you to do something
and our Web Services okay cool we're gonna send an event down
on this control socket that we have with the browser
and the browser does its thing reloads now what's interesting is that all these
all the three boxes are actually within the same process these are all running
within on our web server
so yes all these three boxes are in the same process now the way that works with
the console that we're looking at that console example
and a little bit more tricky I we actually fire up a derp server inside a
your webserver and it opens up a socket
and the console opens up a sock it to our dear be server saying K
and across that that socket we can send events to the server
and have those send it down to a point
so other possible input sources besides
you know file system changing or database changing
thinking your mind turning our we can we can use embedded systems
for example I have my meat curing box
hooked tough to a web server we can use with telefony or
maybe other users for example chat systems like we can use this to build
oh I don't know IRC something like that
so we look at the three topics
and p72 n
Pearle Vision thread safety apparel vision and streaming
and up hopefully I was able to relate them all to each other
so you can see how into these early and inside your applications as well as
inside israel's
internals now
I think I think that cores are increasing
I don't think that this is a a stretch to say that
every time you buy new machines the cores are increasing on your machine
I just bought a MacBook Air and a new Mac Book Air and I'm up to four cores
and that's crazy
it's really awesome but the thing is we need to start utilizing the entire
which is why investigating things like
I doing parallelization with our virtual machines is so important
we do understand how we can make the most of these machines
the other thing I think is changing as we have high latency clients were signed
to get more and more highly in thin clients like people who are on there
really terrible
edge server edge connections on their cell phones
and we need to get data down to these clients as quickly as possible
having them away our server process template is unacceptable
we should be getting data down to them as soon as possible
the other thing i think is changing as that patient is decreasing
and I mean this among people like I think I think everybody's patient is
decreasing and personally I blame this on honey boo boo
up but
I think that people are expecting like they're becoming they're starting to
expect more
instantaneous response is from their web service we need to figure out how to do
and I think the way that we need to do this I was thinking about this the way
that we need to do this as we need to law
said when somebody is asking for a particular calculation we should be
using cached data where we can
we're not actually performing that calculation or catching were lying to
you about it
we need to cheat so updating partial pages
parcels on the page we don't think the whole thing you do requests were only
gonna update the PAR the Pangea changed
for cheating we're not actually worse returning the entire page and we also
need to steal
and I mean steel computation from our end users so moving
moving calculations in the JavaScript and having those calculated on the
on the client-side so
for the future what I want all you need to do
I want you all to why cheat and steal
in other words I want you all to be good engineers
thank you
if you have questions about this please come see me after I don't know if we
have time
four questions and I'm not sure what the protocol is on
questions here but if we have time I'm happy to take them if we don't
come find me so thank you
do we have time for questions are we gonna do that
0 aka five minutes
questions go and K
so I was wondering if we don't have questions I was wondering like so
are there play boutiques
spam places here why
by as their I want to try I wanna see
I can fine from Wake organic shade grown
Fairtrade spam try that
that exist said deep fried spam
South the pleasure questions questions
anyone yes company
I so the question is in temples learning how to handle
cuando I accept things that happened during
streaming and the answer is a
don't have any exceptions no
so there's a there's things we can do like spit out JavaScript to redirect to
or some some have tax but really the whole lacks
I have a good answer for you total ass yes
coming primary whose
rose time the question is about lying on the release candidate the rails for
a on any dates and I am your gummy Polo Grounds a
we thought we would have appeared outta beginning in September and obviously as
possible member but
I am I think more for me
mostly on plot now we're hoping to get
obeyed out shortly and the final before the end of the year
like so soon yes
other question is there a way to a convention in your queue system
a right now and
me yes
and no I yes because
yes because it's basically just hearing aid yeah I in really late
the only thing that's really an real support I is fancy
hash where you say can type object
so if you get a I think rescue
has well okay using in memory he was good enough rare than just music you
queue object if you need something that's he realized he like readiness
on Jeremy campers working on a queue cuing API
or rescue wraps around resting you can use it I'm not her
you can use that day by it will need out
along israel's or arms sidekick how's the QNAP I'm gonna try an experiment
branch so if you want to his attorney
went and I will help you
gets that yes
yes so people who are twenty for you know about future is by
a can talk about latches and barriers for second yes
and latch all act does his
say and allows you to coordinate according to different friends
you're basically saying well 13 you share the latter malval
roads and you say well okay a I want this thread to go to sleep
and tell this other thread has done some particular calculation when the other
thread is finished with its for type particular abolition in a long walk
it all all of the latch and let the other thread
let the other friend oh and simply yes like I
countdown watch for something like that say like well a
I need five different threads to better job for
back so discount on each other and then a
barrier is Evelyn barriers basically the same thing
except that you can also do cyclic barriers so basically means your lap
the thing about lapses for 15 years see you can't for you that
but if you have a sec where you can reset questions
a so the question is let me see
let me see if I'm getting this right a insert going to be a plan for
SSE's inside israel's that I
so giving and giving you like in the fall when you start up when you can
person starred for using SSE so the question is is there going to be a
like some certainty for people to be able to use as a scene from a fire
their web applications a the answer is there's no
there's no plan for that right now a
and also it's kinda hard because
kinda hard to do something like that because right now like action control
alive during spring as an often thing
it's not the whole are all here does not default all here controllers
my personal plan is I would actually like to make that
all I'm like the only factor in the end the rails of
were always using streams I think once we get them back point
then it would make sense to say like okay we're gonna give you know
easy same defaults for doing other things
right now it's like its told me I Y basically so
something I think would really help and what I
having I'm I'm going to admit I'm a really terrible person I've been copying
and pasting this
simple little I O object around between all my applications that
just spits out associates I write an object to it and it translates that
into chase on objects that out as a necessity
probably somebody should write them for that and
not me hope that answers your question
okay do we have time anything else
last question
am I gonna come to the spam champ summer I had didn't know there was hot
has awesome if I'm not somewhere else
yeah I totally as anyone else wants
yes alright thank you