{"modulePath":"github.com/RedAFD/code-core","version":"v1.14.0-rc8","isLatest":false,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package graceful\n\n## Variables\n\n```go\nvar (\n\tErrShutdown  = fmt.Errorf(\"Graceful Manager called Shutdown\")\n\tErrHammer    = fmt.Errorf(\"Graceful Manager called Hammer\")\n\tErrTerminate = fmt.Errorf(\"Graceful Manager called Terminate\")\n)\n```\nErrors for context.Err()\n\n```go\nvar (\n\t// DefaultReadTimeOut default read timeout\n\tDefaultReadTimeOut time.Duration\n\t// DefaultWriteTimeOut default write timeout\n\tDefaultWriteTimeOut time.Duration\n\t// DefaultMaxHeaderBytes default max header bytes\n\tDefaultMaxHeaderBytes int\n)\n```\n\n## Functions\n\n```go\nfunc CloseProvidedListeners() error\n```\nCloseProvidedListeners closes all unused provided listeners.\n\n```go\nfunc GetListener(network, address string) (net.Listener, error)\n```\nGetListener 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.\n\n```go\nfunc GetListenerTCP(network string, address *net.TCPAddr) (*net.TCPListener, error)\n```\nGetListenerTCP 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.\n\n```go\nfunc GetListenerUnix(network string, address *net.UnixAddr) (*net.UnixListener, error)\n```\nGetListenerUnix 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.\n\n```go\nfunc HTTPListenAndServe(network, address, name string, handler http.Handler) error\n```\nHTTPListenAndServe listens on the provided network address and then calls Serve to handle requests on incoming connections.\n\n```go\nfunc HTTPListenAndServeTLS(network, address, name, certFile, keyFile string, handler http.Handler) error\n```\nHTTPListenAndServeTLS listens on the provided network address and then calls Serve to handle requests on incoming connections.\n\n```go\nfunc HTTPListenAndServeTLSConfig(network, address, name string, tlsConfig *tls.Config, handler http.Handler) error\n```\nHTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve to handle requests on incoming connections.\n\n```go\nfunc InitManager(ctx context.Context)\n```\nInitManager creates the graceful manager in the provided context\n\n```go\nfunc KillParent()\n```\nKillParent sends the kill signal to the parent process if we are a child\n\n```go\nfunc RestartProcess() (int, error)\n```\nRestartProcess 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.\n\n## Types\n\n```go\ntype CallbackWithContext func(ctx context.Context, callback func())\n```\nCallbackWithContext is combined runnable and context to watch to see if the caller has finished\n\n```go\ntype ChannelContext struct {\n\t// contains filtered or unexported fields\n}\n```\nChannelContext is a context that wraps a channel and error as a context\n\n```go\nfunc NewChannelContext(done \u003c-chan struct{}, err error) *ChannelContext\n```\nNewChannelContext creates a ChannelContext from a channel and error\n\n```go\nfunc (ctx *ChannelContext) Deadline() (deadline time.Time, ok bool)\n```\nDeadline returns the time when work done on behalf of this context should be canceled. There is no Deadline for a ChannelContext\n\n```go\nfunc (ctx *ChannelContext) Done() \u003c-chan struct{}\n```\nDone returns the channel provided at the creation of this context. When closed, work done on behalf of this context should be canceled.\n\n```go\nfunc (ctx *ChannelContext) Err() error\n```\nErr returns nil, if Done is not closed. If Done is closed, Err returns the error provided at the creation of this context\n\n```go\nfunc (ctx *ChannelContext) Value(key interface{}) interface{}\n```\nValue returns nil for all calls as no values are or can be associated with this context\n\n```go\ntype Manager struct {\n\t// contains filtered or unexported fields\n}\n```\nManager manages the graceful shutdown process\n\n```go\nfunc GetManager() *Manager\n```\nGetManager returns the Manager\n\n```go\nfunc (g *Manager) Deadline() (deadline time.Time, ok bool)\n```\nDeadline returns nil as there is no fixed Deadline for the manager, it allows the manager to be viewed as a context.Context\n\n```go\nfunc (g *Manager) DoGracefulRestart()\n```\nDoGracefulRestart causes a graceful restart\n\n```go\nfunc (g *Manager) DoGracefulShutdown()\n```\nDoGracefulShutdown causes a graceful shutdown\n\n```go\nfunc (g *Manager) DoImmediateHammer()\n```\nDoImmediateHammer causes an immediate hammer\n\n```go\nfunc (g *Manager) Done() \u003c-chan struct{}\n```\nDone allows the manager to be viewed as a context.Context, it returns a channel that is closed when the server is finished terminating\n\n```go\nfunc (g *Manager) Err() error\n```\nErr allows the manager to be viewed as a context.Context done at Terminate, it returns ErrTerminate\n\n```go\nfunc (g *Manager) HammerContext() context.Context\n```\nHammerContext 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.\n\n```go\nfunc (g *Manager) InformCleanup()\n```\nInformCleanup tells the cleanup wait group that we have either taken a listener or will not be taking a listener\n\n```go\nfunc (g *Manager) IsChild() bool\n```\nIsChild returns if the current process is a child of previous Gitea process\n\n```go\nfunc (g *Manager) IsHammer() \u003c-chan struct{}\n```\nIsHammer 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\n\n```go\nfunc (g *Manager) IsShutdown() \u003c-chan struct{}\n```\nIsShutdown returns a channel which will be closed at shutdown. The order of closure is IsShutdown, IsHammer (potentially), IsTerminate\n\n```go\nfunc (g *Manager) IsTerminate() \u003c-chan struct{}\n```\nIsTerminate 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\n\n```go\nfunc (g *Manager) RegisterServer()\n```\nRegisterServer 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\n\n```go\nfunc (g *Manager) RunAtHammer(ctx context.Context, hammer func())\n```\nRunAtHammer creates a go-routine to run the provided function at shutdown\n\n```go\nfunc (g *Manager) RunAtShutdown(ctx context.Context, shutdown func())\n```\nRunAtShutdown creates a go-routine to run the provided function at shutdown\n\n```go\nfunc (g *Manager) RunAtTerminate(ctx context.Context, terminate func())\n```\nRunAtTerminate adds to the terminate wait group and creates a go-routine to run the provided function at termination\n\n```go\nfunc (g *Manager) RunWithShutdownChan(run RunnableWithShutdownChan)\n```\nRunWithShutdownChan 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.\n\n```go\nfunc (g *Manager) RunWithShutdownContext(run func(context.Context))\n```\nRunWithShutdownContext 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.)\n\n```go\nfunc (g *Manager) RunWithShutdownFns(run RunnableWithShutdownFns)\n```\nRunWithShutdownFns 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.\n\n```go\nfunc (g *Manager) ServerDone()\n```\nServerDone 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\n\n```go\nfunc (g *Manager) ShutdownContext() context.Context\n```\nShutdownContext 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.\n\n```go\nfunc (g *Manager) TerminateContext() context.Context\n```\nTerminateContext 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.\n\n```go\nfunc (g *Manager) Value(key interface{}) interface{}\n```\nValue allows the manager to be viewed as a context.Context done at Terminate, it has no values\n\n```go\nfunc (g *Manager) WaitForServers()\n```\nWaitForServers waits for all running servers to finish. Users should probably instead use AtTerminate or IsTerminate\n\n```go\nfunc (g *Manager) WaitForTerminate()\n```\nWaitForTerminate waits for all terminating actions to finish. Only the main go-routine should use this\n\n```go\ntype RunnableWithShutdownChan func(atShutdown \u003c-chan struct{}, atTerminate CallbackWithContext)\n```\nRunnableWithShutdownChan 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.\n\n```go\ntype RunnableWithShutdownFns func(atShutdown, atTerminate func(context.Context, func()))\n```\nRunnableWithShutdownFns 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.\n\n```go\ntype ServeFunction = func(net.Listener) error\n```\nServeFunction represents a listen.Accept loop\n\n```go\ntype Server struct {\n\tBeforeBegin func(network, address string)\n\tOnShutdown  func()\n\t// contains filtered or unexported fields\n}\n```\nServer represents our graceful server\n\n```go\nfunc NewServer(network, address, name string) *Server\n```\nNewServer creates a server on network at provided address\n\n```go\nfunc (srv *Server) ListenAndServe(serve ServeFunction) error\n```\nListenAndServe listens on the provided network address and then calls Serve to handle requests on incoming connections.\n\n```go\nfunc (srv *Server) ListenAndServeTLS(certFile, keyFile string, serve ServeFunction) error\n```\nListenAndServeTLS listens on the provided network address and then calls Serve to handle requests on incoming TLS connections.\n\nFilenames 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.\n\n```go\nfunc (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFunction) error\n```\nListenAndServeTLSConfig listens on the provided network address and then calls Serve to handle requests on incoming TLS connections.\n\n```go\nfunc (srv *Server) Serve(serve ServeFunction) error\n```\nServe 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.\n\nIn 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.\n\n","path":"github.com/RedAFD/code-core/modules/graceful","name":"graceful","synopsis":"","isRedistributable":true}
