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
pubsub with concurrent
Search
takashabe
May 27, 2019
Programming
960
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
pubsub with concurrent
https://gounconference.connpass.com/event/129090/
の発表資料です。
takashabe
May 27, 2019
More Decks by takashabe
See All by takashabe
より良いターミナルでの生活を求めて
takashabe
0
73
OpenCensusでcustom context propagationとexporterを書いた話 / OpenCensus with custom context propagation and exporter
takashabe
0
1.9k
社内ISUCONを開催した話
takashabe
0
1.8k
ISUCON大反省会
takashabe
0
2k
gitのブランチ戦略
takashabe
8
6.1k
サルでもわかるgit
takashabe
0
1.6k
playで複数DBする
takashabe
0
1.6k
MySQLで高トラフィックに立ち向かう
takashabe
0
1.8k
GitHubの良さ
takashabe
2
2.3k
Other Decks in Programming
See All in Programming
Android CLI
fornewid
0
210
源内ハンズオン概要編
hideg
0
130
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
300
5分で問診!Composer セキュリティ健康診断
codmoninc
0
910
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.8k
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
プロポーザルを書いてもらう
pvcresin
0
290
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
220
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
670
Go 1.27 における memory allocation の高速化
andpad
0
110
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
210
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
610
Featured
See All Featured
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
45k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
270
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
450
The Limits of Empathy - UXLibs8
cassininazir
1
600
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
340
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
470
Transcript
Pub/Sub with concurrent Go(Un)ConferenceʢGo͋Μ͜ʣLTେձ 6kg @takashabe
whoami • Takashi Abe (@takashabe) • גࣜձࣾαΠόʔΤʔδΣϯτ ΞυςΫελδΦ • ࠂ৴ϓϩμΫτΛGoͰ࡞͍ͬͯ·͢
• Goྺ3͘Β͍ • ͖ͳඪ४ύοέʔδ `go` ύοέʔδͰ͢ • Macͷ`g`Ωʔ͕յΕ͍ͯͯGo͕ଧͪͮΒͯ͘ࠔ͍ͬͯ·͢
Cloud Pub/Sub Λͬͯಘͨ Subscriber WorkerͷݟΛհ
Cloud Pub/Sub is Կ • GCPͷpub/subαʔϏε • Subscriberͷ࣮ʹ͍͔ͭ͘ύλʔϯ͕͋Δ • Pull
• worker͕pub/sub APIΛcallͯ͠ϝοηʔδΛ ϑΣον͢Δ • Push • worker͕HTTPαʔόͱͯ͠ಈ࡞͢Δ • pub/sub͕ϝοηʔδΛPOSTϦΫΤετ͢Δ
Cloud Pub/Sub is Կ • GCPͷpub/subαʔϏε • Subscriberͷ࣮ʹ͍͔ͭ͘ύλʔϯ͕͋Δ • Pull
• worker͕pub/sub APIΛcallͯ͠ϝοηʔδΛ ϑΣον͢Δ • Push • worker͕HTTPαʔόͱͯ͠ಈ࡞͢Δ • pub/sub͕ϝοηʔδΛPOSTϦΫΤετ͢Δ
Cloud Pub/Sub is Կ https://speakerdeck.com/hlts2/sub-worker-framework- implementation?slide=6 ΑΓҾ༻
ࣄྫ 1 1ϝοηʔδͷॲཧʹ͕͔͔࣌ؒͬͯ٧·Δ
1ϝοηʔδͷॲཧʹ͕͔͔࣌ؒͬͯ٧·Δ • 1ϝοηʔδͨΓͷॲཧ͕͍࣌ؒ • ͔ͭಛఆͷSubscriber worker͕େྔͷϝοηʔδΛॲ ཧ͠Α͏ͱͯ͠͠·͏
• Receive Settingͷ࠷దԽ • goroutineͰฒྻॲཧ
Receive Settingͷ࠷దԽ https://github.com/golang/dep/blob/master/docs/assets/DigbyShadows.png ΑΓҾ༻ • σϑΥϧτ • MaxOutstandingMessages: 1000 •
NumGoroutines: 1 →1 worker͕େྔͷϝοηʔδΛอ࣋ͯ͠͠·͏ type ReceiveSettings struct { ... MaxOutstandingMessages int // ϝϞϦʹஔ͍͓ͯ͘ϝοηʔδ NumGoroutines int // ϝοηʔδΛॲཧ͢Δworker goroutine }
goroutineͰฒྻॲཧ • IOόϯυͳॲཧͳͲɺฒྻԽग़དྷΔͱ͜ΖΛͻͨ͢ ΒฒྻԽ
ࣄྫ2 ͋ΔଐੑΛχΞϦΞϧλΠϜͰҰճ͚ͩߋ৽ ͍ͨ͠
͋ΔଐੑΛχΞϦΞϧλΠϜͰҰճ͚ͩߋ৽͍ͨ͠ • ࠂ͕ݟΒΕͨ࣌ɺͦͷࠂʹඥͮ͘ଐੑΛߋ৽ • ݫີʹຖճߋ৽͢Δඞཁͳ͍ • 1͝ͱʹߋ৽͢ΔΑ͏ʹͨ͠ • σʔλετΞͷ࠷ऴߋ৽࣌Ͱ੍ޚ •
ॲཧதʹෳϦΫΤετ͕͋Δͱແବͳߋ৽͕Δ
golang.org/x/sync/singleflight Ͱॏෳߋ৽Λආ͚Δ
golang.org/x/sync/singleflight https://play.golang.org/p/h_lCoqJDp9s package main import ( "fmt" "sync" "time" "golang.org/x/sync/singleflight"
) func main() { var ( sg singleflight.Group wg sync.WaitGroup ) for i := 0; i < 10; i++ { wg.Add(1) go func() { // "key" ͝ͱʹಉ࣮࣌ߦΛߜΔ now, _, shared := sg.Do("key", func() (interface{}, error) { time.Sleep(time.Millisecond * 100) return time.Now(), nil }) fmt.Printf("%v: shared: %t\n", now, shared) wg.Done() }() } wg.Wait() }
golang.org/x/sync/singleflight 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1
+0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001: shared: true time.Now() ͷ݁Ռ͕ἧ͍ͬͯΔ
ࣄྫ3 όʔετੑ͕͋ΔSubscriberͷAPIίʔϧͰ όοΫΤϯυ͕ࢮ͵
όʔετੑ͕͋ΔSubscriberͷAPIίʔϧͰόοΫΤϯυ͕ࢮ͵ • ϝοηʔδΛड͚ͯόοΫΤϯυͷgRPCαʔόʹϦΫ ΤετΛૹΔαʔϏε • Publishྔ͕֎෦αʔϏεґଘ • ීஈ3,000RPSఔ • ͨ·ʹ20,000RPS͘Β͍
golang.org/x/time/rate ͰBackpressureΛ࣮͢Δ
golang.org/x/time/rate https://play.golang.org/p/HsnbmPfUZ_A package main import ( "context" "fmt" "sync" "time"
"golang.org/x/time/rate" ) func main() { limit := rate.Every(time.Second / 1) // τʔΫϯͷཷ·Δ limiter := rate.NewLimiter(limit, 1) // limitΛݩʹlimiterΛੜɻୈ2ҾτʔΫϯͷ࠷େஷଂྔ(burst) var wg sync.WaitGroup wg.Add(1) go func() { for i := 0; i < 10; i++ { limiter.Wait(context.Background()) fmt.Println(time.Now()) } wg.Done() }() wg.Wait() }
golang.org/x/time/rate 2009-11-10 23:00:00 +0000 UTC m=+0.000000001 2009-11-10 23:00:01 +0000 UTC
m=+1.000000001 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 େମ1ඵͣͭॲཧ͞Ε͍ͯΔ
·ͱΊ • Cloud Pub/SubΛGoͰͬͯૺ۰ͨ͠ඇಉظॲཧܥͷ ࣄྫʹ͍ͭͯհͨ͠ • Pub/SubʹݶͬͨͰͳ͍ • `golang.org/x/…` ύοέʔδsyncΛ͡Ίɺศརͳ
ͷ͕ଟ͍ͷͰͨ·ʹ८ճ͢Δͱָ͍͠ • golang.org/x/net/netutil ͱ͔͖