Skip to content

Commit

Permalink
graceful shutdown http server. (#308)
Browse files Browse the repository at this point in the history
* add graceful shutdown for the http server.
  • Loading branch information
spankie authored Sep 28, 2024
1 parent 8011499 commit f88ef9d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
42 changes: 36 additions & 6 deletions cmd/template/framework/files/main/fiber_main.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"{{.ProjectName}}/internal/server"

_ "github.com/joho/godotenv/autoload"
)

func gracefulShutdown(fiberServer *server.FiberServer) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

// Listen for the interrupt signal.
<-ctx.Done()

log.Println("shutting down gracefully, press Ctrl+C again to force")

// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := fiberServer.ShutdownWithContext(ctx); err != nil {
log.Printf("Server forced to shutdown with error: %v", err)
}

log.Println("Server exiting")
}

func main() {

server := server.New()

server.RegisterFiberRoutes()
port, _ := strconv.Atoi(os.Getenv("PORT"))
err := server.Listen(fmt.Sprintf(":%d", port))
if err != nil {
panic(fmt.Sprintf("cannot start server: %s", err))
}
}
go func() {
port, _ := strconv.Atoi(os.Getenv("PORT"))
err := server.Listen(fmt.Sprintf(":%d", port))
if err != nil {
panic(fmt.Sprintf("http server error: %s", err))
}
}()

gracefulShutdown(server)
}
37 changes: 34 additions & 3 deletions cmd/template/framework/files/main/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
"os/signal"
"syscall"
"time"

"{{.ProjectName}}/internal/server"
)

func gracefulShutdown(apiServer *http.Server) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

// Listen for the interrupt signal.
<-ctx.Done()

log.Println("shutting down gracefully, press Ctrl+C again to force")

// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := apiServer.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown with error: %v", err)
}

log.Println("Server exiting")
}


func main() {

server := server.NewServer()

go gracefulShutdown(server)

err := server.ListenAndServe()
if err != nil {
panic(fmt.Sprintf("cannot start server: %s", err))
if err != nil && err != http.ErrServerClosed {
panic(fmt.Sprintf("http server error: %s", err))
}
}
}

0 comments on commit f88ef9d

Please sign in to comment.