Replacing channel based graceful shutdown with Context and errgroup based solution

For quite some time, I have been using the following code in my http server application to initiate a graceful shutdown. Stripped off unnecessary codes that aren’t contributing right now.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shutdownChan := make(chan error)
go func() {
quitChan := make(chan os.Signal, 1)
signal.Notify(quitChan, syscall.SIGINT, syscall.SIGTERM)
<-quitChan
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
shutdownChan <- srv.Shutdown(ctx)
}()
err := srv.ListenAndServe()
err = <-shutdownChan
</code>
<code>shutdownChan := make(chan error) go func() { quitChan := make(chan os.Signal, 1) signal.Notify(quitChan, syscall.SIGINT, syscall.SIGTERM) <-quitChan ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second) defer cancel() shutdownChan <- srv.Shutdown(ctx) }() err := srv.ListenAndServe() err = <-shutdownChan </code>
shutdownChan := make(chan error)

go func() {
    quitChan := make(chan os.Signal, 1)
    signal.Notify(quitChan, syscall.SIGINT, syscall.SIGTERM)
    <-quitChan

    ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
    defer cancel()

    shutdownChan <- srv.Shutdown(ctx)
}()

err := srv.ListenAndServe()

err = <-shutdownChan

To make sure I know what I am doing here, I will rubber duck a bit.

First, shutdownChan is used to receive errors that might occur during the shutdown process inside the goroutine.

Inside the go func() function, first we need a way to hold the execution of the goroutine so that it doesn’t exit. We declare a channel for receiving signal, and the <-quitChan line keeps waiting for the signal and blocks the execution.

When a signal is passed, eg. Ctrl+C is pressed, execution moves to the next line. We create a context which will get a timeout after 10 seconds and call the cancel() function, aka stop all works and release the resources. Then we call the srv.Shutdown(ctx) function, return any error to the main goroutine through the previously created shutdownChan channel.

I hope all clear so far (kindly point out if I have any misunderstanding about any concept). Now I want to do two things.

  1. Use signal.NotifyContext in place of signal.Notify.
  2. Use errgroup to return errors from the goroutine instead of using a channel.

What I figured so far about the new code is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
return srv.Shutdown(shutdownCtx)
})
err := g.Wait()
</code>
<code>ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() g, ctx := errgroup.WithContext(ctx) g.Go(func() error { <-ctx.Done() shutdownCtx, cancel := context.WithTimeout(context.Background(), 10 * time.Second) defer cancel() return srv.Shutdown(shutdownCtx) }) err := g.Wait() </code>
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

g, ctx := errgroup.WithContext(ctx)

g.Go(func() error {
    <-ctx.Done()
    
    shutdownCtx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
    defer cancel()

    return srv.Shutdown(shutdownCtx)
})

err := g.Wait()

I won’t explain the details what I am doing here since it’s intended to achieve the same thing as before in same concept, but I have this itchy feeling if this would really work. Part of it probably because I am still having doubt about my understanding of how context, channel, errgroup are working under the hood. I have a few questions now.

  1. Is there any fault in the new code?
  2. Compared to previous approach and this, which one is overall better and should be recommended over another?
  3. Can I update the new code to make it better? Or any better graceful shutdown approach?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật