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
2相コミットなどの実例に見るゴルーチンとチャネルの使いどころ
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
kojiaomatsu
November 13, 2021
Technology
380
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
2相コミットなどの実例に見るゴルーチンとチャネルの使いどころ
kojiaomatsu
November 13, 2021
More Decks by kojiaomatsu
See All by kojiaomatsu
FunctionBuildersから見るSwiftUIの文法
kojiaomatsu
0
170
Other Decks in Technology
See All in Technology
GitHub Copilot 最新アップデート – 「一歩先」の実践活用術
moulongzhang
5
1.6k
【2026年版】 ベクトル検索とEmbedding最前線
mocobeta
23
6.9k
Flow 不死:AI 時代 DevOps 的不變本質
cheng_wei_chen
2
440
スタートアップにAmazon EKSは早すぎる? マルチプロダクト戦略を加速する Platform Engineeringの実践 / Is Amazon EKS Too Soon for Startups? Practical Platform Engineering to Accelerate a Multi-Product Strategy
elmodev09
1
1.3k
Lightning近況報告
kozy4324
0
220
「軸足」は 固定しなくていい - 熱量と強みで描く、しなやかなキャリアの形
kakehashi
PRO
1
190
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
3k
徹底討論!ECS vs EKS!
daitak
3
1.2k
コミットの「なぜ」を読む
ota1022
0
110
iAEONの段階的リアーキテクト戦略 / iAEON's_Gradual_Re-architecture_Strategy
aeonpeople
0
240
自分が詳しくない領域でAIを使う #プロヒス2026
konifar
20
7k
Bucharest Tech Week 2026 - Guardians of the Cloud-Native Galaxy
edeandrea
PRO
0
130
Featured
See All Featured
Navigating Team Friction
lara
192
16k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
GitHub's CSS Performance
jonrohan
1033
470k
Become a Pro
speakerdeck
PRO
31
6k
A Soul's Torment
seathinner
6
3k
Optimizing for Happiness
mojombo
378
71k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Typedesign – Prime Four
hannesfritz
42
3.1k
Are puppies a ranking factor?
jonoalderson
1
3.6k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
Transcript
2 Koji Aomatsu 2021 . 11 . 1 3 .
None
None
# 2 視 視 1 : N
# iOS Go Go # GO API
None
# Cloud SQL Datastore # DB # 2
# 2 Begin, Prepare, Commit XA Transaction , 2 MySQL
PostgreSQL
# 視 DB 規 # 視
channel goroutine 1 goroutine 2 視 Datastore Cloud SQL
# 視 視 sync.WaitGroup 規 // Cloud SQL -> Datastore
// ॲཧ201͕ऴΘͬͨ͜ͱΛΒͤΔ c1 := make(chan int) // Cloud SQL -> Datastore // ॲཧ202Ͱબ͞Εͨঢ়ଶΛ͑Δ c2 := make(chan int) // Cloud SQL -> Datastore // Prepare (err != nil ͳΒࣦഊ͍ͯ͠Δ) dbPrepareCh := make(chan error) // Datastore -> Cloud SQL // DatastoreͷτϥϯβΫγϣϯ݁Ռ dbTxResultCh := make(chan error, 1)
channel goroutine 1 goroutine 2 視 Datastore Cloud SQL
// Datastore go func() { defer close(dsTxResultCh) begin() doSomething(101) <-c1
doSomething(102) state := <-c2 doSomething(state) // Prepare err := <-dbPrepareCh if err != nil { dsTxResultCh <- err rollback() } else { err = commit() dsTxResultCh <- err } }() # defer 視 dbPrepareCh
// Datastore go func() { ... doSomething(101) _, ok :=
<-c1 if !ok { rollback() return } doSomething(102) state := <-c2 err := doSomething(state) if err != nil { rollback() // ϒϩοΫΛղআ͢Δඞཁ͕ग़Δ <-dbPrepareCh return } ... }() # select
channel goroutine 1 goroutine 2 視 Datastore Cloud SQL
// Cloud SQL go func() { defer close(c1) defer close(c2)
defer close(dbPrepareCh) begin() doSomething(201) c1 <- 0 doSomething(202) c2 <- SomeState err := doSomething(203) dbPrepareCh <- err err = <-dsTxResultCh if err != nil { commit() } else { rollback() } }() # defer dbPrepareCh Prepare dsTxResultCh Datastore
# #
# 規 規
None
# API # 視 #
# API 30 GAE 10% # runtime.NumGoroutine() 10%
# // 3000ඵ͔͔Δ func handle1() { for i := 0;
i < 1000; i++ { send(i) } } // 3ඵͰऴΘΔ func handle2() { for i := 0; i < 1000; i++ { go send(i) } } func send(num int) { time.Sleep(3 * time.Second) fmt.Printf("sent %v\n", num) }
# func handle() { c := make(chan int, 300) go
func() { defer close(c) for i := 0; i < 10000; i++ { c <- i } }() var wg sync.WaitGroup for i := 0; i < 100; i++ { wg.Add(1) go func() { defer wg.Done() for { num, ok := <-c if !ok { return } send(num) } }() } wg.Wait() }
# 1
# API GAE Context log
None
# 規 視 2 3 1
# API DB
Thank you for watching. Twitter @kojiaomatsu