realtime rails four2012.rubyworld-conf.org/files/slides/rwc2012_a-6.pdfallow concurrency (enable)...
TRANSCRIPT
![Page 1: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/1.jpg)
RealtimeRails Four
![Page 2: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/2.jpg)
Rails FourFor you and for me!
![Page 3: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/3.jpg)
Aaron Patterson@tenderlove
![Page 4: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/4.jpg)
HI!! :-)
![Page 5: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/5.jpg)
AT&T, AT&T logo and all AT&T related marks are trademarks of AT&T Intellectual Property and/or AT&T affiliated companies.
![Page 6: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/6.jpg)
Rails FourFor you and me!!!
![Page 7: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/7.jpg)
The Web!
![Page 8: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/8.jpg)
Ruby
Rails
Web
![Page 9: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/9.jpg)
Thread SafetyIn Rails
![Page 10: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/10.jpg)
Delete config.threadsafe!
![Page 11: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/11.jpg)
Why delete?
![Page 12: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/12.jpg)
Always bethread safe!
![Page 13: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/13.jpg)
Always bethread safe!
MyOpinion!
![Page 14: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/14.jpg)
Simplify
![Page 15: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/15.jpg)
Is it safe to remove?
![Page 16: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/16.jpg)
What did it do?
![Page 17: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/17.jpg)
threadsafe!
❤ Preload frameworks (enable)❤ Cache classes (enable)❤ Dependency loading (disable)❤ Allow concurrency (enable)
![Page 18: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/18.jpg)
Loading Code isn’t Thread-safe*
![Page 19: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/19.jpg)
Preload Frameworks
ENABLED
![Page 20: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/20.jpg)
Cache classes
ENABLED
![Page 21: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/21.jpg)
Dependency Loading
DISABLED
![Page 22: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/22.jpg)
Allow Concurrency
ENABLED
![Page 23: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/23.jpg)
Get Lock
Read from Socket
Process Stuff
Write to Socket
Release Lock
Rack::Lock
![Page 24: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/24.jpg)
Thread 2 Get Lock
Read from Socket
Process Stuff
Write to Socket
Release Lock
Thread 1
Rack::Lock
![Page 25: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/25.jpg)
Thread 2 Get Lock
Read from Socket
Process Stuff
Write to Socket
Release Lock
Rack::Lock
![Page 26: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/26.jpg)
Thread 2
Get Lock
Read from Socket
Process Stuff
Write to Socket
Release Lock
Rack::Lock
![Page 27: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/27.jpg)
Rack::Lock with Processes
![Page 28: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/28.jpg)
Rack::Lock withThreads
![Page 29: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/29.jpg)
Best Case:Extra Overhead
![Page 30: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/30.jpg)
Worst Case:1 req at a time
![Page 31: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/31.jpg)
IMPACT
![Page 32: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/32.jpg)
Boot time increases (in prod)
![Page 33: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/33.jpg)
Fewer middleware
![Page 34: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/34.jpg)
Multi-Proc serversstay the same
![Page 35: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/35.jpg)
Threaded serversJust Work™
![Page 36: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/36.jpg)
Bug Fixes
![Page 37: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/37.jpg)
100% Caching
![Page 38: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/38.jpg)
Locking ||=
def some_method @value ||= some_calculationend
![Page 39: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/39.jpg)
check then act
Is nil?
Calculate
Return
Set
![Page 40: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/40.jpg)
check then act
Is nil?
Calculate
Return
Thread 1
Set
![Page 41: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/41.jpg)
check then act
Is nil?
Calculate
Return
Thread 1
Set
Thread 2
![Page 42: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/42.jpg)
check then act
Is nil?
Calculate
Return
Thread 1 Set
Thread 2
![Page 43: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/43.jpg)
check then act
Is nil?
Calculate
Return
Set
![Page 44: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/44.jpg)
Fix #1 (eager init)class Foo class << self def hard_calculation @calc end end
@calc = fib(34)end
p Foo.hard_calculation
![Page 45: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/45.jpg)
Fix #2 (locking)class Foo @lock = Mutex.new
class << self def hard_calculation @lock.synchronize do @calc ||= fib(34) end end endend
p Foo.hard_calculation
![Page 46: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/46.jpg)
Fix #2 (locking)class Foo @lock = Mutex.new
class << self def hard_calculation @lock.synchronize do @calc ||= fib(34) end end endend
p Foo.hard_calculation
![Page 47: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/47.jpg)
Fix #2 (locking)class Foo @lock = Mutex.new
class << self def hard_calculation @lock.synchronize do @calc ||= fib(34) end end endend
p Foo.hard_calculation
![Page 48: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/48.jpg)
Move methods to instances
![Page 49: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/49.jpg)
Move to objectclass Foo def initialize @calc = fib(34) end
def hard_calculation @calc endend
Foo.new.hard_calculation
![Page 50: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/50.jpg)
Lazy Objectclass Foo include Mutex_m
def hard_calculation synchronize do @calc ||= fib(34) end endend
Foo.new.hard_calculation
![Page 51: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/51.jpg)
Maintain APIclass Foo include Mutex_m
def hard_calculation synchronize do @calc ||= fib(34) end end
Instance = new
def self.hard_calculation Instance.hard_calculation endend
Foo.hard_calculation
![Page 52: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/52.jpg)
Hash.new { }class Foo def initialize @cache = Hash.new { |h,k| h[k] = [] } end
def some_value(key) @cache[key] endend
![Page 53: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/53.jpg)
Fix #1 (lock)class Foo include Mutex_m
def initialize super
@cache = Hash.new { |h,k| h[k] = [] } end
def some_value(key) synchronize { @cache[key] } endend
![Page 54: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/54.jpg)
Fix #2 thread_safe
https://github.com/headius/thread_safe
![Page 55: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/55.jpg)
What about the App Level?
![Page 56: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/56.jpg)
Thread SafetyIn Web Apps
![Page 57: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/57.jpg)
Avoid shared data
![Page 58: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/58.jpg)
Most people don’t type “Thread.new”
![Page 59: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/59.jpg)
Look for things that are global.
![Page 60: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/60.jpg)
Global Variables
$so_global = {}$so_global[:foo] ||= “bar”
![Page 61: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/61.jpg)
Constants
SET_TWICE = 10SET_TWICE = 10
ALSO_GLOBAL = {}ALSO_GLOBAL[:foo] ||= “bar”
Warning
No Warning
![Page 62: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/62.jpg)
Class methods
class MyModel def self.some_cache @foo ||= fib(34) endend
![Page 63: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/63.jpg)
Avoid global data
![Page 64: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/64.jpg)
Add locks
![Page 65: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/65.jpg)
Streaming
![Page 66: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/66.jpg)
Template Rendering Today
![Page 67: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/67.jpg)
Templates Results are Buffered
![Page 68: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/68.jpg)
Clients are blocked while Rails works
![Page 69: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/69.jpg)
The entire page must fit in memory
![Page 70: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/70.jpg)
Rack Encourages Buffering
class MyApplication def call(env) [200, {}, [‘my page’]] endend
![Page 71: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/71.jpg)
We can do I/O and CPU in parallel.
![Page 72: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/72.jpg)
So why buffer?
![Page 73: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/73.jpg)
ActionController::Live
![Page 74: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/74.jpg)
Exampleclass BrowserController < ApplicationController include ActionController::Live
def index 100.times do response.stream.write "hello!\n" end response.stream.close endend
![Page 75: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/75.jpg)
Exampleclass BrowserController < ApplicationController include ActionController::Live
def index 100.times do response.stream.write "hello!\n" end response.stream.close endend
Mix in
Stream
![Page 76: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/76.jpg)
response.stream acts likean I/O object
![Page 77: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/77.jpg)
EverythingIs a File
![Page 78: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/78.jpg)
How does it work?
![Page 79: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/79.jpg)
Our APIdef index response.status = 200 response.headers[‘X-Whatever’] = ‘<3’ response.stream.write ‘hello’ response.stream.write ‘ world’ response.stream.closeend
![Page 80: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/80.jpg)
Rack API
def call(env) return [200, {‘X-Whatever’ => ‘<3’}, [‘hello world’]]end
![Page 81: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/81.jpg)
Wrapped Requestclass Response attr_accessor :status attr_reader :headers, :stream
def initialize @status = 200 @headers = {} @stream = StringIO.new endend
def call(env) res = Response.new controller.response = res controller.index [res.status, res.headers, res.stream]end
![Page 82: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/82.jpg)
Threaded action
def call(env) res = Response.new controller.response = res Thread.new { controller.index } [res.status, res.headers, res.stream]end
![Page 83: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/83.jpg)
Block until writedef call(env) res = Response.new controller.response = res Thread.new { controller.index } res.stream.await [res.status, res.headers, res.stream]end
![Page 84: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/84.jpg)
Block until writedef call(env) res = Response.new controller.response = res Thread.new { controller.index } res.stream.await [res.status, res.headers, res.stream]end
Block
![Page 85: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/85.jpg)
Blocking Bufferclass Buffer def initialize @latch = Latch.new @buffer = Queue.new end
def await # wait for write @latch.await end
def write(str) @latch.release @buffer << str endend
![Page 86: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/86.jpg)
Blocking Bufferclass Buffer def initialize @latch = Latch.new @buffer = Queue.new end
def await # wait for write @latch.await end
def write(str) @latch.release @buffer << str endend
`call` blocks here
![Page 87: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/87.jpg)
Blocking Bufferclass Buffer def initialize @latch = Latch.new @buffer = Queue.new end
def await # wait for write @latch.await end
def write(str) @latch.release @buffer << str endend
`write` unblocks
![Page 88: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/88.jpg)
What can we do?
![Page 89: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/89.jpg)
Rails Internals
![Page 90: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/90.jpg)
Streaming ERB
![Page 91: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/91.jpg)
View Source
# encoding: utf-8
require 'erb'
doc = ERB.new '<%= hello %> world'puts doc.src
![Page 92: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/92.jpg)
Source
#coding:UTF-8_erbout = ''; _erbout.concat(( hello ).to_s); _erbout.concat " world"; _erbout.force_encoding(__ENCODING__)
![Page 93: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/93.jpg)
Control Outputclass MyERB < ERB def set_eoutvar(compiler, eoutvar = '_erbout') compiler.put_cmd = "#{eoutvar}.write" compiler.insert_cmd = "#{eoutvar}.write" compiler.pre_cmd = [] compiler.post_cmd = [] endend
doc = MyERB.new '<%= hello %> world', nil, nil, '$stdout'puts doc.src
![Page 94: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/94.jpg)
Source
#coding:UTF-8$stdout.write(( hello ).to_s); $stdout.write " world"
![Page 95: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/95.jpg)
$ ruby test.rbhello world$
![Page 96: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/96.jpg)
Web Apps
![Page 97: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/97.jpg)
Infinite Streams
![Page 98: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/98.jpg)
Server Sent Events
![Page 99: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/99.jpg)
SSE ResponseHTTP/1.1 200 OKX-Frame-Options: SAMEORIGINX-XSS-Protection: 1; mode=blockX-Content-Type-Options: nosniffContent-Type: text/event-streamTransfer-Encoding: chunked
event: pingdata: {"ping":"2012-10-06T21:44:41-07:00"}
event: reloaddata: {"changed":["/Users/aaron/git/lolwut/app/views/users/"]}
![Page 100: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/100.jpg)
SSE ResponseHTTP/1.1 200 OKX-Frame-Options: SAMEORIGINX-XSS-Protection: 1; mode=blockX-Content-Type-Options: nosniffContent-Type: text/event-streamTransfer-Encoding: chunked
event: pingdata: {"ping":"2012-10-06T21:44:41-07:00"}
event: reloaddata: {"changed":["/Users/aaron/git/lolwut/app/views/users/"]}
![Page 101: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/101.jpg)
SSE ResponseHTTP/1.1 200 OKX-Frame-Options: SAMEORIGINX-XSS-Protection: 1; mode=blockX-Content-Type-Options: nosniffContent-Type: text/event-streamTransfer-Encoding: chunked
event: pingdata: {"ping":"2012-10-06T21:44:41-07:00"}
event: reloaddata: {"changed":["/Users/aaron/git/lolwut/app/views/users/"]}
![Page 102: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/102.jpg)
SSE ResponseHTTP/1.1 200 OKX-Frame-Options: SAMEORIGINX-XSS-Protection: 1; mode=blockX-Content-Type-Options: nosniffContent-Type: text/event-streamTransfer-Encoding: chunked
event: pingdata: {"ping":"2012-10-06T21:44:41-07:00"}
event: reloaddata: {"changed":["/Users/aaron/git/lolwut/app/views/users/"]}
![Page 103: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/103.jpg)
Client SidejQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control');
// if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1);});
![Page 104: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/104.jpg)
Client SidejQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control');
// if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1);});
![Page 105: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/105.jpg)
Client SidejQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control');
// if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1);});
![Page 106: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/106.jpg)
Client SidejQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control');
// if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1);});
![Page 107: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/107.jpg)
Real-Time BrowserCommunication
![Page 108: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/108.jpg)
![Page 109: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/109.jpg)
Puma
BrowserFS-Events
FS Events
![Page 110: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/110.jpg)
Puma
BrowserFS-Events
FS Events
![Page 111: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/111.jpg)
Puma
BrowserFS-Events
FS Events
![Page 112: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/112.jpg)
Puma
BrowserFS-Events
FS Events
![Page 113: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/113.jpg)
Puma
BrowserConsole
DRB
DB Events
![Page 114: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/114.jpg)
Puma
BrowserConsole
DRB
DB Events
Socket
![Page 115: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/115.jpg)
Other Input Sources
❤ Embedded systems (sausage box)❤ Telephony (twilio)❤ Other users (chat systems)
![Page 116: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/116.jpg)
Thread Safety
P72NStreaming
![Page 117: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/117.jpg)
Cores are increasing.
![Page 118: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/118.jpg)
We need to utilize the entire machine
![Page 119: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/119.jpg)
High latency clients are increasing.
![Page 120: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/120.jpg)
Patience is decreasing.
![Page 121: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/121.jpg)
Lie.Using cached data
![Page 122: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/122.jpg)
Cheat.Updating partial parts of the page
![Page 123: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/123.jpg)
Steal.Move computations to client side via JS
![Page 124: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/124.jpg)
Lie.Cheat.Steal.
![Page 125: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/125.jpg)
Be GoodEngineers
![Page 126: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/126.jpg)
THANK YOU
![Page 127: Realtime Rails Four2012.rubyworld-conf.org/files/slides/rwc2012_A-6.pdfAllow concurrency (enable) Loading Code isn’t Thread-safe* Preload Frameworks ED Cache classes ED Dependency](https://reader033.vdocuments.site/reader033/viewer/2022050513/5f9db31bd440c20ca71f6c56/html5/thumbnails/127.jpg)
Questions?