graceful

package module
v0.1.1-0...-7e7960e Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2020 License: MIT Imports: 12 Imported by: 0

README

graceful

A simple package for creating socket listeners to restart servers gracefully.

Installation

Import the package by using go mod:

import "github.com/jqqjj/graceful"

Usage

Use in net/http:

ln, err := graceful.NewGraceListener(":8080")
if err != nil {
    log.Fatalln(err)
}
http.Serve(ln, nil)

Use in gin framework:

ln, err := graceful.NewGraceListener(":8080")
if err != nil {
    log.Fatalln(err)
}

router := gin.Default()
router.GET("/", func(c *gin.Context) {
    c.String(http.StatusOK, "this is a test")
})
router.RunListener(ln)

Graceful restart:

kill -HUP PID

Run Args:

  • daemon    Run app as a daemon

Features

  • Will not close connections including http actively.
  • Old process will not exit until all connections are completed and closed.
  • Restart gracefully is not implemented on windows but you can develop and run your business codes on it.

Thanks:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Logger       *log.Logger
	ArgDaemonKey = "daemon"

	ForkTimeout    = time.Second * 10
	ForkTimeoutErr = errors.New("forking timeout")

	SigGraceFinish  = syscall.Signal(12)
	SigGraceRestart = syscall.SIGHUP
)

Functions

This section is empty.

Types

type GraceConnection

type GraceConnection struct {
	net.Conn
	// contains filtered or unexported fields
}

func NewGraceConnection

func NewGraceConnection(wg *sync.WaitGroup, c net.Conn) *GraceConnection

func (*GraceConnection) Close

func (c *GraceConnection) Close() error

type GraceListener

type GraceListener struct {
	net.Listener
	// contains filtered or unexported fields
}

func NewGraceListener

func NewGraceListener(addr string) (*GraceListener, error)

func (*GraceListener) Accept

func (l *GraceListener) Accept() (net.Conn, error)

func (*GraceListener) Close

func (l *GraceListener) Close() error

Jump to

Keyboard shortcuts

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