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
RustのWebフレームワーク周りの概観
Search
hayao
July 20, 2022
Programming
0
470
RustのWebフレームワーク周りの概観
FutureCon2022での登壇資料です。
RustのWebフレームワーク周りの代表的なソフトウェアスタックであるaxum、hyper、tokio、mio、towerについて簡単に説明しました。
hayao
July 20, 2022
Tweet
Share
Other Decks in Programming
See All in Programming
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
500
導入から5年が経って見えた Datadog APM 運用の課題
bgpat
2
540
オブジェクト指向コードレビューの新しいアプローチ
akkie76
3
1.5k
もうすぐ新年度、Babylon.jsがお勧めな3個の理由
hideg
0
160
ISUCONってなんだか難しそう……!!でも、初めてのISUCONにPHPで挑戦してきました!
kotomin_m
1
300
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
hirobe1999
0
840
Ruby製社内ツールのGo移行
bgpat
2
260
IntelliJ IDEA を知らなかった 自分に教えたい小ネタ集 / IntelliJ IDEA Hints for My Past Self
mackey0225
3
170
RubyVM を PHP で実装する 〜Hello World を出力するまで〜
memory1994
PRO
1
490
incrementalモデルの理解を深める
ikkimiyazaki
2
640
Kotlinを用いたDSL的な設計手法と使用上の注意
kohii00
3
530
object-oriented-conference-2024
fuwasegu
6
1.9k
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
350
18k
The Invisible Side of Design
smashingmag
293
49k
Ruby is Unlike a Banana
tanoku
95
10k
Designing the Hi-DPI Web
ddemaree
275
33k
Designing for humans not robots
tammielis
247
25k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
185
15k
Code Review Best Practice
trishagee
54
15k
A designer walks into a library…
pauljervisheath
199
23k
Bash Introduction
62gerente
604
210k
Docker and Python
trallard
33
2.6k
It's Worth the Effort
3n
180
27k
Building Flexible Design Systems
yeseniaperezcruz
317
37k
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