graceful

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 10 Imported by: 5

README

graceful

Run Tests codecov Go Report Card GoDoc

Gin wrapper to enable graceful termination when shutting down a process

Example

package main

import (
  "context"
  "net/http"
  "os/signal"
  "syscall"

  "github.com/gin-contrib/graceful"
  "github.com/gin-gonic/gin"
)

func main() {
  ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  defer stop()

  router, err := graceful.Default()
  if err != nil {
    panic(err)
  }
  defer router.Close()

  router.GET("/", func(c *gin.Context) {
    c.String(http.StatusOK, "Welcome Gin Server")
  })

  if err := router.RunWithContext(ctx); err != nil && err != context.Canceled {
    panic(err)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyStarted = errors.New("already started router")

ErrAlreadyStarted is returned when trying to start a router that has already been started

View Source
var ErrNotStarted = errors.New("router not started")

ErrNotStarted is returned when trying to stop a router that has not been started

Functions

This section is empty.

Types

type Graceful

type Graceful struct {
	*gin.Engine
	// contains filtered or unexported fields
}

Graceful is a wrapper around a gin.Engine that provides graceful shutdown

func Default

func Default(opts ...Option) (*Graceful, error)

Default returns a Graceful gin instance with the Logger and Recovery middleware already attached.

func New

func New(router *gin.Engine, opts ...Option) (*Graceful, error)

New returns a Graceful gin instance from the given gin.Engine.

func (*Graceful) Close

func (g *Graceful) Close()

Close gracefully shuts down the server. It first shuts down the server using the Shutdown method, then it performs any cleanup operations registered with the server. Finally, it resets the server's internal state.

func (*Graceful) Run

func (g *Graceful) Run(addr ...string) error

Run attaches the router to a http.Server and starts listening and serving HTTP requests.

func (*Graceful) RunFd

func (g *Graceful) RunFd(fd uintptr) error

RunFd attaches the router to a http.Server and starts listening and serving HTTP requests through the specified file descriptor.

func (*Graceful) RunListener

func (g *Graceful) RunListener(listener net.Listener) error

RunListener attaches the router to a http.Server and starts listening and serving HTTP requests through the specified net.Listener

func (*Graceful) RunTLS

func (g *Graceful) RunTLS(addr, certFile, keyFile string) error

RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.

func (*Graceful) RunUnix

func (g *Graceful) RunUnix(file string) error

RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests through the specified unix socket (i.e. a file).

func (*Graceful) RunWithContext

func (g *Graceful) RunWithContext(ctx context.Context) error

RunWithContext attaches the router to the configured http.Server (fallback to configuring one on :8080 if none are configured) and starts listening and serving HTTP requests. If the passed context is canceled, the server is gracefully shut down

func (*Graceful) Shutdown

func (g *Graceful) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server without interrupting any active connections.

func (*Graceful) Start

func (g *Graceful) Start() error

Start will start the Graceful instance and all underlying http.Servers in a separate goroutine and return right away. You must call Stop and not Shutdown if you use Start.

func (*Graceful) Stop

func (g *Graceful) Stop() error

Stop will stop the Graceful instance previously started with Start. It will return once the instance has been stopped.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option specifies instrumentation configuration options.

func WithAddr

func WithAddr(addr string) Option

WithAddr configure a http.Server to listen on the given address.

func WithFd

func WithFd(fd uintptr) Option

WithFd configure a http.Server to listen on the given file descriptor.

func WithListener

func WithListener(l net.Listener) Option

WithListener configure a http.Server to listen on the given net.Listener.

func WithTLS

func WithTLS(addr string, certFile string, keyFile string) Option

WithTLS configure a http.Server to listen on the given address and serve HTTPS requests.

func WithUnix

func WithUnix(file string) Option

WithUnix configure a http.Server to listen on the given unix socket file.

Jump to

Keyboard shortcuts

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