-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graceful shutdown http server. (#308)
* add graceful shutdown for the http server.
- Loading branch information
Showing
2 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |