1. Wae Shutdowni piryohago wae taimaauti pilsuin-ga
HTTP seobeoreul cheoeum ttuiumyeon, seobeoreul “meomchuneun” ireun geunyang main()eul kkeutnaemyeon doeneun geotcheoreom boimnida. Hajiman seobeoeneun hwalseong yeongyeol, “jinhaeng jungin” yocheong, beopeo, geurigo hangsang maljjal deureojuneun geosi anin keullaieonteuga isseumnida. I seksyeoneseoneun Shutdowni jeonghwakhi mueoseul haneunji, geurigo wae taimaaut eopsineun jom bi peuroseseureul eotge doel su inneunji salpyeobnida. “Beolseo kkeutnaya hal geot gateunde” waenya hamyeon kkeutnaji anneun geu sangtaemalibnida.
(*http.Server).Shutdown(ctx)neun yuhan jongryoimnida. Uimireul malhamyeon, seobeoneun saeroun yeongyeoreul deo isang badeuji anko imi sijakhan yocheongdeuri “jeongsangjeogeuro” kkeutnal su itge gireul yeoreojumnida. Hajiman yeogiseo haeksim daneoneun gihweimnida. Yocheongi meomchwo beorigeona, keullaieonteuga yeongyeoreul jwijgo itgeona, haendeulleoga neomu ora geolligeona, ttoneun eodingaeseo oebu riseoseureul gidarigo itdamyeon, daegiga gireojil su itseumnida.
Yeogiseo jungyohan aidi-eoga deungjanghamnida: context-ga daegi siganui sanghanseoneul jeonghamnida. Go-eseo context-neun deadline/timeoutgwa chwiso-reul tonghae “sigan-i da doeeotda”rago allyeojul su itsseumnida. Geurigo context.WithTimeoutneun deadline-i inneun 파생 context-reul mandeuneun pyojun bangbeopimnida.
Shutdown(context.Background())reul buleumyeon, “eolmana geollyeodo joa, kyesok gidaryeo”raneun tteusimnida. Peurodeoksyeoneseoneun ige “jamkkanmanyo, 5 bunman”haego saengsaeng moutgwanui 3 il dongan sarajineun geotgwa gateumnida. Geuraeseo gyuchigeun dansundo gangryeokhamnida:
Shutdown-neun hangsang context.WithTimeout-euro saengseonghan context-wa hamkke bulleo야 합니다 (taimaaut pilyosu).
Banghyangeul jabgi wihan jageun pyoreul bomnida:
| Method | Uimi | Hwalseong yocheonge mitchineun yeonghyang | Jueuihal jeom |
|---|---|---|---|
|
yuhan jongryo | kkeutnal ttaekkaji gidariryeogo sim | taimaaut eopsimyeon muhanhi gidaril su isseum |
|
gangje jongryo | yeongyeori kkeun-eojim | sayongjaga yeongyeol kkeum/gwa oryu-reul gyeok-eul su isseum |
2. WithTimeoutgwa hamkke sseuneun kanonikal shutdown paeteon
Gibon paeteon: WithTimeout + defer cancel() + Shutdown
Manheun choboja-deureun “jireumgireul taga” sip-eohamnida: srv.Shutdown(context.Background())reul sseugeona, taimaaut-eul mandeureotjiman cancel()eul bbeomeokseumnida. Ireon ireun chungbunhi il-eonal su itsseumnida. Uri noe-neun eneojireul jeoyakhamyeo “geunyang jagdonghagetji”rago saenggakhamnida. Geureona context-eneun taimereu, yeongyeol jeongbo gateun riseoseuga itgo, olbareun paeteoneun gwallyojeog bureokeulasi ga anira kodeu wisaengimnida.
Jongryo-reul wihan kanonikal paeteoneun ireotseumnida: taimaauti inneun context-reul mandeulgo, jeil meonjeo defer cancel()eul baechihan dwi, Shutdowneul hoechulhamnida. WithTimeouteun sigani jinamyeon jadong-euro chwiso-doeneun 파생 context-reul saengseonghamnida.
Jageun yeje (gongsiganeseo, sinho-do seobeodo jubyeon guseongdo eopsi):
package main
import (
"context"
"log"
"net/http"
"time"
)
func stopServer(srv *http.Server) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v", err)
}
}
Yeogiseoneun taimaauti ittdaneun geotman jungyohan geosi anira, cancel()i hangsang hoechuldoendorok bojangdoendaneun geotdo jungyohamnida. Ne, context-neun sigani jinamyeon seuseuro chwiso-doebnida. Hajiman cancel()eun “jari jeongri”wa gateumnida: uri-ga deo ppalli kkeutnatdamyeon yeonggyeondoen riseoseureul deo iljjik haejebnida.
Shutdown oryu-neun eotteoke roginghal geosinga
Chobojadeureun jaju Shutdowni “seonggong”ina “manggaejim” dul jung han gajirago saenggakhapnida. Geureona silje-ro-neun ses beonjjae modeuga itsseumnida: “choeseoneul dahayeotjiman sigan-i bujokhaetda”. Ige hangsang jaeang-eun anijiman, dangsin-ui aebpeullikeisyeoni chwiso-reul wanbyeokhage jonjunghaji mothageona taimaauti siljero neomu jagdaneun sinho-neun doel su itsseumnida.
Taimaauti jinatdamyeon, ilbanjeogeuro context.DeadlineExceededwa gwanryeondoen oryu-reul boseul geosimnida. Geuligo geureon context-eseoui ctx.Err() yeoksido context.DeadlineExceededga doemnida. Ige pyojun mekeonijeumimnida: deadline-i dochakhayeotgo, context-ga chwisodoeeotgo, geu gyeolgwa jageopdo kkeutnaya hamnida.
Gwa jangdo obeo-do anin, jom deo mal-i doeneun jinsang yeje:
package main
import (
"context"
"errors"
"log"
"net/http"
"time"
)
func shutdownWithLog(srv *http.Server) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Printf("shutdown: timeout exceeded")
return
}
log.Printf("shutdown error: %v", err)
}
}
Yeogiseoneun oryu-ui cheungeul dul ro bunrihae saenggakhaeya hamnida:
- http.ErrServerClosedneun seobeoga jeongsangjeogeuro 닫힐 ttae ListenAndServeeseo naoneun yesangdoen oryuimnida (seobeoreul sijakhaneun jitjeomeseo igeol pilteoringhamnida).
- context.DeadlineExceededneun shutdown jeolcha-ga deadline an-e kkeutnaji motaetdaneun sinhoimnida (igeon shutdown bun-gi-eseo roginghamnida).
3. Shutdown taimaaut-eul seontaekhaneun bangbeop
Danjaljeogeuro “5cho-ro seoljeonghamyeon da doel geoyeyo”raneun sutjareul ju-go sip-eul su itsseumnida. Hajiman peurogeuraeming-eseo majik neombeoneun jal meokhiji anseumnida (42neun yeoeaga itjiman, geugeon dareun ganguiimnida). Geu daesin ireoke saenggakhaeya hamnida: seobeoga yocheongdeureul jeongsangjeogeuro kkeutnaegi wihae eolmana-ui sigani piryohan-ga?
Nonlineun ireotseumnida: shutdown taimaauti chungbunhi gireoseo dangsin-ui “botong” yocheongdeuri kkeutnal su isseoya hako, dongsie chungbunhi jjalbaseo peuroseseuga yeongwonhi meomchuji anneun sangtaega doeeoseoneun andoemnida. Manyak dangsin-i mandeuneun geosi APIigo, ilbanjeogin yocheongi 20–50 millisecond jeongdo geollindamyeon 30cho shutdown-eun neoreobge neukkyeojil geosimnida. Banhadae-ro siljero 2–3cho-ga geolineun jageobi itdamyeon (ye: mugeoun aggregation), 1cho shutdown-eun jaju peuroseseureul neomu iljjik kkeuneobeoril geosimnida.
Go-eseo contextga deadline-gwa chwiso-reul call stack araero jeonpahagi ttaemune, olbareun aekitekcheo-raneun ilbanjeogeuro haendeulleowa uijonseongi hamnijeogin sigan an-e kkeutnage hageona, ani-myeon context-e ttara jeongsangjeogeuro chwiso-doedorok mandeumnida.
Gyoyungyong task seobeo(yesireul deureo todo/tasks)eseoneun 5cho-eseo sijakhaedo jo-seumnida. Haeksimeun sutja jachega anira, geuge jonjaehago kodeu-e myeongsihage sseyeo ittaneun geosimnida.
Joeun seutail-eun taimaaut-eul sangsuro ppaeneun geosimnida. Geuraeya nonli soge mutyeo sarajiji anseumnida:
package main
import "time"
const shutdownTimeout = 5 * time.Second
4. main()eseoui graceful shutdown: sinho, taimaaut, Shutdown
Jeonche heulleum
main()eseo seobeoneun ilbanjeogeuro ListenAndServe()reul tonghae sijakdoego heulleumeul beullokinghamnida. Jongryo sinho-neun “bakke”eseo deureoogi ttaemune, urineun geu sinho-reul gidarida-ga jongryoreul sijakhal su inneun jari-ga piryohamnida. Ireul wihae signal.NotifyContextwa <-sigCtx.Done() daegireul sayonghago, geu dwi-e taimaauti inneun olbareun Shutdowneul chugahamnida.
Gaenyumjeogeuro-neun da-eumgwa gatseumnida:
flowchart TD
A[main: HTTP seobeo sijak] --> B[ListenAndServega beullokingdoem]
A --> C["goroutine: <-sigCtx.Done() daegi"]
C --> D["shutdownCtx = WithTimeout(...) saengseong"]
D --> E["srv.Shutdown(shutdownCtx) hoechul"]
E --> F[ListenAndServe jongryo]
Seobeo sijak mit sinho daegi
Kodeu yeje (yeogiseoneun muxga imi junbidoeeotdago gajeonghamnida):
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
mux := http.NewServeMux()
srv := &http.Server{Addr: ":8080", Handler: mux}
sigCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go shutdownOnSignal(sigCtx, srv)
err := srv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("server error: %v", err)
}
}
Jeongmal jungyohan buneun shutdownOnSignal soge sumgyeojyeo itsseumnida. Yeogiseoneun taimaauti pilsuiga 되도록 ireoke guhyeonhamnida:
package main
import (
"context"
"log"
"net/http"
"time"
)
const shutdownTimeout = 5 * time.Second
func shutdownOnSignal(sigCtx context.Context, srv *http.Server) {
<-sigCtx.Done() // jongryo sinho-reul gidarim
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v", err)
}
}
Yeogiseo jalmogo bwaya hal diseong-i itsseumnida. Urineun WithTimeout(context.Background(), ...)reul sseuji, WithTimeout(sigCtx, ...)reul sseuji anseumnida. Wae geureolkka-yo? sigCtxneun shutdownkkaji dochakhaenneun sungan imi chwisodoeeo itsseumnida (uri-ga <-sigCtx.Done()eseo ppajyeo nawatgiro haesseunikka-yo), geureom igeol parent-ro samneun sungan “baro chwisodoen” context-reul eotge doemnida. Geureohamyeon shutdown-eun siljillo amu geotdo gidariji motage doemnida.
Geuraeseo shutdown bun-gieseo-neun ttaro “jungnipjeogin” parent-in context.Background()reul jaju sayonghamnida.
Wae Shutdowneul geunyang “ListenAndServe dwi”e hoechulhamyeon andoeneun-ga
Chobojaga jaju ppajineun hamjeongeun da-eumgwa gatseumnida:
err := srv.ListenAndServe()
srv.Shutdown(...)
Cheoeumen nonri-jeogeuro boyimnida. “Seobeoga kkeutnamyeon geuttae shutdownhajiyo.” Hajiman ListenAndServeneun beulloking hoechulimnida. Seobeoga dongjakhaneun dongan-eun i jul-i dorawa juji anseumnida. Geureoni Shutdownkkajineun ajik dochakhal su eopsseumnida.
Ttaemune shutdown-eun “yeope seo” silhaengdoemnida. bo-tong-eun goroutine-eul sayonghamnida: juyo heulleumeun seobeoreul unyeonghago, byeoldo heulleumeun jongryo sinho-reul gidaryeotda-ga shutdown-eul bulleojumnida. Ige “mabeop”i anira geunyang seoro dareun du gae ui byeolhaeng jageopimnida.
Gadokseongeul wihan main() mini rifaegteoring
Kodeuga keojimyeon main()eun geumbang “seupageti kodeu”ga doemnida: rutingdo, DIdo, rogingdo, shutdown-do modu han gose seokkigi ttaemuni-jyo. Ajigeun simhwadoen layer aekitekcheoreul mandeul pilyoneun eopsjiman, jageun rifaegteoringman-eurodo chungbunhi doumi doemnida: seobeo jorib-eun ham-suro ppago, main()eneun saengmyeong jugi-man namgineun geosimnida.
Yeje:
package main
import (
"net/http"
)
func newServer() *http.Server {
mux := http.NewServeMux()
// mux.HandleFunc(...) // handlersneun imi apeseo jaksseongdoem
return &http.Server{Addr: ":8080", Handler: mux}
}
Geureom main()i jom deo “kkaekkeushaejimnida”:
package main
import (
"context"
"os"
"os/signal"
"syscall"
)
func main() {
srv := newServer()
sigCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go shutdownOnSignal(sigCtx, srv)
runServer(srv)
}
Geuligo runServerneun http.ErrServerClosedreul pilteoringhaneun gosimnida:
package main
import (
"errors"
"log"
"net/http"
)
func runServer(srv *http.Server) {
err := srv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("server error: %v", err)
}
}
Ireoke gineungeul ham-su byeollo nanumyeon joeun jeomeun, ije-neun geoui mulri-jeogeurodo Shutdowneul taimaaut eopsi “sil suro” bulgiga himdeureojindaneun geosimnida: shutdown-i meomuneun ham-suga han gose jeongri-doeeo itgo, geu an-e imi WithTimeouti deureo itgi ttaemunijyo.
5. Server.Shutdown(ctx)wa taimaauteseo jaju haneun silsu
Silsu 1: “danhaeseo pyeonha니까” Shutdown(context.Background())reul sseunda.
Igeoya말로 gajang wiheomhan “pyeonham”imnida. Ireoke hamyeon peuroseseu jongryoga bokbulbok-i doemnida. Eotteon yocheongi meomchwo beorimyeon, Shutdowneun aju ora gidaril su itsseumnida. Haegyeolbeop-eun dan순하지만 gyuyuljeogimnida: shutdown context-neun hangsang context.WithTimeout(...)euro-man mandeulgo, taimaaut gapseun sangsuro myeongsihamnida.
Silsu 2: WithTimeouteun mandeureotjiman cancel()eul ppeomeokneunda.
Taimaauti seuseuro manryodoendago haedo, cancel()eun context-wa yeonggyeondoen riseoseureul olbareuge haejeneun haengdongimnida. Go-eseoneun ilbanjeogeuro cancel()eul hangsang hoechulhamnida. Bodong-eun context-reul saengseonghan jikhu defer cancel()euro cheorihamnida. I jageun seupgwan hana-ga uioero keun siseutem-eseo manheun seuteureseureul julyeo jum니다.
Silsu 3: imi chwisodoen context-reul shutdown context-ui parent-ro sseunda.
Heunhan si나리오는 ireotseumnida: <-sigCtx.Done()reul gidarin dwi, context.WithTimeout(sigCtx, 5*time.Second)reul buleumnida. Geureona sigCtxneun imi chwisodoeeotgo, geu gyeolgwa child context-do baro chwisodoebnida. Geureom shutdown-eun “sungan jeuksig” kkeutnago gidarimeun saerobge saenggiji anseumnida. Olbareun bangbeopeun shutdown bun-gieseo context.Background()eseo saeroun context-reul mandeuneun geosimnida.
Silsu 4: http.ErrServerClosedreul “seobeo oryu”ro roginghanda.
Igeon jeongsang jeongji ttae ListenAndServega banhwanhaneun yesangdoen jongryo-sangtaeimnida. Igeol jinjja oryu-cheoreom namgimyeon log-ga si끄러워지고 jalmosdoen gyeongboga saenggimnida. Seobeoreul sijakhaneun jijeomeseo errors.Is(err, http.ErrServerClosed)ro pilteoringhaseyo.
Silsu 5: Shutdown oryu-reul geunyang museuhanda.
Manyak Shutdowni taimaaut an-e kkeutnaji motaetdamyeon, jeogeodo log-lo-neun geu sasil-eul ara야 hamnida. Gyoyungyong peurojekteu-eseodo mesijireul namgyeo du-myeon joeumnida. Geureoji anheumyeon “modeun geosi areumdapge kkeutnasseo”rago midge doejiman, siljero-neun ilbu yocheongi deadline ttaemune jalryeot-eul su itsseumnida.
GO TO FULL VERSION