Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby & Friends - Taking Go as an example
Search
Richard Lee
April 26, 2014
Technology
3
570
Ruby & Friends - Taking Go as an example
Presented at RubyConf TW 2014
Richard Lee
April 26, 2014
Tweet
Share
More Decks by Richard Lee
See All by Richard Lee
LIFF SDK 的開發者體驗與實用秘訣
dlackty
0
87
Account Kit after 1 year
dlackty
0
58
【Modern Web 2015】愛料理如何打造產品及技術團隊
dlackty
0
40
Chef & Immutable Infrasturcture
dlackty
7
390
軟體上線之後的營運管理
dlackty
8
610
Using CocoaPods for Objective-C Library Management
dlackty
1
330
Does OpsWorks work?
dlackty
10
470
打造愛料理開發及營運團隊
dlackty
79
7.7k
Ruby Toolbox for DevOps
dlackty
22
1.2k
Other Decks in Technology
See All in Technology
非機能品質を作り込むための実践アーキテクチャ
knih
5
1.5k
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
270
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
290
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
200
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
170
podman_update_2024-12
orimanabu
1
280
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
160
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
120
多領域インシデントマネジメントへの挑戦:ハードウェアとソフトウェアの融合が生む課題/Challenge to multidisciplinary incident management: Issues created by the fusion of hardware and software
bitkey
PRO
2
110
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
37
15k
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
66
4.5k
Into the Great Unknown - MozCon
thekraken
33
1.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Designing for humans not robots
tammielis
250
25k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Optimizing for Happiness
mojombo
376
70k
Transcript
Ruby & Friends Taking Go as an example
... In this talk, I'm going to present several ways
to make such two-way communications between Ruby & Go language.
Richard Lee • CTO @ Polydice, Inc • iCook.tw dev
& ops • Open source lover • @dlackty everywhere
Agenda 1. Why Go language? 2. Go with Ruby using
asynchronous workers 3. Remote Procedure Code 4. Advanced tools
Go Why choose another language when you have Ruby? 1.
Static typing 2. Compiled language 3. Higher level concurrency abstraction (goroutines) 4. No object oriented design
Use cases 1. Command line tools - Heroku's hk 2.
System softwares - Docker 3. DevOps tools - ngrok / packer
Heroku's CLI benchmark ## version $ time heroku version >/dev/null
real 0m1.813s $ time hk version >/dev/null real 0m0.016s ## list $ time heroku apps >/dev/null real 0m3.830s $ time hk apps >/dev/null real 0m0.785s
You don't really understand a language until you use it
in production
Then you might want to use Go in your system
with Ruby
My First Law of Distributed Object Design: Don't distribute your
objects — Martin Fowler
Async processing Use Redis or any message queue based processing.
Resque / Sidekiq friends: • From Ruby to Go • go-workers • goworkers • go-sidekiq • From Go to Ruby • go-resque
go-workers for Resque in action func myFunc(queue string, args ...interface{})
error { fmt.Printf("From %s, %v", queue, args) return } func init() { goworker.Register("MyClass", myFunc) }
...and in Resque class MyClass @queue = :myqueue end Resque.enqueue
MyClass, ['hi', 'there']
Async processing (cont'd) • Pros: • Not fast enough but
quite reliable • easy to scale out • Cons: • Async • Additional setup for queues
Be careful about typing // Expecting (int, string) func myFunc(queue,
args ...interface{}) error { id, ok := args[0].(int) if !ok { return errorInvalidParam } name, ok := args[1].(string) if !ok { return errorInvalidParam } doSomething(id, name) return nil }
Compared to Ruby code class MyClass @queue = :myqueue def
self.perform(i, str) doSomething(i, str) end end
Performance boost • Benchmarked using matrix multiplication a = Matrix[...]
b = Matrix[...] c = a * b puts a * b • 9x faster than Resque • 4x faster than Sidekiq
In some cases, you might need immediate result Then you
need RPC
Remote Procedure Calls Just like function invokation but remotely •
Dynamically-typed • JSON-RPC • MsgPack-RPC • Statically format • Protobuf • Thrift
Dynamically-typed Which means: 1. No need to predefine data format
2. No generated codes for both client & server 3. Quite easy to migrate
JSON-RPC 1. Everybody love JSON! 2. Golang has built-in library
3. Ruby has nice wrappers
JSON-RPC Protocol Can be built on simple TCP connection or
HTTP Request: {"method": "echo", "params": ["Hello JSON-RPC"], "id": 1} Response: {"result": "Hello JSON-RPC", "error": null, "id": 1}
It's demo time.
Client sock = TCPSocket.new "localhost", "5566" call = { method:"RPCMethods.Say",
params:[{"text"=>"Hello, world!"}], id:rand(100) } sock.write JSON.dump call JSON.load sock.readline
Server func (m *RPCMethods) Say(args Args, results *Results) error {
results.Text = args.Text fmt.Println(results.Text) return nil // no error } func (m *RPCMethods) Ping(args Args, results *Results) error { results.Text = "Pong" fmt.Println(results.Text) return nil // no error }
MsgPack 1. MsgPack is efficient binary serialization format 2. Quite
similar to JSON but with binary type 3. Nice packages for most language
MsgPack-RPC 1. MsgPack-RPC is ...RPC using MsgPack 2. Again, nice
packages for different languages
It's demo time.
Client code cli = MessagePack::RPC::Client.new("127.0.0.1", 5566) cli.timeout = 5 v
= cli.call(:echo, "Ruby Conference Taiwan!") cli.close
Statically-typed Which means: 1. Predefine format with special language (IDL)
2. Usually with generated codes 3. Good when you have nice plan
Protobuf 1. Widely used by Google 2. Built in with
Golang 3. However, there's no official RPC mechanism
Thrift 1. Open sourced by Facebook 2. Officially support a
wide range of language 3. Have built in RPC mechanism
Thrift IDL RpcService.thrift namespace go demo.rpc namespace ruby demo.rpc enum
TweetType { TWEET, // 1 RETWEET // 2 } struct Tweet { 1: required i32 userId; 2: required string userName; 3: required string text; 4: optional Location loc; 5: optional TweetType tweetType = TweetType.TWEET }
Thrift in aciton 1. Usually your first start from defining
you service in IDL 2. Generate server / client code using thrift commands 3. Copy that to both side and integrate
But wait, why not just use REST?
RPC v.s. HTTP / RESTful APIs • Persistent connection •
Smaller in size • Easier to implement
Conclusion
Most importantly, learning a new language is always fun and
inspiring
Thank you! Let's Keep in touch Twitter / GitHub: @dlackty