Documentation
¶
Index ¶
Constants ¶
View Source
const Version = version
Variables ¶
This section is empty.
Functions ¶
func Closers ¶
func Closers() closerS
Closers returns the container includes all registered closable objects.
The simplest ways is using package level Close function:
func main() { defer is.Closers().Close() // others statements ... is.Closers().RegisterCloseFns(func(){ sth.Close() }) // more statements... }
func Signals ¶
func Signals() signalS
Signals returns a signals-helper so that you can catch them, and raise them later.
Typically, its usage is `catcher := is.Signals().Catch(); ...`.
By default, catcher will listen on standard signals set: os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT.
But you can change them with:
is.Signals().Catch(os.Kill, os.Interrupt)
or:
is.Signals().Catch().WithSignals(os.Interrupt, os.Kill)
You should put your long-term codes inside `cb` of WaitFor(cb), and defer call to `closer()` in. The `closer()` is a param of `cb`.
For example:
package main import ( "context" "fmt" "os" "sync" "github.com/hedzr/env" "github.com/hedzr/go-socketlib/net" logz "logslog" ) func main() { dbglog.SetLevel(dbglog.DebugLevel) server := net.NewServer(":7099") defer server.Close() ctx, cancel := context.WithCancel(context.Background()) defer cancel() catcher := is.Signals().Catch() catcher.WithVerboseFn(func(msg string, args ...any) { dbglog.Debug(fmt.Sprintf("[verbose] %s", fmt.Sprintf(msg, args...))) }). WithOnSignalCaught(func(sig os.Signal, wg *sync.WaitGroup) { println() dbglog.Debug("signal caught", "sig", sig) if err := server.Shutdown(); err != nil { dbglog.Error("server shutdown error", "err", err) } cancel() }). WaitFor(func(closer func()) { dbglog.Debug("entering looper's loop...") server.WithOnShutdown(func(err error) { closer() }) err := server.StartAndServe(ctx) if err != nil { dbglog.Fatal("server serve failed", "err", err) } }) }
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.