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
300
GolangでURLルーターをつくった
bmf_san
May 07, 2020
Tweet
Share
More Decks by bmf_san
See All by bmf_san
古くなってしまったPHPフレームワークとPHPのバージョンアップ戦略
bmf_san
1
130
アジャイルワークショップ
bmf_san
0
58
Makuakeの認証基盤とRe-Architectureチーム
bmf_san
0
2k
天下一HTTPRouter武闘会.pdf
bmf_san
8
4k
ゆっくりHackerRank
bmf_san
0
96
ハイ__ᐛ___パァ_テキストプリプロフェッ__ᐛ___サァ_.pdf
bmf_san
0
170
net/httpでつくるHTTPルーター自作入門
bmf_san
0
400
GoでRouter自作実装寄りな話
bmf_san
0
160
Golang_chromedp_slack_botでslackの絵文字自動生成ボットをつくってみた.pdf
bmf_san
0
89
Other Decks in Programming
See All in Programming
RaaP
ksss
0
160
GitHub Actionsの痒いところを埋めるサードパーティーランナー
dora1998
2
270
“Seeing Like a Programmer”—Resiliency, Limits, and Moral Hazards in Software Engineering (LambdaConf 2024)
chriskrycho
0
430
freeeのエンジニアが 就活で出そうな コーディングテストを 解説してみる
freee
1
170
戦略的DDDは重いのか? / Is strategic DDD heavy?
pictiny
3
2.1k
Ruby on Fails - effective error handling with Rails conventions
talyssonoc
0
300
Try creating your own orderedmap
kazamori
1
280
TypeScriptでもLLMアプリケーション開発 / LLM Application In Typescript
rkaga
5
1.3k
TypeScriptコードの漸進的改善 / Progressive Improvement of TypeScript Code
medley
1
450
Jetpack Composeとデザインシステム
rmakiyama
0
240
The test code generator using static analysis and LLM
mikik0
1
170
otelcol receiver 自作RTA / Pepabo Tech Conference #22 春のSREまつり
arthur1
0
1k
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
21
4k
jQuery: Nuts, Bolts and Bling
dougneiner
60
7.2k
Debugging Ruby Performance
tmm1
70
11k
How STYLIGHT went responsive
nonsquared
92
4.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
126
32k
The Illustrated Children's Guide to Kubernetes
chrisshort
32
47k
Bash Introduction
62gerente
605
210k
Clear Off the Table
cherdarchuk
86
310k
Git: the NoSQL Database
bkeepers
PRO
423
63k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
7k
Testing 201, or: Great Expectations
jmmastey
30
6.4k
Web development in the modern age
philhawksworth
203
10k
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