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
17
契約テストとPactについて
bmf_san
0
60
5分でわかるSLO
bmf_san
2
83
権限について考える
bmf_san
2
100
自作HTTPルーターから新しいServeMuxへ
bmf_san
3
1.7k
古くなってしまったPHPフレームワークとPHPのバージョンアップ戦略
bmf_san
1
360
アジャイルワークショップ
bmf_san
0
140
Makuakeの認証基盤とRe-Architectureチーム
bmf_san
0
2.6k
天下一HTTPRouter武闘会.pdf
bmf_san
8
4.3k
Other Decks in Programming
See All in Programming
API for docs
soutaro
4
1.7k
note の Elasticsearch 更新系を支える技術
tchov
9
3.5k
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
philipschwarz
PRO
0
160
ComposeでのPicture in Picture
takathemax
0
130
파급효과: From AI to Android Development
l2hyunwoo
0
160
Contribute to Comunities | React Tokyo Meetup #4 LT
sasagar
0
600
Vibe Coding の話をしよう
schroneko
14
3.7k
Flutterでllama.cppをつかってローカルLLMを試してみた
sakuraidayo
0
130
Laravel × Clean Architecture
bumptakayuki
PRO
0
140
生成AIで知るお願いの仕方の難しさ
ohmori_yusuke
1
110
一緒に働きたくなるプログラマの思想 #QiitaConference
mu_zaru
80
20k
Beyond_the_Prompt__Evaluating__Testing__and_Securing_LLM_Applications.pdf
meteatamel
0
110
Featured
See All Featured
A better future with KSS
kneath
239
17k
Writing Fast Ruby
sferik
628
61k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
120
52k
Adopting Sorbet at Scale
ufuk
76
9.3k
Typedesign – Prime Four
hannesfritz
41
2.6k
GitHub's CSS Performance
jonrohan
1031
460k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Automating Front-end Workflow
addyosmani
1370
200k
Into the Great Unknown - MozCon
thekraken
38
1.7k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
Fontdeck: Realign not Redesign
paulrobertlloyd
84
5.5k
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