shutdown

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

Package shutdown provides a mechanism for graceful shutdown of gin.

Support graceful shutdown of gin during an interrupt signal. This tool captures the Linux SIGINT and SIGKILL signals, so it won't work (completely) with Apple or Windows.

Import Packages

import (
    "github.com/gin-gonic/gin"
    "github.com/madkins23/gin-utils/pkg/shutdown"
)

Initialize Server

graceful := &shutdown.Graceful{}
graceful.Initialize()
defer graceful.Close()

The defer statement will automatically shut down and cleanup after the server when the enclosing scope is exited.

Configure Router

router := gin.Default()
router.GET("/exit", handler.Exit)

Actual router configuration will depend on the application.

Run Server:

if err := graceful.Serve(router, port); err != nil {
    log.Fatal().Err(err).Msg("Running gin server")
}

The server will shut down gracefully if the process receives either a SIGINT or SIGKILL signal.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Graceful

type Graceful struct {
	// Embed configuration information.
	// If not provided port can be specified in Serve() method.
	system.Config
	// contains filtered or unexported fields
}
Example
graceful := &Graceful{}
graceful.Initialize()
defer graceful.Close()

router := gin.New() // not gin.Default()
router.Use(ginzero.Logger())
router.GET("/ping", handler.Ping)

go func() {
	if err := server.WaitFor(url, timeout); err != nil {
		log.Logger.Error().Err(err).Msg("Waiting for server")
	}
	time.Sleep(25 * time.Millisecond)
	if err := server.Interrupt(); err != nil {
		log.Logger.Error().Err(err).Msg("Unable to interrupt server")
	}
}()

if err := graceful.Serve(router, port); err != nil {
	log.Logger.Fatal().Err(err).Msg("Running gin server example")
}
Output:

func (*Graceful) Close

func (g *Graceful) Close()

Close the Graceful object, stopping signal capture.

func (*Graceful) Initialize

func (g *Graceful) Initialize()

Initialize configures the Graceful object.

func (*Graceful) Serve

func (g *Graceful) Serve(router *gin.Engine, port uint) error

Serve executes the gin service as defined. Service is done in a separate goroutine but this method waits until service is done.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL