grace

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 13 Imported by: 0

README

Gracefully restart the process

How to use it

package main

func main() {
	g, err := grace.NewNetGrace(grace.NetGraceOption{})
	if err != nil {
		return
	}
	defer g.Close()

	if _, err := g.Listen("tcp", "127.0.0.1:8080"); err != nil {
		return
	}

	ready, err := g.Ready()
	if err != nil {
		return
	}

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
	for {
		select {
		case <-ready:
			signal.Stop(sig)
			return
		case s, _ := <-sig:
			switch s {
			case syscall.SIGHUP:
				grace.Fork()
			case syscall.SIGTERM, syscall.SIGINT:
				signal.Stop(sig)
				return
			}
		}
	}
}

Documentation

Index

Constants

View Source
const (
	PathMetadata = "/metadata"
	PathReady    = "/ready"
)
View Source
const (
	DefaultUpgradeSock = "grace_upgrade.sock"
)

Variables

This section is empty.

Functions

func Fork

func Fork() (int, error)

func GetFiles

func GetFiles(uc *net.UnixConn, filenames []string) ([]*os.File, error)

GetFiles receives file descriptors from a Unix domain socket.

You need to close all files in the returned slice. The slice can be non-empty even if this function returns an error.

Use net.FileConn() if you're receiving a network connection.

func SendFiles

func SendFiles(uc *net.UnixConn, fds []int) error

SendFiles sends file descriptors to Unix domain socket.

func SetLogger

func SetLogger(l Logger)

func StopListener

func StopListener(ln net.Listener) error

Types

type FdsMetadata

type FdsMetadata struct {
	Filenames []fileName
}

type Logger

type Logger interface {
	Trace(string, ...interface{})
	Debug(string, ...interface{})
	Info(string, ...interface{})
	Warn(string, ...interface{})
	Error(string, ...interface{})
}

type NetGrace

type NetGrace struct {
	// contains filtered or unexported fields
}

NetGrace handles zero downtime upgrades and passing files between processes.

func NewNetGrace

func NewNetGrace(option NetGraceOption) (*NetGrace, error)

func (*NetGrace) Close

func (n *NetGrace) Close() error

func (*NetGrace) CloseIdleFiles

func (n *NetGrace) CloseIdleFiles() error

func (*NetGrace) CloseListener

func (n *NetGrace) CloseListener(ln net.Listener)

func (*NetGrace) Inherit

func (n *NetGrace) Inherit() error

Inherit load listener from old process by unixSocket 1. Dail to upgradeSocket to determine whether there is an old process 2. Reads length of fds from old process. Returns if length is 0 3. Response ack after bind listenSocket. Gets fds by listenSocket.

func (*NetGrace) ListInheritFilenames

func (n *NetGrace) ListInheritFilenames() []fileName

func (*NetGrace) Listen

func (n *NetGrace) Listen(network, address string) (net.Listener, error)

Listen return Listener if it in the inherited map and move it to used list otherwise net.Listen

func (*NetGrace) Ready

func (n *NetGrace) Ready() (chan struct{}, error)

type NetGraceOption

type NetGraceOption struct {
	UpgradeSock string
}

type NetGraceProtocol

type NetGraceProtocol struct {
	Data interface{}
}

func (*NetGraceProtocol) ReadFrom

func (n *NetGraceProtocol) ReadFrom(r io.Reader) (int64, error)

func (*NetGraceProtocol) WriteTo

func (n *NetGraceProtocol) WriteTo(w io.Writer) (int64, error)

type UpgradeRequest

type UpgradeRequest struct {
	Path string
}

Jump to

Keyboard shortcuts

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