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
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
180
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
260
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
210
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
490
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
140
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
310
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
150
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
仕様駆動開発の消費期限
watany
16
7.2k
5分で問診!Composer セキュリティ健康診断
codmoninc
0
910
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
860
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
370
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.9k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.3k
The untapped power of vector embeddings
frankvandijk
2
1.8k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
56k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.9k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Into the Great Unknown - MozCon
thekraken
41
2.7k
Being A Developer After 40
akosma
91
590k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
430
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
66
57k
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 ͱ͔͖