Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CleanUpFunc ¶
type CleanUpFunc func()
CleanUpFunc is a type for batch cleanups. It should be given to Graceful.RegisterCleanupFunctions method.
type Graceful ¶
type Graceful struct {
// contains filtered or unexported fields
}
Graceful type for registering cleanup functions and graceful shutdown. You can define as in the example below.
Example:
g := &Graceful{}
func (*Graceful) RegisterCleanupFunctions ¶
func (g *Graceful) RegisterCleanupFunctions(functions ...CleanUpFunc)
RegisterCleanupFunctions registers cleanup functions. You can pass the functions that should be cleaned up after graceful shutdown. They can be closing database, file, or even context cancelling.
func (*Graceful) Shutdown ¶
Shutdown listens given os signals. If any of them is triggered, the given http.Server is shutdown. Finally, all cleanup functions are triggered after the http.Server is shutdown. If no signal is passed to method, it means that all signals are listened. Graceful.Shutdown blocks the code until notified by any given signal.
Example:
srv := &http.Server{}
dbCleanUpFunc := func() {
db.Close()
}
fileCleanUpFunc := func() {
f.Close()
}
g := &Graceful{}
g.RegisterCleanupFunctions(dbCleanUpFunc, fileCleanUpFunc)
g.Shutdown(srv, 5*time.Second, os.Interrupt, syscall.SIGTERM)