gitea: code.gitea.io/gitea/modules/graceful Index | Files

package graceful

import "code.gitea.io/gitea/modules/graceful"

Index

Package Files

context.go manager.go manager_unix.go net_unix.go restart_unix.go server.go server_hooks.go server_http.go

Variables

var (
    ErrShutdown  = fmt.Errorf("Graceful Manager called Shutdown")
    ErrHammer    = fmt.Errorf("Graceful Manager called Hammer")
    ErrTerminate = fmt.Errorf("Graceful Manager called Terminate")
)

Errors for context.Err()

var (
    // DefaultReadTimeOut default read timeout
    DefaultReadTimeOut time.Duration
    // DefaultWriteTimeOut default write timeout
    DefaultWriteTimeOut time.Duration
    // DefaultMaxHeaderBytes default max header bytes
    DefaultMaxHeaderBytes int
)

func CloseProvidedListeners Uses

func CloseProvidedListeners() error

CloseProvidedListeners closes all unused provided listeners.

func GetListener Uses

func GetListener(network, address string) (net.Listener, error)

GetListener obtains a listener for the local network address. The network must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It returns an provided net.Listener for the matching network and address, or creates a new one using net.Listen.

func GetListenerTCP Uses

func GetListenerTCP(network string, address *net.TCPAddr) (*net.TCPListener, error)

GetListenerTCP announces on the local network address. The network must be: "tcp", "tcp4" or "tcp6". It returns a provided net.Listener for the matching network and address, or creates a new one using net.ListenTCP.

func GetListenerUnix Uses

func GetListenerUnix(network string, address *net.UnixAddr) (*net.UnixListener, error)

GetListenerUnix announces on the local network address. The network must be: "unix" or "unixpacket". It returns a provided net.Listener for the matching network and address, or creates a new one using net.ListenUnix.

func HTTPListenAndServe Uses

func HTTPListenAndServe(network, address string, handler http.Handler) error

HTTPListenAndServe listens on the provided network address and then calls Serve to handle requests on incoming connections.

func HTTPListenAndServeTLS Uses

func HTTPListenAndServeTLS(network, address, certFile, keyFile string, handler http.Handler) error

HTTPListenAndServeTLS listens on the provided network address and then calls Serve to handle requests on incoming connections.

func HTTPListenAndServeTLSConfig Uses

func HTTPListenAndServeTLSConfig(network, address string, tlsConfig *tls.Config, handler http.Handler) error

HTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve to handle requests on incoming connections.

func InitManager Uses

func InitManager(ctx context.Context)

InitManager creates the graceful manager in the provided context

func KillParent Uses

func KillParent()

KillParent sends the kill signal to the parent process if we are a child

func RestartProcess Uses

func RestartProcess() (int, error)

RestartProcess starts a new process passing it the active listeners. It doesn't fork, but starts a new process using the same environment and arguments as when it was originally started. This allows for a newly deployed binary to be started. It returns the pid of the newly started process when successful.

type CallbackWithContext Uses

type CallbackWithContext func(ctx context.Context, callback func())

CallbackWithContext is combined runnable and context to watch to see if the caller has finished

type ChannelContext Uses

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

ChannelContext is a context that wraps a channel and error as a context

func NewChannelContext Uses

func NewChannelContext(done <-chan struct{}, err error) *ChannelContext

NewChannelContext creates a ChannelContext from a channel and error

func (*ChannelContext) Deadline Uses

func (ctx *ChannelContext) Deadline() (deadline time.Time, ok bool)

Deadline returns the time when work done on behalf of this context should be canceled. There is no Deadline for a ChannelContext

func (*ChannelContext) Done Uses

func (ctx *ChannelContext) Done() <-chan struct{}

Done returns the channel provided at the creation of this context. When closed, work done on behalf of this context should be canceled.

func (*ChannelContext) Err Uses

func (ctx *ChannelContext) Err() error

Err returns nil, if Done is not closed. If Done is closed, Err returns the error provided at the creation of this context

func (*ChannelContext) Value Uses

func (ctx *ChannelContext) Value(key interface{}) interface{}

Value returns nil for all calls as no values are or can be associated with this context

type Manager Uses

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

Manager manages the graceful shutdown process

func GetManager Uses

func GetManager() *Manager

GetManager returns the Manager

func (*Manager) Deadline Uses

func (g *Manager) Deadline() (deadline time.Time, ok bool)

Deadline returns nil as there is no fixed Deadline for the manager, it allows the manager to be viewed as a context.Context

func (*Manager) DoGracefulRestart Uses

func (g *Manager) DoGracefulRestart()

DoGracefulRestart causes a graceful restart

func (*Manager) DoGracefulShutdown Uses

func (g *Manager) DoGracefulShutdown()

DoGracefulShutdown causes a graceful shutdown

func (*Manager) DoImmediateHammer Uses

func (g *Manager) DoImmediateHammer()

DoImmediateHammer causes an immediate hammer

func (*Manager) Done Uses

func (g *Manager) Done() <-chan struct{}

Done allows the manager to be viewed as a context.Context, it returns a channel that is closed when the server is finished terminating

func (*Manager) Err Uses

func (g *Manager) Err() error

Err allows the manager to be viewed as a context.Context done at Terminate, it returns ErrTerminate

func (*Manager) HammerContext Uses

func (g *Manager) HammerContext() context.Context

HammerContext returns a context.Context that is Done at hammer Callers using this context should ensure that they are registered as a running server in order that they are waited for.

func (*Manager) InformCleanup Uses

func (g *Manager) InformCleanup()

InformCleanup tells the cleanup wait group that we have either taken a listener or will not be taking a listener

func (*Manager) IsChild Uses

func (g *Manager) IsChild() bool

IsChild returns if the current process is a child of previous Gitea process

func (*Manager) IsHammer Uses

func (g *Manager) IsHammer() <-chan struct{}

IsHammer returns a channel which will be closed at hammer The order of closure is IsShutdown, IsHammer (potentially), IsTerminate Servers running within the running server wait group should respond to IsHammer if not shutdown already

func (*Manager) IsShutdown Uses

func (g *Manager) IsShutdown() <-chan struct{}

IsShutdown returns a channel which will be closed at shutdown. The order of closure is IsShutdown, IsHammer (potentially), IsTerminate

func (*Manager) IsTerminate Uses

func (g *Manager) IsTerminate() <-chan struct{}

IsTerminate returns a channel which will be closed at terminate The order of closure is IsShutdown, IsHammer (potentially), IsTerminate IsTerminate will only close once all running servers have stopped

func (*Manager) RegisterServer Uses

func (g *Manager) RegisterServer()

RegisterServer registers the running of a listening server, in the case of unix this means that the parent process can now die. Any call to RegisterServer must be matched by a call to ServerDone

func (*Manager) RunAtHammer Uses

func (g *Manager) RunAtHammer(ctx context.Context, hammer func())

RunAtHammer creates a go-routine to run the provided function at shutdown

func (*Manager) RunAtShutdown Uses

func (g *Manager) RunAtShutdown(ctx context.Context, shutdown func())

RunAtShutdown creates a go-routine to run the provided function at shutdown

func (*Manager) RunAtTerminate Uses

func (g *Manager) RunAtTerminate(ctx context.Context, terminate func())

RunAtTerminate adds to the terminate wait group and creates a go-routine to run the provided function at termination

func (*Manager) RunWithShutdownChan Uses

func (g *Manager) RunWithShutdownChan(run RunnableWithShutdownChan)

RunWithShutdownChan takes a function that has channel to watch for shutdown and atTerminate callbacks After the atShutdown channel is closed, the main function must return once shutdown is complete. (Optionally IsHammer may be waited for instead however, this should be avoided if possible.) The callback function provided to atTerminate must return once termination is complete. Please note that use of the atTerminate function will create a go-routine that will wait till terminate - users must therefore be careful to only call this as necessary.

func (*Manager) RunWithShutdownContext Uses

func (g *Manager) RunWithShutdownContext(run func(context.Context))

RunWithShutdownContext takes a function that has a context to watch for shutdown. After the provided context is Done(), the main function must return once shutdown is complete. (Optionally the HammerContext may be obtained and waited for however, this should be avoided if possible.)

func (*Manager) RunWithShutdownFns Uses

func (g *Manager) RunWithShutdownFns(run RunnableWithShutdownFns)

RunWithShutdownFns takes a function that has both atShutdown and atTerminate callbacks After the callback to atShutdown is called and is complete, the main function must return. Similarly the callback function provided to atTerminate must return once termination is complete. Please note that use of the atShutdown and atTerminate callbacks will create go-routines that will wait till their respective signals - users must therefore be careful to only call these as necessary. If run is not expected to run indefinitely RunWithShutdownChan is likely to be more appropriate.

func (*Manager) ServerDone Uses

func (g *Manager) ServerDone()

ServerDone declares a running server done and subtracts one from the running server wait group. Users probably do not want to call this and should use one of the RunWithShutdown* functions

func (*Manager) ShutdownContext Uses

func (g *Manager) ShutdownContext() context.Context

ShutdownContext returns a context.Context that is Done at shutdown Callers using this context should ensure that they are registered as a running server in order that they are waited for.

func (*Manager) TerminateContext Uses

func (g *Manager) TerminateContext() context.Context

TerminateContext returns a context.Context that is Done at terminate Callers using this context should ensure that they are registered as a terminating server in order that they are waited for.

func (*Manager) Value Uses

func (g *Manager) Value(key interface{}) interface{}

Value allows the manager to be viewed as a context.Context done at Terminate, it has no values

func (*Manager) WaitForServers Uses

func (g *Manager) WaitForServers()

WaitForServers waits for all running servers to finish. Users should probably instead use AtTerminate or IsTerminate

func (*Manager) WaitForTerminate Uses

func (g *Manager) WaitForTerminate()

WaitForTerminate waits for all terminating actions to finish. Only the main go-routine should use this

type RunnableWithShutdownChan Uses

type RunnableWithShutdownChan func(atShutdown <-chan struct{}, atTerminate CallbackWithContext)

RunnableWithShutdownChan is a runnable with functions to run at shutdown and terminate. After the atShutdown channel is closed, the main function must return once shutdown is complete. (Optionally IsHammer may be waited for instead however, this should be avoided if possible.) The callback function provided to atTerminate must return once termination is complete. Please note that use of the atTerminate function will create a go-routine that will wait till terminate - users must therefore be careful to only call this as necessary.

type RunnableWithShutdownFns Uses

type RunnableWithShutdownFns func(atShutdown, atTerminate func(context.Context, func()))

RunnableWithShutdownFns is a runnable with functions to run at shutdown and terminate After the callback to atShutdown is called and is complete, the main function must return. Similarly the callback function provided to atTerminate must return once termination is complete. Please note that use of the atShutdown and atTerminate callbacks will create go-routines that will wait till their respective signals - users must therefore be careful to only call these as necessary. If run is not expected to run indefinitely RunWithShutdownChan is likely to be more appropriate.

type ServeFunction Uses

type ServeFunction = func(net.Listener) error

ServeFunction represents a listen.Accept loop

type Server Uses

type Server struct {
    BeforeBegin func(network, address string)
    OnShutdown  func()
    // contains filtered or unexported fields
}

Server represents our graceful server

func NewServer Uses

func NewServer(network, address string) *Server

NewServer creates a server on network at provided address

func (*Server) ListenAndServe Uses

func (srv *Server) ListenAndServe(serve ServeFunction) error

ListenAndServe listens on the provided network address and then calls Serve to handle requests on incoming connections.

func (*Server) ListenAndServeTLS Uses

func (srv *Server) ListenAndServeTLS(certFile, keyFile string, serve ServeFunction) error

ListenAndServeTLS listens on the provided network address and then calls Serve to handle requests on incoming TLS connections.

Filenames containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.

func (*Server) ListenAndServeTLSConfig Uses

func (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFunction) error

ListenAndServeTLSConfig listens on the provided network address and then calls Serve to handle requests on incoming TLS connections.

func (*Server) Serve Uses

func (srv *Server) Serve(serve ServeFunction) error

Serve accepts incoming HTTP connections on the wrapped listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them. Handler is typically nil, in which case the DefaultServeMux is used.

In addition to the standard Serve behaviour each connection is added to a sync.Waitgroup so that all outstanding connections can be served before shutting down the server.

Package graceful imports 21 packages (graph) and is imported by 59 packages. Updated 2021-01-22. Refresh now. Tools for package owners.