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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sivchari
June 13, 2024
0
38
What's context package
sivchari
June 13, 2024
Tweet
Share
More Decks by sivchari
See All by sivchari
静的解析 x Kubernetes API Conventions = Kube API Linter ~ ベストプラクティスに準拠したカスタムリソースの作り方と運用 ~
sivchari
0
62
What's GOCACHEPROG ?
sivchari
1
420
gh_extensionsによる快適なOSS生活.pdf
sivchari
0
33
Visualization Go scheduler by gosched-simulator
sivchari
1
490
protoc pluginのはじめかた
sivchari
0
35
Dive into arena package ~ Go 1.20 release party ~
sivchari
0
83
GopherCon 2023 recap
sivchari
0
48
Go 1.22 range over func/range over int
sivchari
0
87
Deep dive into runtime features provided by Go1.22
sivchari
0
36
Featured
See All Featured
Believing is Seeing
oripsolob
1
54
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
140
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
320
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
410
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
150
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
Designing for humans not robots
tammielis
254
26k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
140
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
The Pragmatic Product Professional
lauravandoore
37
7.1k
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が起きないか注意する