아이리스 프레임워크는 우아한 종료를 지원합니다. 실제 비즈니스 시나리오에서 웹 애플리케이션을 다시 시작하거나 닫을 때 Go 프로세스를 직접 종료하면 비즈니스 프로세스가 완료되기 전에 중단될 수 있어 비즈니스 예외를 발생시킬 수 있습니다. 더 나은 접근 방식은 새 요청을 수락하는 것을 중지하고 진행 중인 요청을 처리한 후에 Go 프로세스를 종료하는 것입니다.
우아한 종료 또는 다시 시작을 수행하기 위해 여러 가지 방법을 사용할 수 있습니다. 이를 위해 목적에 맞게 구축된 써드 파티 패키지를 사용하거나 app.Shutdown(context.Context) 메서드를 사용할 수도 있습니다.
iris.RegisterOnInterrupt를 사용하여 CTRL/CMD+C 이벤트를 등록합니다:
idleConnsClosed := make(chan struct{})
iris.RegisterOnInterrupt(func() {
timeout := 10 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
// 모든 호스트를 종료합니다.
app.Shutdown(ctx)
close(idleConnsClosed)
})
// [...]
app.Listen(":8080", iris.WithoutInterruptHandler, iris.WithoutServerError(iris.ErrServerClosed))
<-idleConnsClosed