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
72
OpenCensusでcustom context propagationとexporterを書いた話 / OpenCensus with custom context propagation and exporter
takashabe
0
1.8k
社内ISUCONを開催した話
takashabe
0
1.7k
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
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.1k
AI 輔助遺留系統現代化的經驗分享
jame2408
1
190
The NotImplementedError Problem in Ruby
koic
1
810
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
210
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
Performance Engineering for Everyone
elenatanasoiu
0
140
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
250
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
A2UI という光を覗いてみる
satohjohn
1
140
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
9
870
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
190
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Docker and Python
trallard
47
3.9k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
390
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
310
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
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 ͱ͔͖