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
GolangでURLルーターをつくった
Search
bmf_san
May 07, 2020
Programming
1
340
GolangでURLルーターをつくった
bmf_san
May 07, 2020
Tweet
Share
More Decks by bmf_san
See All by bmf_san
レーダーをつくる
bmf_san
0
16
契約テストとPactについて
bmf_san
0
58
5分でわかるSLO
bmf_san
2
78
権限について考える
bmf_san
2
98
自作HTTPルーターから新しいServeMuxへ
bmf_san
3
1.7k
古くなってしまったPHPフレームワークとPHPのバージョンアップ戦略
bmf_san
1
350
アジャイルワークショップ
bmf_san
0
130
Makuakeの認証基盤とRe-Architectureチーム
bmf_san
0
2.6k
天下一HTTPRouter武闘会.pdf
bmf_san
8
4.3k
Other Decks in Programming
See All in Programming
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
640
SEAL - Dive into the sea of search engines - Symfony Live Berlin 2025
alexanderschranz
1
130
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
240
The Weight of Data: Rethinking Cloud-Native Systems for the Age of AI
hollycummins
0
270
Making TCPSocket.new "Happy"!
coe401_
1
570
プロダクト横断分析に役立つ、事前集計しないサマリーテーブル設計
hanon52_
2
430
[NG India] Event-Based State Management with NgRx SignalStore
markostanimirovic
1
150
Youtube Lofier - Chrome拡張開発
ninikoko
0
2.4k
Rollupのビルド時間高速化によるプレビュー表示速度改善とバンドラとASTを駆使したプロダクト開発の難しさ
plaidtech
PRO
1
170
KawaiiLT 登壇資料 キャリアとモチベーション
hiiragi
0
110
Do Dumb Things
mitsuhiko
0
430
エンジニア未経験が最短で戦力になるためのTips
gokana
0
270
Featured
See All Featured
Embracing the Ebb and Flow
colly
85
4.6k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Speed Design
sergeychernyshev
29
890
Site-Speed That Sticks
csswizardry
5
480
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.3k
Into the Great Unknown - MozCon
thekraken
37
1.7k
It's Worth the Effort
3n
184
28k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.2k
Scaling GitHub
holman
459
140k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
178
53k
Statistics for Hackers
jakevdp
798
220k
Transcript
GolangでHTTP Routerをつくった @bmf_san
https://speakerdeck.com/bmf_san/urlruteinguwotukuruepisodo1
続編にして完結編
⽬次 ・HTTP Routerの仕組み ・データ構造 ・Golangでの実装
Github https://github.com/bmf-san/goblin
1 HTTP Routerの仕組み
HTTP Router is 何? ・リクエストされたURLやHTTP Methodに応じて、 処理の実⾏を制御するアプリケーション ・URLとアプリケーションの処理を結びつける
処理のイメージ
パターン Routerが解釈するルーティングのパターン ・静的なルーティング →/foo/barが/foo/barに⼀致 ・動的なルーティング →/foo/bar/123が/foo/bar/:idに⼀致
2 データ構造
データ構造を考える ・Router≒⽂字列マッチング ・URLの階層構造と相性が良いデータ構造 →⽊構造
⽊構造
トライ⽊(プレフィックス⽊) ・⽂字列の集合を扱う⽊構造の⼀種 ・ざっくりいうと、⽂字列を探索しやすいように⽊に 格納したやつ ・ex. サジェスト、IPアドレス探索、形態素解析とか に応⽤が⾒られる
トライ⽊を知る ・https://www.cs.usfca.edu/~galles/visualization/ Trie.html →アルゴリズムのビジュアライズ ・https://github.com/bmf-san/road-to-algorithm- master/tree/master/data_structures/tree/trie →参照実装かいた
オレオレトライ⽊ ・ルーティングの定義を元にオレオレトライ⽊をつく る(これが結構試⾏錯誤した..) ・トライ⽊の定義に当てはまるのかちょっと⾃信ない けど多分トライ⽊、いやギリトライ⽊だと思う
オレオレトライ⽊ • /foo/ • /baz/ • /foo/bar/ • /foo/:name[string]/ •
/foo/bar/:id[int] ˞ύϥϝʔλͷఆٛͷํ%4-ͳͷͰࣗ༝
ͦΜͳͰେৎ͔ʁ
⼤丈夫だ、問題ない。でも・・ ・時間的計算量(処理時間)、空間的計算量(メモ リ)をより最適化するなら、当然選択肢はある ・Radix Tree https://www.cs.usfca.edu/~galles/ visualization/RadixTree.html
3 Golangでの実装
GolangでのURLパターンマッチの 仕組み、お作法
net/http ・net/httpのインターフェースついて、HTTPServer を⽴てるコードの実装から内部処理を読んでおく →net/httpのmuxを拡張する ・cf. https://bmf-tech.com/posts/GolangのHTTP サーバーのコードリーディング
muxとは ・マルチプレクサ。多重器、多重装置、多重化装置、合波器。 ・2つの⼊⼒から1つの出⼒を得るもの ・雑にいうとURLのパターンマッチするやつ ・net/httpではServeMux構造体(URLパターンマッチやる 君) cf. https://golang.org/pkg/net/http/#ServeMux
net/httpのmuxの拡張 ・http.ListenAndServeから処理をたどる →addressとHandlerインターフェースを満たしたhandlerを引数に 取る。内部的には、handlerの呼び出し=ServeHTTPの呼び出しとな る cf. https://golang.org/src/net/http/server.go?s=96538:96593#L3074 ・ServeMuxはServeHTTPを実装しており、Handlerインターフェー スを満たしている cf.
https://golang.org/pkg/net/http/#ServeMux.ServeHTTP
)BOEMFS*OUFSGBDF 何を拡張すればいい? IUUQ-JTUFO"OE4FSWF "EESFTT )BOEMFS 4FSWF)551 ཁ͢Δʹ͜Εʂ
こんな感じ ・https://github.com/bmf-san/goblin/blob/master/ router.go#L60 ࣗલͷ63-ύλʔϯϚον
⾃前のURLパターンマッチの実装
オレオレトライ⽊の実装 ・オレオレトライ⽊を実装に落とし込む →トライ⽊の応⽤。パラメータ部分の実装がちょっ と⾯倒 ・コアの実装 https://github.com/bmf-san/goblin/blob/master/trie.go
再帰処理との闘い ・つらい ・テストとデバッガで頑張れば 乗り越えられる
Example ・参照実装 https://github.com/bmf-san/goblin/blob/master/_examples/main.go ・トレイリングスラッシュは気にしないで⼤丈夫なは ず ・静的ファイルはhttp.ServeFile使って問題なく返せ るはず
ベンチマーク ɾࢀর https://github.com/bmf-san/goblin#benchmark →ͪΌΜͱੳͰ͖͍ͯͳ͍͚Ͳɺ΅ͪ΅ͪͳײ͢͡Δ ɾgoRouterͷϕϯνϚʔΫੳ https://github.com/julienschmidt/go-http-routing-benchmark
Issue・PR・Star
参考 ・雑まとめ GolangͰgoblinͱ͍͏URLϧʔλʔΛࣗ࡞ͨ͠ URLϧʔςΟϯάࣗ࡞ೖɹΤϐιʔυ̍ URLϧʔςΟϯάࣗ࡞ೖɹΤϐιʔυ̎ GolangͷHTTPαʔόʔͷίʔυϦʔσΟϯά A Trie implementation in
Golang