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
630
RustのWebフレームワーク周りの概観
FutureCon2022での登壇資料です。
RustのWebフレームワーク周りの代表的なソフトウェアスタックであるaxum、hyper、tokio、mio、towerについて簡単に説明しました。
hayao
July 20, 2022
Tweet
Share
Other Decks in Programming
See All in Programming
HTMLの品質ってなんだっけ? “HTMLクライテリア”の設計と実践
unachang113
4
2.3k
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
410
速いWebフレームワークを作る
yusukebe
5
1.7k
アセットのコンパイルについて
ojun9
0
110
Kiroで始めるAI-DLC
kaonash
2
560
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
830
TROCCO×dbtで実現する人にもAIにもやさしいデータ基盤
nealle
0
1.2k
1から理解するWeb Push
dora1998
7
1.7k
Jakarta EE Core Profile and Helidon - Speed, Simplicity, and AI Integration
ivargrimstad
0
350
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
1
580
個人軟體時代
ethanhuang13
0
310
AI時代のUIはどこへ行く?
yusukebe
16
8.2k
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
200k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
What's in a price? How to price your products and services
michaelherold
246
12k
The Invisible Side of Design
smashingmag
301
51k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Statistics for Hackers
jakevdp
799
220k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Thoughts on Productivity
jonyablonski
70
4.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
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