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
phpアプリケーションをGo言語へ
Search
yuuki takezawa
November 03, 2016
Programming
9
9k
phpアプリケーションをGo言語へ
yuuki takezawa
November 03, 2016
Tweet
Share
More Decks by yuuki takezawa
See All by yuuki takezawa
PHPでアクターモデルを理解・体験しよう / Understand and experience the actor model in PHP
ytake
2
170
再考 アクターモデル/ reconsider actor model
ytake
0
580
GoとアクターモデルでES+CQRSを実践! / proto_actor_es_cqrs
ytake
1
270
Phluxorでアクターモデルを 理解・体験しよう / toolkit-for-flexible-actor-models-in-php-phluxor
ytake
1
210
オブジェクトのおしゃべり大失敗 メッセージングアンチパターン集 / messaging anti-pattern collection
ytake
2
980
DRE/SREのプラクティス融合によるクラウドネイティブなデータ基盤作り / dre_sre
ytake
0
690
技術的負債と向き合う取り組みでよかったもの / positive_efforts_to_tackle_technical_debt
ytake
10
3.8k
アプリケーションエンジニアから強いデータエンジニアへの歩き方 / How to transition and become a Data Engineer from an Application Engineer
ytake
1
490
入門 境界づけられたコンテキスト
ytake
6
4.1k
Other Decks in Programming
See All in Programming
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
Macとオーディオ再生 2024/11/02
yusukeito
0
370
距離関数を極める! / SESSIONS 2024
gam0022
0
280
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
役立つログに取り組もう
irof
28
9.6k
CSC509 Lecture 09
javiergs
PRO
0
140
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
910
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
CSC509 Lecture 11
javiergs
PRO
0
180
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
580
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
860
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
GitHub's CSS Performance
jonrohan
1030
460k
A Philosophy of Restraint
colly
203
16k
Making Projects Easy
brettharned
115
5.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Building Your Own Lightsaber
phodgson
103
6.1k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Transcript
phpΞϓϦέʔγϣϯΛ Goݴޠ go func() { messages <- "php" }() yuuki
takezawa phpcon 2016
GoݴޠΛ࠾༻͢Δ͜ͱʹͳͬͨഎܠ
ڊେͳαʔϏεͷݶք
ڊେαʔϏεʹΑΔ • 1Օॴͷमਖ਼͕க໋తͳΛҾ͖ى͜͢έʔε • ෳࡶԽ͢Δ༷ʹରԠ͢Δҝͷ࣮͕ύϑΥʔϚϯε ʹӨڹΛ༩͑࢝Ίͨ • ڊେʹͳΓ͗ͨ͢σʔλϕʔε • ୲֎ͷαʔϏε༷͕Θ͔Βͳ͚Εमਖ਼Ͱ͖ͳ͍
ঢ়ଶ • API͕͘͢ͳ͘ɺଟ͕͘σʔλϕʔεʹґଘ • ϏδωεͷมԽʹैͰ͖ͳ͍ঢ়ଶ • େྔσʔλͷόονॲཧ
ʹཱ͔ͪ͏ • σʔλϕʔεࢀরͷΓ͠ • APIͷඋ json, xml, etc… • ΫϥΠΞϯτͷॲཧΛ؆ུԽ͢ΔҝʹɺHATEOAS
(औΓΈத) • σʔλϕʔεࣗମͷׂ (ςʔϒϧͷ࠶ઃܭɺతผʹׂɺNoSQLซ༻) • ԣஅతʹؔΘΔαʔϏεͱͷ࿈ܞ (message queue, ฒྻॲཧ)
σʔλϕʔεࢀরͷΓ͠ • ιʔείʔυͷ͍ͨΔͱ͜Ζʹ͋ΔσʔλϕʔεࢀরΛ நԽɺDependency Injection • σʔλϕʔεଓΛߦ͍ͬͯΔίʔυ͔Βɺ ֎తཁҼΛΓ͠ • ΞϓϦέʔγϣϯߏΛ·ͣϨΠϠʔԽ
• ঃʑʹAPIࢀরͱΓସ͍͑ͯ͘ • PHPΞϓϦέʔγϣϯΛૄ݁߹ʹ
class PointBalance { /** @var PointRepositoryInterface */ protected $repository; /**
* @param PointRepositoryInterface $repository */ public function __construct(PointRepositoryInterface $repository) { $this->repository = $repository; } // snippet }
Controller / Action Domain Infrastructure MySQL API Elasticsearch Aspect
None
APIͷඋ • ଞαʔϏεͷࣝΛෆཁʹ (URLͷߏஙɺͲΜͳAPIͰͲΜͳ͜ͱ͕Ͱ͖Δͷʁ) • ϦΫΤετͷݮ • ΤϯυϙΠϯτʹಈࢺΘͣɺϝιουͰදݱ͢Δ • ڞ௨Ͱར༻͢ΔͷίϯϙʔωϯτԽ
• ݹ͍APIͳͲɺଟ͘ͷαʔϏε͕ґଘ͍ͯ͠Δͷͳ ͔ͳ͔͍͠ • ύϑΥʔϚϯε͕ϘτϧωοΫͳͷreplace
{ "id": 1598, "user_id": 123, "title": "ίϝϯτʹͨ͠Α", "content": "ϝʔϧ͕དྷͳ͍ΑͶ", "tags":
[ { "id": 5098, "name": "ඒന" } ], "created_at": "2016-10-12 17:31:50" }
{ "id": 3746, "user_id": 123, "title": "ςετ", "content": "aaa", "tags":
[ { "id": 6081, "name": "ΞϩϚΦΠϧ", "_links": { "self": { "href": "http://example.com/tags/6081", "method": "GET" } } } ], "created_at": "2016-10-14 16:57:37", "_links": { "self": { "href": "http://example.com/3746", "method": "GET" } } }
navi menu content content
ϑϩϯτͰར༻͞ΕΔAPI • ϑϩϯτʹग़ྗ͞ΕΔͷ͚ͩͰͳ͘ɺ ͦͷޙΖʹ߇͑Δͷ • Ϣʔβʔొใ • ϝʔϧૹ৴ • ͳʹ͔͠Βͷߦಈʹ͏Ճࢉݮࢉ
• ඇಉظɺϦΞϧλΠϜ • τϥϯβΫγϣϯεΫϦϓτ • ฒྻϦΫΤετʹ͑͏Δͷ • ϑϩϯτ͔ΒΓ͢͜ͱͰదࡐదॴͳݴޠ࠾༻
େྔͷϦΫΤετΛޮྑ͘ࡹ͖͍ͨ
Nginx + php-fpm Response time: 0.01 secs Transaction rate: 384.48
trans/sec
େ͖ͳͳ͠ • ΞΫηε͕ଟ͘ͳΕɺΛ૿͢ • opcacheಋೖ͠(PHP7)ɺߴͳϨεϙϯε • ࣮໘ͰͷόάΛ࠷খݶʹ৯͍ࢭΊ͍ͨ • PHP7ʹͳΓɺݫ֨Ͱ͋Δ͕ɺ ։ൃऀશһ͕ཧղ͍ͯ͠ΔΘ͚Ͱແ͍
• ϦΞϧλΠϜॲཧΛͱʹ͔͘ૣ͘ • ඞཁͳใΛଋͶͯฦ٫͍ͨ͠
γϯϓϧɺฒߦ͔ͭߴͰ੩తܕ͚ݴޠ
Go • webαʔόͳͲ͕ෆཁͰ୯ମͰಈ࡞͢Δ • جຊγϯάϧόΠφϦͷҝɺϦϦʔε͕༰қ • ίϯύΠϧ͕ૣ͘ɺΫϩείϯύΠϧ • ϝϞϦϦʔΫʹͳΓಘΔͷ͕ഉআ͞Ε͍ͯΔ •
1αʔόͰෳىಈͤ͞Δ͜ͱ͕Ͱ͖Δ • ίϯύΠϧͱ࣮ߦ͕ಉ࣌ʹͰ͖ΔͨΊɺ LLݴޠ։ൃऀͰ؆୯ • Ϋϥε͕ແ͍
// linux͚windows͚ͳͲ༷ʑͳOSʹରԠ $ GOOS=linux GOARCH=amd64 go build -tags=production app.go
Struct • ΫϥεͰͳ͍ • ໊લͱܕͷू·Γ • ઌ಄͕େจࣈͷͷpublic ͦ͏Ͱͳ͍ͷprivate •
ίϯετϥΫλଘࡏͤͣɺܧঝͳ͍ • ߏମΛຒΊࠐΜͰར༻͢Δ
type Item struct { Language } type Language struct {
Name string } Item{Language{"PHP"}}
func handler(w http.ResponseWriter, r *http.Request) { person := &Person{"yuuki", "takezawa"}
fullName(person) } func fullName(name UserName) { fmt.Println(name.Name()) } type Person struct { FirstName string LastName string } func (p *Person) Name() string { return p.FirstName + " " + p.LastName } type UserName interface { Name() string }
goroutine • ಉ࣌ʹෳͷλεΫΛॲཧ • ॲཧΛׂ͍ͯ͘͠ • εϨουͷΑ͏ʹ1εϨου্Ͱෳಈ࡞ • ಄ʹgo Λ͚ͭΔ͚ͩ
go func() • ͕ͪ࣌ؒൃੜͯ͠͠·͏Α͏ͳॲཧΛඇಉظͰ • ฒߦॲཧ
func f(from string) { for i := 0; i <
3; i++ { fmt.Println(from, ":", i) } } // ॱ൪อূ͞Ε·ͤΜ fun main() { go f("routine") go f("routine_one") go f(“routine_two") }
ฒߦͱฒྻ • ฒߦ ࣮ߦঢ়ଶΛෳอͯΔͳΒ • ฒྻ ෳͷಈ࡞Λಉ࣌ʹग़དྷΔ • GoݴޠͷฒߦੑΛө૾Խ͢Δ http://postd.cc/go_concurrency_visualize/
channel • <- (ҹͷํʹྲྀΕΔ) • goroutine ؒͰΓऔΓΛߦ͏ • phpͰ͍͜͠ͱΛΧόʔ͢Δ •
ΩϡʔΠϯάɺॲཧͪঢ়ଶͳͲΛ࡞Δ
func read(r io.Reader) <-chan message { lines := make(chan message)
go func() { defer close(lines) scan := bufio.NewScanner(r) for scan.Scan() { lines <- message(scan.Bytes()) } }() return lines }
defer ͱ panic • defer: ϝιου͕ऴྃ͢Δࡍʹίʔϧ͞ΕΔ • σʔλϕʔεͳͲͷஅͳͲॲཧͷԆʹ͏ • panicruntime
errorʹ͋ͨΓɺ ࣮ߦ͕͍͠߹ʹͷΈར༻͢Δ • panic࣌ʹdefer͕ίʔϧ͞ΕΔ • ΤϥʔॲཧΛ͢ΔͷͰ͋ΕerrorΛ͢ • panic͔Β͔ͳΒͣrecover()͢Δ
nginxΛͬͨϩʔυόϥϯγϯά
upstream go_http { server 127.0.0.1:8080; server 127.0.0.1:8081; keepalive 300; }
server { listen 80; server_name go.app; charset utf-8; access_log off; error_log /dev/null crit; location / { proxy_pass http://go_http; proxy_http_version 1.1; proxy_set_header Connection ""; } }
Nginx + go Response time: 0.00 secs Transaction rate: 2142.67
trans/sec
খ͞ͳͱ͜Ζ͔Βஔ͖͑Λ࢝Ίͨ • PHPͰ࣮ߦ͞Ε͍ͯͨେྔσʔλͷόονॲཧ ͔͔࣌ؒΓɺσʔλͷྔ͕૿͑Δ͜ͱͰߋʹ࣌ؒ ϝϞϦ͕৺(͕݅૿͑Δ͝ͱʹ૿Ճ) • Goʹஔ͖͑ɺϝϞϦ༻ྔ͕ܹݮ ͕࣌ؒॖ σʔλϕʔεʹΑͬͯ͋·ΓมΘΒͳ͍͜ͱ͋ͬͨ •
web։ൃʹదͨ͠ඪ४ϥΠϒϥϦ͕ଟ͘ɺ ίʔυಡΈ͘͢ɺ݁ՌҰ෦Ͱ࠾༻
ϑϨʔϜϫʔΫ echo
echo • ϚΠΫϩϑϨʔϜϫʔΫͰɺlumenzend expressive ͱಉ͡ײ֮Ͱ։ൃ͕Ͱ͖Δ • fasthttp͕ΦϓγϣϯͰ͑Δ • grace͕͙͢ʹ͑Δঢ়ଶͰɺͱʹ͔͘؆୯ •
ඞཁͳͷࣗͰΈࠐΉ • ܰྔͳAPIͰ͋Εൺֱత࣌ؒͰ࣮Մೳ
ಋೖલʹ࣮ફ • ϓϥΠϕʔτͰ։ൃ͍ͯͨ͠ΞϓϦͷAPIʹ·ͣಋೖ • ҆ఆՔಇͱ࣮͕ૣ͍ • OAuthαʔό࣮ • ߴෛՙͰඇৗʹ҆ఆ •
facebookgo/inject • ΦϒδΣΫτࢦϥΠΫʹ࣮͍͕ͯͨ͠ɺ Goͷ࡞๏ʹͷͬͨ΄͏͕γϯϓϧ • ϦϑϨΫγϣϯۃྗΘͳ͍
Go goroutine API API Database queue
վળ • ΑΓૣ͘ɺݎ࣮ͳΞϓϦέʔγϣϯ • ͦΕͧΕͷαʔϏεͷॲཧʹదͨ͠ͷΛಋೖ • ϦϦʔε࡞ۀͷ୯७Խ ίϯύΠϧͯ͠(CIͳͲ) • supervisord
• ύϑΥʔϚϯεվળ গͣͭ͠
๊͍͑ͯΔ • ڥઃఆͳͲͷڞ༗ viper? • όΠφϦʹͯ͢ͷσʔλΛ࣋ͨͤΔ͔ go-bindata etc • ϑϥάͱڥ͝ͱͷઃఆ
ιʔείʔυʹηΩϡΞͳใΛؚΊΒΕͳ͍ • ؆୯ͱݴͬͯͳ͔ͳ͔։ൃऀ͕૿͑ͳ͍
Request Response web server + php
$_SERVER net/http application server
phpΛิ͍ɺͦΕΛαʔϏεͱͯ͠׆͔͢
վળ࢝·͔ͬͨΓ ՝ղܾΛPHP+αͰ (దࡐదॴ)
PHPΞϓϦέʔγϣϯΛGoݴޠͱ࡞Δ