Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
RustのWebフレームワーク周りの概観
hayao
July 20, 2022
Programming
0
180
RustのWebフレームワーク周りの概観
FutureCon2022での登壇資料です。
RustのWebフレームワーク周りの代表的なソフトウェアスタックであるaxum、hyper、tokio、mio、towerについて簡単に説明しました。
hayao
July 20, 2022
Tweet
Share
Other Decks in Programming
See All in Programming
FutureCon 2022 FlutterアプリのPerformance測定
harukafujita
0
140
FargateとAthenaで作る、機械学習システム
nayuts
0
170
Pythonによる開発をアップデートするライブラリの紹介
daikikatsuragawa
1
600
20220706_Google Apps Scriptを実演で学ぶ~ GAS × Slack ~
apachan
2
620
atama plusの開発チームはどのように「不確実性」に向き合ってきたか〜2022夏版〜
atamaplus
3
610
SwiftUIで「意図」を伝える / swiftui_intention
uhooi
2
150
Google IO 2022 社内LT会 / What's new in Android development tools
shingo_kobayashi
0
400
WindowsコンテナDojo: 第4回 Red Hat OpenShift Localを使ってみよう
oniak3ibm
PRO
0
180
アジャイルで始める データ分析基盤構築
nagano
1
900
Pluggable Storage in PostgreSQL
sira
1
190
Introduction to Property-Based Testing @ COSCUP 2022
cybai
1
150
夕食断食にTRY!/for-lt-12th
pachikuriii
0
240
Featured
See All Featured
Become a Pro
speakerdeck
PRO
3
910
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
7
1.1k
Music & Morning Musume
bryan
35
4.3k
Pencils Down: Stop Designing & Start Developing
hursman
113
9.8k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
107
16k
jQuery: Nuts, Bolts and Bling
dougneiner
56
6.4k
Mobile First: as difficult as doing things right
swwweet
213
7.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
6
570
Web Components: a chance to create the future
zenorocha
303
40k
GraphQLとの向き合い方2022年版
quramy
16
8.4k
Adopting Sorbet at Scale
ufuk
63
7.6k
The Brand Is Dead. Long Live the Brand.
mthomps
46
2.7k
Transcript
1 RustによるWeb開発のエコシステム
自己紹介 本田紘規 2021年4月新卒入社 金融系PJに従事 2
RustでWeb開発に触れたいきさつ ITほとんど未経験で入社 → 弊社はWebシステムの構築案件が多いので、Web開発の基礎を身に つける必要がある →Rustが好きだから、RustでWeb開発に入門しよう🤪 3
RustでWeb開発を行うメリット • GCがなく、C/C++と同程度に高速。 • 型付けが強く、コンパイラの補助も強力なので、安定したシステムを 構築しやすいかもしれない。 • クールな機能をたくさん持っているので生産性高く開発できる。 4
本発表で参照するクレートの関係 axum hyper Service tokio mio use use web framework
http server asynchronous runtime I/O library Routing ObjectをServiceに 変換してhyperに渡す 5
参照したバージョン axum 0.5.4 hyper 0.14.18 tokio 0.17.0 mio 0.8.0 tower
0.4.11 6
tokio axum hyper Service tokio mio use use web framework
http server asynchronous runtime I/O library Routing ObjectをServiceに 変換してhyperに渡す 7
tokio Rustのデファクトの非同期ランタイム。AWSの人が中心となって開発 していて、AWS内部でも使われている。 V1.0が出ていて成熟している。 関連クレートも充実しつつある。 • tracing • tokio-metrics •
tokio-console 8
tokio サンプル コード 9
Output 10
イメージ図 tokio runtime contextでspawnを呼べばtokioがtaskを良しなに実 行してくれる。 Thread Pool tokio runtime (multi
thread scheduler) spawn Future tokio runtime context SpawnにFutureを渡すとtaskが生成される block_onを呼ぶことでruntimeを起動 11
Futureトレイト 12
Futureトレイト Runtimeはpollを呼ぶことでtaskを進行させる。 pollはすぐに値を返すことが期待されていて • Poll::Pending • Poll::ready(val) のいずれかを返す 13
runtime task群の実行を管理 協調的スケジューリング taskがブロッキングI/Oを行って、ブロッキングしたり、busy loop に陥るのは厳禁 runtime全体がスタックする可能性がある。 ワークスティーリング idle状態のスレッドがbusyなスレッドからtaskを奪う 14
mio axum hyper Service tokio mio use use web framework
http server asynchronous runtime I/O library Routing ObjectをServiceに 変換してhyperに渡す 15
mio クロスプラットフォームI/Oライブラリ I/Oイベントを監視する。 I/O多重化 このI/Oイベント監視して! (複数イベント登録可能) mio register OS poll
Event loop 16
hyper axum hyper Service tokio mio use use web framework
http server asynchronous runtime I/O library Routing ObjectをServiceに 変換してhyperに渡す 17
hyper HTTPクライアント・サーバーライブラリ。SeanさんというAWSの人が開発 している。 高速であること、正確であることを重視しているらしい V1.0へのロードマップが公開されていて、V1.0になるのもそう遠くなさそう。 Web開発エコシステムにおける主な役割 • HTTPプロトコルによるクライアントとのやり取り • Serviceとソケットを紐づけ
18
axum axum hyper Service tokio mio use use web framework
http server asynchronous runtime I/O library Routing ObjectをServiceに 変換してhyperに渡す 19
axum tokioチームのdavidさんという方を中心に開発されているWebフレーム ワーク 2021年7月に発表された若いフレームワーク。現在も活発に開発が進め られている。 RouterとHandlerに注力している。 20
Routing 21
Handler 引数に欲しいものを書き並べていく方式 22
Handler トレイト 23
call 関数 ライブラリ使用者が実装したハンドラとcall関数の紐づけ call Request<B> handler URI Method Body res
Response (Pin<…>) from_request from_request from_request Into_response 24
tower axum hyper Service tokio mio use use web framework
http server asynchronous runtime I/O library Routing ObjectをServiceに 変換してhyperに渡す 25
tower Serviceトレイトを定義 雰囲気的にはJavaのServlet インターフェースのような もの axumとhyperの橋渡し 26
まとめ • RustによるWeb開発でaxumを使おうとすると、デフォルトでは hyper、tokio、mioが使われる。 • 基盤となるtokio、hyperは成熟していて、プロダクションで使えるク オリティ。 • Rustを使えば高速で安定したWebシステムを生産性高く構築できる かも?
27