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
kojiaomatsu
November 13, 2021
Technology
390
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
カートの信頼性を担保するWireMockを使ったe2eテスト
ykagano
0
260
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
13
8.7k
Sets in Go
ramalho
1
500
私がブラウザを自作したくなった理由
supurazako
1
230
攻撃と防御で学ぶAI時代のプロダクトセキュリティ演習
recruitengineers
PRO
9
2.8k
まちスペース®とデジタルツインと「まちづくり」
hiro_ogi
0
110
Agent 時代の Kaggle 展望 / kaggle-in-the-agentic-era
upura
1
720
Bits Agent Builder の⼊⾨と活⽤事例
nulabinc
PRO
0
140
【CEDEC2026】コードレビュー支援ツール開発から学ぶ:LLMを用いた業務システムの実践的な運用設計と誤出力対策
cygames
PRO
0
740
今こそ聞きたいソフトウェア設計 ドメイン駆動設計再入門
masuda220
PRO
17
7.3k
ガバメント AI 源内を地方自治体は活用できるのか可能性と課題、期待について
takeda_h
1
400
【CEDEC2026】『ウマ娘 プリティーダービー』 英語版のキャラクターの方言や口調をローカライズするための創造的アプローチ
cygames
PRO
2
730
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
55
12k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
700
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Thoughts on Productivity
jonyablonski
76
5.3k
The Spectacular Lies of Maps
axbom
PRO
1
910
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
280
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.8k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Mind Mapping
helmedeiros
PRO
1
310
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