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
What's context package
Search
sivchari
June 13, 2024
0
18
What's context package
sivchari
June 13, 2024
Tweet
Share
More Decks by sivchari
See All by sivchari
What's GOCACHEPROG ?
sivchari
1
120
gh_extensionsによる快適なOSS生活.pdf
sivchari
0
6
Visualization Go scheduler by gosched-simulator
sivchari
1
350
protoc pluginのはじめかた
sivchari
0
22
Dive into arena package ~ Go 1.20 release party ~
sivchari
0
41
GopherCon 2023 recap
sivchari
0
21
Go 1.22 range over func/range over int
sivchari
0
31
Deep dive into runtime features provided by Go1.22
sivchari
0
20
Go 1.22で追加予定 だった zeroの紹介 Go Conference mini
sivchari
0
300
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Why Our Code Smells
bkeepers
PRO
336
57k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
It's Worth the Effort
3n
184
28k
Designing for humans not robots
tammielis
250
25k
The Language of Interfaces
destraynor
156
24k
The Cult of Friendly URLs
andyhume
78
6.2k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
GraphQLとの向き合い方2022年版
quramy
44
13k
Transcript
What’s context package ? sivchari
context package contextパッケージの中で定義されているContext型は、デッドライン、キャンセルシグナ ル、その他のリクエストに応じた値をAPI境界やプロセス間で伝達します FYI context
ex ListenAndServeはリクエストごとにgoroutineを 生成する 実践的に考えるとそれぞれの goroutineがさら にgoroutineを生成する可能性がある (DB, 外部APIへのリクエスト) -> 意識せず多くのgoroutineを生成している可
能性がある -> goroutineは勝手に消滅する? -> goroutine leak
goroutine leak out put 1 before 2 in leak 2
after leak この場合はnil chanをしっかりmakeしてcloseまでする必 要がある nilでもchannelはcompile timeでerrorを発見できない(可 能性としてはruntime error)
problem - goroutine leakがより複雑になり10個などになった時 - タイムアウトした時 - 使っているライブラリでgoroutineが生成されていたら? - ある関数はcloseしたいが、ある関数は継続させたい処理にどう対応する?
- チャネルを複数用意する方法もあるがチャネルの生成にはコストがかかる
context context.Contextはinterface BackgroundやWithCancelなどユースケースによる実 装をinterfaceで抽象化できる しない場合 context.WithCancelなど限定的な型が必要
Background contextの生成にはBackgroundを使う 内部実装はContext interfaceを満たすprivate pointer intが生成される contextを使うか微妙な場合はcontext.TODOを引数に取る
contextの関係 - contextが親子関係にある - 親のcontextのキャンセルは親から派生したcontextも同じ挙動をとる - contextが兄弟関係にある(parentからctx1, ctx2が生成される) - 親がキャンセルされるとどちらも終了する
- 言い換えると親は子の影響を受けない - ある関数はcloseしたいが、ある関数は継続させたい処理にどう対応する?という 問題が解決される
Request Scope of context Keyを指定してcontextにセットする 同じcontextに対してkeyでsetした値を取得する type structの理由は一意性を型として担保するため ex) 別packageごとに同じkeyをセットした場合を考える
(hogeとfugaで同じ文字列をkeyにする) パッケージに閉じた(privateな)keyを使うようにする Q. 閉じていればintなどでもいいのでは? - 空のstructはメモリサイズが0 FYI https://go.dev/play/p/TYZV5GPrIWm
Don’t use context like this structの中にcontextを含めない - contextにはスコープごとの値がsetされるかもしれない(JWT, Token etc..)
- structに含めるとどこで参照されるかわからない 実際にBad PracticeとGo Teamが言っている Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions. context -> 第一引数で渡すようにする
Why? - スコープがinterface(API)として明確になる - 例外はnet/http - 後方互換性(contextは1.7から登場した) - database/sqlはcontext対応のメソッドを生やした FYI
https://go.dev/blog/context-and-structs https://github.com/golang/go/blob/ecf6b52b7f4ba6e8c98f25adf9e83773fe908829/ src/net/http/request.go#L319-L323
まとめ - 自分のライブラリを作る場合はcontextを渡すようなinterfaceを実装する - クライアント(使う側)から明示的にストップさせる以外はcontextを使う方がいい (streamなどはchannelを使う方がいい) - 逆説としてchannelを使うときはgoroutine leakが起きないか注意する