Gas optimization in Solidity, Ethereum
Iโm sorry but my English is terrible. I hope you understand that generously.Recently, I was developing a toy project named Blind Market. Itโs a simple P2P trading application using smart contract. I was making a contract using Solidity, and the trade stage proceeded in the order of pending, shipping, and done. The problem was appeared in done phase. The problem was that when I tried to close the transaction by paying the price raised by the seller in msg.value, the following error occurred.Pe...
Uvicorn & Gunicorn
Uvicorn and GunicornUvicorn and Gunicorn are important concepts when developing applications in Python. However, there are many concepts to be aware of in order to fully understand Uvicorn and Gunicorn. The following is a brief summary of the necessary concepts, and the details will be dealt with separately later.Necessary ConceptsStarletteStarlette is a Web application server that can run asynchronously. Starlette runs on top of Uvicorn.FastAPIFastAPI provides many features on top of Starlet...
P2WPKH
P2WPKHP2WPKH๋ ๋นํธ์ฝ์ธ ๋ด์์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์คํฌ๋ฆฝํธ ํ์์ผ๋ก ๋นํธ์ฝ์ธ ํ๋กํ ์ฝ์ ๋ํ ์ง๋ถ ๊ฑฐ๋ ์ ํ์ด๋ค. ์ฃผ์๋ 1๋ก ์์ํ๋๋ฐ, ์ธ๊ทธ์์ ์ง์ํ๋ ์๋ก์ด ์ฃผ์ 3 ๋๋ bc1๋ก ์์ํ๋ ์ฃผ์๋ณด๋ค ํจ์ฌ ๋น์ธ๋ค. https://mirror.xyz/0xA1d9f681B25C14C1eE7B87f1CF102E73cA3ad4d9/egjhNVklgy_LgZmcTXXAOTBa6ePBqO3Ja9ZSoDIad-8 ์ฆ, ๋นํธ์ฝ์ธ ์ฃผ์๊ฐ 1๋ก ์์ํ๋ฉด P2PKH ์ฃผ์๋ฅผ ์ฌ์ฉํ๊ณ ์๋ ๊ฒ์ด๋ค. ๊ณต๊ฐํค์ ๊ฐ๋จํ ํด์์ด๋ฉฐ, ์ด ํด์๋ฅผ ์ฃผ์๋ก ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ์ด๊ฒ์ ์๋ ๋นํธ์ฝ์ธ ์ฃผ์ ํ์์ด์์ผ๋ฉฐ ์ค๋๊น์ง๋ ์ถฉ์คํ ์๋ํ๋ค. ๋ ๊ฑฐ์ ์ฃผ์๋ ์ธ๊ทธ์๊ณผ ํธํ๋์ง ์์ง๋ง, ์ฌ์ ํ ๋ฌธ์ ์์ด P2PKH ์ฃผ์์์ ์ธ๊ทธ์ ์ฃผ์๋ก BTC๋ฅผ ๋ณด๋ผ ์ ์๋ค. ๊ทธ๋ฌ๋ ๋ ๊ฑฐ์ ์ฃผ์ ํธ๋์ญ์ ์ด ๋ ํฌ๊ธฐ ๋๋ฌธ์ P2PKH ์ฃผ์์์ ์ ์กํ๋ ํ๊ท ์๋๋ ์ธ๊ทธ์ ์ฃผ์์์ ์ ์กํ ๋๋ณด๋ค ๋ ๋์ ์๊ธ์ด ๋ฐ์ํ ์ ์๋ค....
Smart Contract Developer, Web3 Backend Developer

Subscribe to Primrose
Gas optimization in Solidity, Ethereum
Iโm sorry but my English is terrible. I hope you understand that generously.Recently, I was developing a toy project named Blind Market. Itโs a simple P2P trading application using smart contract. I was making a contract using Solidity, and the trade stage proceeded in the order of pending, shipping, and done. The problem was appeared in done phase. The problem was that when I tried to close the transaction by paying the price raised by the seller in msg.value, the following error occurred.Pe...
Uvicorn & Gunicorn
Uvicorn and GunicornUvicorn and Gunicorn are important concepts when developing applications in Python. However, there are many concepts to be aware of in order to fully understand Uvicorn and Gunicorn. The following is a brief summary of the necessary concepts, and the details will be dealt with separately later.Necessary ConceptsStarletteStarlette is a Web application server that can run asynchronously. Starlette runs on top of Uvicorn.FastAPIFastAPI provides many features on top of Starlet...
P2WPKH
P2WPKHP2WPKH๋ ๋นํธ์ฝ์ธ ๋ด์์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์คํฌ๋ฆฝํธ ํ์์ผ๋ก ๋นํธ์ฝ์ธ ํ๋กํ ์ฝ์ ๋ํ ์ง๋ถ ๊ฑฐ๋ ์ ํ์ด๋ค. ์ฃผ์๋ 1๋ก ์์ํ๋๋ฐ, ์ธ๊ทธ์์ ์ง์ํ๋ ์๋ก์ด ์ฃผ์ 3 ๋๋ bc1๋ก ์์ํ๋ ์ฃผ์๋ณด๋ค ํจ์ฌ ๋น์ธ๋ค. https://mirror.xyz/0xA1d9f681B25C14C1eE7B87f1CF102E73cA3ad4d9/egjhNVklgy_LgZmcTXXAOTBa6ePBqO3Ja9ZSoDIad-8 ์ฆ, ๋นํธ์ฝ์ธ ์ฃผ์๊ฐ 1๋ก ์์ํ๋ฉด P2PKH ์ฃผ์๋ฅผ ์ฌ์ฉํ๊ณ ์๋ ๊ฒ์ด๋ค. ๊ณต๊ฐํค์ ๊ฐ๋จํ ํด์์ด๋ฉฐ, ์ด ํด์๋ฅผ ์ฃผ์๋ก ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ์ด๊ฒ์ ์๋ ๋นํธ์ฝ์ธ ์ฃผ์ ํ์์ด์์ผ๋ฉฐ ์ค๋๊น์ง๋ ์ถฉ์คํ ์๋ํ๋ค. ๋ ๊ฑฐ์ ์ฃผ์๋ ์ธ๊ทธ์๊ณผ ํธํ๋์ง ์์ง๋ง, ์ฌ์ ํ ๋ฌธ์ ์์ด P2PKH ์ฃผ์์์ ์ธ๊ทธ์ ์ฃผ์๋ก BTC๋ฅผ ๋ณด๋ผ ์ ์๋ค. ๊ทธ๋ฌ๋ ๋ ๊ฑฐ์ ์ฃผ์ ํธ๋์ญ์ ์ด ๋ ํฌ๊ธฐ ๋๋ฌธ์ P2PKH ์ฃผ์์์ ์ ์กํ๋ ํ๊ท ์๋๋ ์ธ๊ทธ์ ์ฃผ์์์ ์ ์กํ ๋๋ณด๋ค ๋ ๋์ ์๊ธ์ด ๋ฐ์ํ ์ ์๋ค....
Share Dialog
Share Dialog
<100 subscribers
<100 subscribers
golang์ ์จ๋ดค์ผ๋ฉด ๋ค๋ค ํ ๋ฒ์ฏค ์ฌ์ฉํ๊ฒ ๋๋๊ฒ์ด ๋ฐ๋ก context.Context ์ด๋ค.
go์์๋ context๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ๊ณ ์ด๋ป๊ฒ ์ค๊ณํ์์ง ์ดํด๋ณด์.
๊ณต์๋ฌธ์๋ฅผ ์ฝ์ผ๋ฉด์ ์ดํด๋๋ฅผ ๋์ฌ๋ณด์.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
context ํจํค์ง๋ API ๊ฒฝ๊ณ๋ฅผ ๋์ด ํ๋ก์ธ์ค ๊ฐ์ deadline, ์ทจ์ signal ๋ฐ ๊ธฐํ ์์ฒญ ๋ฒ์ ๊ฐ์ ์ ๋ฌํ๋ ์ปจํ
์คํธ ์ ํ์ ์ ์ํฉ๋๋ค.
์ปจํ ์คํธ๋ ์ฐ๋ฆฌ ๋ง๋ก ๋ฌธ๋งฅ, ๋งฅ๋ฝ์ ๋ปํ๋ค. REST ์์ฒญ์ด๊ฑด, ์์ผ ์์ฒญ์ํ๊ฑด, ์์ฒญ-์๋ต์ ํ๋ฆ ์์์ ์ ์งํด์ผ ํ ์ํ๋ฅผ context๋ฅผ ํตํด ๊ณต์ ํ๋ค.
Incoming requests to a server should create a Context, and outgoing calls to servers should accept a Context.
์๋ฒ๋ก ๋ค์ด์ค๋ ์์ฒญ์ ์ปจํ
์คํธ๋ฅผ ์์ฑํ๊ณ ์๋ฒ๋ก ๋๊ฐ๋ ํธ์ถ์ ์ปจํ
์คํธ๋ฅผ ์๋ฝํด์ผ ํฉ๋๋ค.
The chain of function calls between them must propagate the Context, optionally replacing it with a derived Context created using WithCancel, WithDeadline, WithTimeout, or WithValue.
์ด๋ค ์ฌ์ด์ ํจ์ ํธ์ถ ์ฒด์ธ์ ์ปจํ
์คํธ๋ฅผ ์ ํํด์ผ ํ๋ฉฐ ์ ํ์ ์ผ๋ก WithCancel, WithDadline, WithTimeout ๋๋ WithValue๋ฅผ ์ฌ์ฉํ์ฌ ๋ง๋ ํ์ ์ปจํ
์คํธ๋ก ๋์ฒดํด์ผ ํฉ๋๋ค.
When a Context is canceled, all Contexts derived from it are also canceled.
์ปจํ
์คํธ๊ฐ ์ทจ์๋๋ฉด ์ปจํ
์คํธ์์ ํ์๋ ๋ชจ๋ ์ปจํ
์คํธ๋ ์ทจ์๋ฉ๋๋ค.
์ฆ, ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์ปจํ ์คํธ๋ฅผ ์์ฑํ๊ณ , ์์ฒญ์ด ๋๋๋ฉด ์ปจํ ์คํธ๋ฅผ ์ข ๋ฃ์ํค๊ฒ ๋ค๋ ๋ป์ด๋ค.
์์์ ์๊ธฐํ๋ ์์ฒญ-์๋ต์ ํ๋ฆ์ ์ค์ ์ผ๋ก ๋ณด์. ๋ชจ๋ API๋ ์์ฒญ๊ณผ ์๋ต์ด ์๋ค. ํ๋์ ์์ฒญ ์์์๋ ๋ถ๊ธฐํด์ ๋ง์ ๋ก์ง์ ์ํํ ์ ์๋ค. ๊ทธ๋ ๊ฒ๋๋ฉด ํ๋์ ์์ฒญ-์๋ต ์ฌ์ด์๋ ์๋ง์ ์์ฒญ2-์๋ต2, ์์ฒญ3-์๋ต3โฆ ์ด ์๊ธฐ๊ฒ ๋๋ค.
ํ๋์ ์์ฒญ-์๋ต์ด ํ๋์ lifecycle์ ๊ฐ์ง๋ค๊ณ ํ์๋ root๋ก ๋ถํฐ context๋ฅผ ํ์์์ผ๊ฐ๋ฉด ๋ง์ API์ lifecycle์ ๊ด๋ฆฌํ ์๊ฐ ์๋ค.
WithCancel,WithDeadline,WithTimeout,WithValue๋ฅผ ์ด์ฉํด์ ํ์์ํค๊ณ , ๊ด๋ฆฌํ๋ฉด ๋ ๊ฒ์ด๋ค.
The WithCancel, WithDeadline, and WithTimeout functions take a Context (the parent) and return a derived Context (the child) and a CancelFunc.
WithCancel, WithDedline, WithTimeout ๊ธฐ๋ฅ์ ์ปจํ
์คํธ(๋ถ๋ชจ)๋ฅผ ์ ํํ๊ณ ํ์ ์ปจํ
์คํธ(์๋
)์ CancelFunc๋ฅผ ๋ฐํํฉ๋๋ค.
Calling the CancelFunc cancels the child and its children, removes the parent's reference to the child, and stops any associated timers.
CancelFunc๋ฅผ ํธ์ถํ๋ฉด ๋ณธ์ธ๊ณผ ์๋
๊ฐ ์ทจ์๋๊ณ , ์๋
์ ๋ํ ๋ถ๋ชจ์ ์ฐธ์กฐ๊ฐ ์ ๊ฑฐ๋๋ฉฐ, ์ฐ๊ด๋ ํ์ด๋จธ๊ฐ ์ค์ง๋ฉ๋๋ค.
Failing to call the CancelFunc leaks the child and its children until the parent is canceled or the timer fires.
CancelFunc๋ฅผ ํธ์ถํ์ง ๋ชปํ๋ฉด ๋ถ๋ชจ๊ฐ ์ทจ์๋๊ฑฐ๋ ํ์ด๋จธ๊ฐ ์คํ๋ ๋๊น์ง ์๋
์ปจํ
์คํธ๋ค์ ๋์๊ฐ ๋ฐ์๋ฉ๋๋ค.
The go vet tool checks that CancelFuncs are used on all control-flow paths.
go vet ๋ CancelFuncs๊ฐ ๋ชจ๋ ์ ์ด ํ๋ฆ ๊ฒฝ๋ก์์ ์ฌ์ฉ๋๋์ง ํ์ธํฉ๋๋ค.
cancel() ํจ์๋ฅผ ์คํํ๋ฉด ์์๋ค์ด ์ ๋ถ ์ทจ์๋ ๊น?
func childCtx(ctx context.Context, cancelFunc context.CancelFunc) {
defer cancelFunc()
time.Sleep(3 * time.Second)
log.Println("childCtx done", ctx.Err())
}
func parentsCtx(c context.Context) {
ctx, cancel := context.WithCancel(c)
defer cancel()
go childCtx(ctx, cancel)
time.Sleep(time.Second * 1)
log.Println("parentsCtx done")
}
// 2023/09/09 23:00:30 parentsCtx done
// 2023/09/09 23:00:32 childCtx done context canceled
์ค์ ๋ก ๋์ ์์ฒด๋ฅผ ๋ฉ์ถ๋ ๊ฒ์ ์๋๊ณ , context๊ฐ ์ทจ์๋๋ค. ์์ ํจ์๊ฐ ๋ฐ๋ก ์ข ๋ฃ๋์ง๋ ์๋๋ค.
The WithCancelCause function returns a CancelCauseFunc, which takes an error and records it as the cancellation cause. Calling Cause on the canceled context or any of its children retrieves the cause. If no cause is specified, Cause(ctx) returns the same value as ctx.Err().
WithCancelCause ํจ์๋ CancelCauseFunc๋ฅผ ๋ฐํํ๋ฉฐ, ์ด ํจ์๋ ์ค๋ฅ๋ฅผ ๊ฐ์ ธ์ ์ทจ์ ์์ธ์ผ๋ก ๊ธฐ๋กํฉ๋๋ค. ์ทจ์๋ ์ปจํ
์คํธ ๋๋ ํด๋น ํ์ ์ปจํ
์คํธ์์ ์์ธ์ ํธ์ถํ๋ฉด ์์ธ์ ๊ฒ์ํฉ๋๋ค. ์์ธ์ด ์ง์ ๋์ง ์์ผ๋ฉด Cause(ctx)๋ ctx.Err()์ ๋์ผํ ๊ฐ์ ๋ฐํํฉ๋๋ค.
์ปจํ ์คํธ ์์ฒด๋ฅผ ์ทจ์ ์ฒ๋ฆฌํ๊ณ ์๋ฌ๋ฅผ ๊ธฐ๋กํ๋ค.
Programs that use Contexts should follow these rules to keep interfaces consistent across packages and enable static analysis tools to check context propagation:
์ปจํ
์คํธ๋ฅผ ์ฌ์ฉํ๋ ํ๋ก๊ทธ๋จ์ ํจํค์ง ๊ฐ ์ธํฐํ์ด์ค๋ฅผ ์ผ๊ด๋๊ฒ ์ ์งํ๊ณ ์ ์ ๋ถ์ ๋๊ตฌ๊ฐ ์ปจํ
์คํธ ์ ํ๋ฅผ ํ์ธํ ์ ์๋๋ก ๋ค์ ๊ท์น์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx:
๊ตฌ์กฐ์ฒด ์ ํ ์์ ์ปจํ
์คํธ๋ฅผ ์ ์ฅํ์ง ๋ง๊ณ ์ปจํ
์คํธ๋ฅผ ํ์ํ ๊ฐ ํจ์์ ๋ช
์์ ์ผ๋ก ์ ๋ฌํฉ๋๋ค. ์ปจํ
์คํธ๋ ์ผ๋ฐ์ ์ผ๋ก ctx๋ก ๋ช
๋ช
๋๋ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ ๋ณ์์ฌ์ผ ํฉ๋๋ค:
func DoSomething(ctx context.Context, arg Arg) error {
// ... use ctx ...
}
Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.
ํจ์๊ฐ ํ์ฉํ๋๋ผ๋ nil ์ปจํ
์คํธ๋ฅผ ์ ๋ฌํ์ง ๋ง์ญ์์ค. ์ฌ์ฉํ ์ปจํ
์คํธ๊ฐ ํ์คํ์ง ์์ผ๋ฉด context.TODO๋ฅผ ์ ๋ฌํฉ๋๋ค.
Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.
context Values๋ ํ๋ก์ธ์ค ๋ฐ API๋ฅผ ์ ์กํ๋ ์์ฒญ ๋ฒ์ ๋ฐ์ดํฐ์๋ง ์ฌ์ฉํ๋ฉฐ, ์ต์
ํ๋ผ๋ฏธํฐ๋ฅผ ํจ์์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ์ง ์์ต๋๋ค.
The same Context may be passed to functions running in different goroutines; Contexts are safe for simultaneous use by multiple goroutines.
๋์ผํ ์ปจํ
์คํธ๋ฅผ ๋ค๋ฅธ ๊ฒฝ๋ก์์ ์คํ ์ค์ธ ํจ์์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์ปจํ
์คํธ๋ ์ฌ๋ฌ goroutine์์ ๋์์ ์ฌ์ฉํ๊ธฐ์ ์์ ํฉ๋๋ค.
์ปจํ ์คํธ๋ฅผ ์ฌ์ฉํ๋ ์์ ์ ๋ํ ๊ฒ์ด๋ค.
context.Context์์ฒด๊ฐ ์ธํฐํ์ด์ค๋ค. ๋ฐ๋ผ์ ๋ณธ์ธ์ด ๊ตฌํํ ์์ฒด Context๋ฅผ ์ฌ์ฉํ๋๋ผ๋ ์ด๋ฌํ ์ฌํญ์ ๋ํด์ ์งํค๋ฉฐ ๊ฐ๋ฐ์ ํ๋ฉด ์ข์ ๊ฒ์ด๋ค.
go ์ธ์ด์ ์ฒ ํ์ค์ ์ด๋ฐ ๋ง์ด ์๋ค.
์๋ก ๋ค๋ฅธ ํ๋ก์ธ์ค, API, ํจ์ ๋ฑ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณต์ ํ๊ฒ ํ๋ ๊ฒ์ด ์๋๋ผ, ์ปจํ ์คํธ๋ฅผ ํตํด ์ปค๋ฎค๋์ผ์ด์ ์ ํ์ฑํ๋ฉด ์ด๋ฌํ ์ฒ ํ์ ๋ฌ์ฑํ ์ ์์๊ฒ์ด๋ค.
golang์ ์จ๋ดค์ผ๋ฉด ๋ค๋ค ํ ๋ฒ์ฏค ์ฌ์ฉํ๊ฒ ๋๋๊ฒ์ด ๋ฐ๋ก context.Context ์ด๋ค.
go์์๋ context๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ๊ณ ์ด๋ป๊ฒ ์ค๊ณํ์์ง ์ดํด๋ณด์.
๊ณต์๋ฌธ์๋ฅผ ์ฝ์ผ๋ฉด์ ์ดํด๋๋ฅผ ๋์ฌ๋ณด์.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
context ํจํค์ง๋ API ๊ฒฝ๊ณ๋ฅผ ๋์ด ํ๋ก์ธ์ค ๊ฐ์ deadline, ์ทจ์ signal ๋ฐ ๊ธฐํ ์์ฒญ ๋ฒ์ ๊ฐ์ ์ ๋ฌํ๋ ์ปจํ
์คํธ ์ ํ์ ์ ์ํฉ๋๋ค.
์ปจํ ์คํธ๋ ์ฐ๋ฆฌ ๋ง๋ก ๋ฌธ๋งฅ, ๋งฅ๋ฝ์ ๋ปํ๋ค. REST ์์ฒญ์ด๊ฑด, ์์ผ ์์ฒญ์ํ๊ฑด, ์์ฒญ-์๋ต์ ํ๋ฆ ์์์ ์ ์งํด์ผ ํ ์ํ๋ฅผ context๋ฅผ ํตํด ๊ณต์ ํ๋ค.
Incoming requests to a server should create a Context, and outgoing calls to servers should accept a Context.
์๋ฒ๋ก ๋ค์ด์ค๋ ์์ฒญ์ ์ปจํ
์คํธ๋ฅผ ์์ฑํ๊ณ ์๋ฒ๋ก ๋๊ฐ๋ ํธ์ถ์ ์ปจํ
์คํธ๋ฅผ ์๋ฝํด์ผ ํฉ๋๋ค.
The chain of function calls between them must propagate the Context, optionally replacing it with a derived Context created using WithCancel, WithDeadline, WithTimeout, or WithValue.
์ด๋ค ์ฌ์ด์ ํจ์ ํธ์ถ ์ฒด์ธ์ ์ปจํ
์คํธ๋ฅผ ์ ํํด์ผ ํ๋ฉฐ ์ ํ์ ์ผ๋ก WithCancel, WithDadline, WithTimeout ๋๋ WithValue๋ฅผ ์ฌ์ฉํ์ฌ ๋ง๋ ํ์ ์ปจํ
์คํธ๋ก ๋์ฒดํด์ผ ํฉ๋๋ค.
When a Context is canceled, all Contexts derived from it are also canceled.
์ปจํ
์คํธ๊ฐ ์ทจ์๋๋ฉด ์ปจํ
์คํธ์์ ํ์๋ ๋ชจ๋ ์ปจํ
์คํธ๋ ์ทจ์๋ฉ๋๋ค.
์ฆ, ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์ปจํ ์คํธ๋ฅผ ์์ฑํ๊ณ , ์์ฒญ์ด ๋๋๋ฉด ์ปจํ ์คํธ๋ฅผ ์ข ๋ฃ์ํค๊ฒ ๋ค๋ ๋ป์ด๋ค.
์์์ ์๊ธฐํ๋ ์์ฒญ-์๋ต์ ํ๋ฆ์ ์ค์ ์ผ๋ก ๋ณด์. ๋ชจ๋ API๋ ์์ฒญ๊ณผ ์๋ต์ด ์๋ค. ํ๋์ ์์ฒญ ์์์๋ ๋ถ๊ธฐํด์ ๋ง์ ๋ก์ง์ ์ํํ ์ ์๋ค. ๊ทธ๋ ๊ฒ๋๋ฉด ํ๋์ ์์ฒญ-์๋ต ์ฌ์ด์๋ ์๋ง์ ์์ฒญ2-์๋ต2, ์์ฒญ3-์๋ต3โฆ ์ด ์๊ธฐ๊ฒ ๋๋ค.
ํ๋์ ์์ฒญ-์๋ต์ด ํ๋์ lifecycle์ ๊ฐ์ง๋ค๊ณ ํ์๋ root๋ก ๋ถํฐ context๋ฅผ ํ์์์ผ๊ฐ๋ฉด ๋ง์ API์ lifecycle์ ๊ด๋ฆฌํ ์๊ฐ ์๋ค.
WithCancel,WithDeadline,WithTimeout,WithValue๋ฅผ ์ด์ฉํด์ ํ์์ํค๊ณ , ๊ด๋ฆฌํ๋ฉด ๋ ๊ฒ์ด๋ค.
The WithCancel, WithDeadline, and WithTimeout functions take a Context (the parent) and return a derived Context (the child) and a CancelFunc.
WithCancel, WithDedline, WithTimeout ๊ธฐ๋ฅ์ ์ปจํ
์คํธ(๋ถ๋ชจ)๋ฅผ ์ ํํ๊ณ ํ์ ์ปจํ
์คํธ(์๋
)์ CancelFunc๋ฅผ ๋ฐํํฉ๋๋ค.
Calling the CancelFunc cancels the child and its children, removes the parent's reference to the child, and stops any associated timers.
CancelFunc๋ฅผ ํธ์ถํ๋ฉด ๋ณธ์ธ๊ณผ ์๋
๊ฐ ์ทจ์๋๊ณ , ์๋
์ ๋ํ ๋ถ๋ชจ์ ์ฐธ์กฐ๊ฐ ์ ๊ฑฐ๋๋ฉฐ, ์ฐ๊ด๋ ํ์ด๋จธ๊ฐ ์ค์ง๋ฉ๋๋ค.
Failing to call the CancelFunc leaks the child and its children until the parent is canceled or the timer fires.
CancelFunc๋ฅผ ํธ์ถํ์ง ๋ชปํ๋ฉด ๋ถ๋ชจ๊ฐ ์ทจ์๋๊ฑฐ๋ ํ์ด๋จธ๊ฐ ์คํ๋ ๋๊น์ง ์๋
์ปจํ
์คํธ๋ค์ ๋์๊ฐ ๋ฐ์๋ฉ๋๋ค.
The go vet tool checks that CancelFuncs are used on all control-flow paths.
go vet ๋ CancelFuncs๊ฐ ๋ชจ๋ ์ ์ด ํ๋ฆ ๊ฒฝ๋ก์์ ์ฌ์ฉ๋๋์ง ํ์ธํฉ๋๋ค.
cancel() ํจ์๋ฅผ ์คํํ๋ฉด ์์๋ค์ด ์ ๋ถ ์ทจ์๋ ๊น?
func childCtx(ctx context.Context, cancelFunc context.CancelFunc) {
defer cancelFunc()
time.Sleep(3 * time.Second)
log.Println("childCtx done", ctx.Err())
}
func parentsCtx(c context.Context) {
ctx, cancel := context.WithCancel(c)
defer cancel()
go childCtx(ctx, cancel)
time.Sleep(time.Second * 1)
log.Println("parentsCtx done")
}
// 2023/09/09 23:00:30 parentsCtx done
// 2023/09/09 23:00:32 childCtx done context canceled
์ค์ ๋ก ๋์ ์์ฒด๋ฅผ ๋ฉ์ถ๋ ๊ฒ์ ์๋๊ณ , context๊ฐ ์ทจ์๋๋ค. ์์ ํจ์๊ฐ ๋ฐ๋ก ์ข ๋ฃ๋์ง๋ ์๋๋ค.
The WithCancelCause function returns a CancelCauseFunc, which takes an error and records it as the cancellation cause. Calling Cause on the canceled context or any of its children retrieves the cause. If no cause is specified, Cause(ctx) returns the same value as ctx.Err().
WithCancelCause ํจ์๋ CancelCauseFunc๋ฅผ ๋ฐํํ๋ฉฐ, ์ด ํจ์๋ ์ค๋ฅ๋ฅผ ๊ฐ์ ธ์ ์ทจ์ ์์ธ์ผ๋ก ๊ธฐ๋กํฉ๋๋ค. ์ทจ์๋ ์ปจํ
์คํธ ๋๋ ํด๋น ํ์ ์ปจํ
์คํธ์์ ์์ธ์ ํธ์ถํ๋ฉด ์์ธ์ ๊ฒ์ํฉ๋๋ค. ์์ธ์ด ์ง์ ๋์ง ์์ผ๋ฉด Cause(ctx)๋ ctx.Err()์ ๋์ผํ ๊ฐ์ ๋ฐํํฉ๋๋ค.
์ปจํ ์คํธ ์์ฒด๋ฅผ ์ทจ์ ์ฒ๋ฆฌํ๊ณ ์๋ฌ๋ฅผ ๊ธฐ๋กํ๋ค.
Programs that use Contexts should follow these rules to keep interfaces consistent across packages and enable static analysis tools to check context propagation:
์ปจํ
์คํธ๋ฅผ ์ฌ์ฉํ๋ ํ๋ก๊ทธ๋จ์ ํจํค์ง ๊ฐ ์ธํฐํ์ด์ค๋ฅผ ์ผ๊ด๋๊ฒ ์ ์งํ๊ณ ์ ์ ๋ถ์ ๋๊ตฌ๊ฐ ์ปจํ
์คํธ ์ ํ๋ฅผ ํ์ธํ ์ ์๋๋ก ๋ค์ ๊ท์น์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx:
๊ตฌ์กฐ์ฒด ์ ํ ์์ ์ปจํ
์คํธ๋ฅผ ์ ์ฅํ์ง ๋ง๊ณ ์ปจํ
์คํธ๋ฅผ ํ์ํ ๊ฐ ํจ์์ ๋ช
์์ ์ผ๋ก ์ ๋ฌํฉ๋๋ค. ์ปจํ
์คํธ๋ ์ผ๋ฐ์ ์ผ๋ก ctx๋ก ๋ช
๋ช
๋๋ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ ๋ณ์์ฌ์ผ ํฉ๋๋ค:
func DoSomething(ctx context.Context, arg Arg) error {
// ... use ctx ...
}
Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.
ํจ์๊ฐ ํ์ฉํ๋๋ผ๋ nil ์ปจํ
์คํธ๋ฅผ ์ ๋ฌํ์ง ๋ง์ญ์์ค. ์ฌ์ฉํ ์ปจํ
์คํธ๊ฐ ํ์คํ์ง ์์ผ๋ฉด context.TODO๋ฅผ ์ ๋ฌํฉ๋๋ค.
Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.
context Values๋ ํ๋ก์ธ์ค ๋ฐ API๋ฅผ ์ ์กํ๋ ์์ฒญ ๋ฒ์ ๋ฐ์ดํฐ์๋ง ์ฌ์ฉํ๋ฉฐ, ์ต์
ํ๋ผ๋ฏธํฐ๋ฅผ ํจ์์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ์ง ์์ต๋๋ค.
The same Context may be passed to functions running in different goroutines; Contexts are safe for simultaneous use by multiple goroutines.
๋์ผํ ์ปจํ
์คํธ๋ฅผ ๋ค๋ฅธ ๊ฒฝ๋ก์์ ์คํ ์ค์ธ ํจ์์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์ปจํ
์คํธ๋ ์ฌ๋ฌ goroutine์์ ๋์์ ์ฌ์ฉํ๊ธฐ์ ์์ ํฉ๋๋ค.
์ปจํ ์คํธ๋ฅผ ์ฌ์ฉํ๋ ์์ ์ ๋ํ ๊ฒ์ด๋ค.
context.Context์์ฒด๊ฐ ์ธํฐํ์ด์ค๋ค. ๋ฐ๋ผ์ ๋ณธ์ธ์ด ๊ตฌํํ ์์ฒด Context๋ฅผ ์ฌ์ฉํ๋๋ผ๋ ์ด๋ฌํ ์ฌํญ์ ๋ํด์ ์งํค๋ฉฐ ๊ฐ๋ฐ์ ํ๋ฉด ์ข์ ๊ฒ์ด๋ค.
go ์ธ์ด์ ์ฒ ํ์ค์ ์ด๋ฐ ๋ง์ด ์๋ค.
์๋ก ๋ค๋ฅธ ํ๋ก์ธ์ค, API, ํจ์ ๋ฑ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณต์ ํ๊ฒ ํ๋ ๊ฒ์ด ์๋๋ผ, ์ปจํ ์คํธ๋ฅผ ํตํด ์ปค๋ฎค๋์ผ์ด์ ์ ํ์ฑํ๋ฉด ์ด๋ฌํ ์ฒ ํ์ ๋ฌ์ฑํ ์ ์์๊ฒ์ด๋ค.
No activity yet