Documentation
¶
Index ¶
- Variables
- func Close()
- func Raise(sig syscall.Signal) error
- func RegisterClosable(servers ...Closable)
- func RegisterCloseFn(fn func())
- func RegisterCloseFns(fns ...func())
- func RegisterClosers(cc ...Closer)
- func RegisterPeripheral(servers ...Peripheral)
- type AutoStart
- type Basic
- type Catcher
- type Closable
- type Closer
- type Infrastructure
- type OldInfra
- type OnLooper
- type OnSignalCaught
- type Peripheral
Constants ¶
This section is empty.
Variables ¶
var VerboseFn func(msg string, args ...any)
Functions ¶
func Close ¶
func Close()
Close will cleanup all registered closers. You must make a call to Close before your app shutting down. For example:
func main() { defer basics.Close() // ... }
func RegisterClosable ¶
func RegisterClosable(servers ...Closable)
RegisterClosable adds a peripheral/closable into our global closers set. a basics.Peripheral object is a closable instance.
func RegisterCloseFn ¶
func RegisterCloseFn(fn func())
RegisterCloseFn adds a simple closure into our global closers set
func RegisterCloseFns ¶
func RegisterCloseFns(fns ...func())
RegisterCloseFns adds a simple closure into our global closers set
func RegisterClosers ¶
func RegisterClosers(cc ...Closer)
RegisterClosers adds a simple closure into our global closers set
func RegisterPeripheral ¶
func RegisterPeripheral(servers ...Peripheral)
RegisterPeripheral adds a peripheral/closable into our global closers set. a basics.Peripheral object is a closable instance.
Types ¶
type AutoStart ¶
type AutoStart interface {
AutoStart()
}
AutoStart identify a peripheral object can be started automatically. see AddPeripheral.
type Basic ¶
type Basic struct {
// contains filtered or unexported fields
}
Basic is a base type to simplify your codes since you're using Peripheral type.
func (*Basic) AddPeripheral ¶
func (s *Basic) AddPeripheral(peripherals ...Peripheral)
AddPeripheral adds a Peripheral object into Basic holder/host.
A peripheral represents an external resource such as redis manager which manages the links to remote redis server, etc.. A peripheral can be auto-started implicit by AddPeripheral while it implements AutoStart interface.
type Catcher ¶
type Catcher interface { // WithSignals declares a set of signals which can be caught with // application processing logics. // // An empty call means the default set will be applying: // // signals = []os.Signal{os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP} // // These signals are hard-coded. WithSignals(signals ...os.Signal) Catcher // WithCloseHandlers gives the extra Peripheral's a chance // with safely shutting down. // // The better way is registering them with env.Closers().RegisterPeripheral(p). // All registered peripherals will be released/closed in the ending of Wait. WithCloseHandlers(onFinish ...Peripheral) Catcher // WithPrompt show a message before entering main loop. // // While many messages given, the final one will be used. // Use an empty call like WithPrompt() to request a default // prompt line. // // If you dislike printing anything, do Wait() directly // without WithPrompt call. WithPrompt(msg ...string) Catcher // WithOnSignalCaught setups handlers while any os signals caught by app. WithOnSignalCaught(cb ...OnSignalCaught) Catcher // WithOnLoop setups OnLooper handlers. // // Generally, the OnLooper handlers can be sent to Wait(...) while invoked. // But you can always register some looper(s) before Wait(a-main-looper). // // For example: // // is.Signals().Catch(). // WithOnLoop(redisStarter, etcdStarter, mongoStarter, dbStarter). // Wait(func(stopChan chan<- os.Signal, wgDone *sync.WaitGroup) { // // pop3server.Debug("entering looper's loop...") // // // setup handler: close catcher's waiting looper while 'pop3server' shut down // pop3server.WithOnShutdown(func(err error, ss net.Server) { wgShutdown.Done() }) // // go func() { // err := pop3server.ListenAndServe(ctx, nil) // if err != nil { // pop3server.Error("server serve failed", "err", err) // panic(err) // } // }() // }) WithOnLoop(cb ...OnLooper) Catcher // WithVerboseFn gives a change to log he catcher's internal state. WithVerboseFn(cb func(msg string, args ...any)) Catcher // Wait get the current thread blocked on reading 'done' channel // till an os signal break it. // // Each OnLooper must call wgDone.Done() to notify the looper was done. // // An optional way by sending os.Interrupt to stopChan could stop // catcher Wait loop programmatically, if necessary. Wait(stopperHandler OnLooper) }
Catcher is a builder to build the programmatic structure for entering a infinite loop and waiting for os signals caught or trigger anyone of them programmatically.
At the ending of program, all closers (see Peripheral and Close) will be closed safely, except panic in their Close codes.
func Catch ¶
Catch returns a builder to build the programmatic structure for entering a infinite loop and waiting for os signals caught or trigger anyone of them programmatically.
At the ending of program, all closers (see Peripheral and Close) will be closed safely, except panic in their Close codes.
For example,
basics.VerboseFn = t.Logf is.Signals().Catch(). WithPrompt(). Wait(func(stopChan chan<- os.Signal) { basics.VerboseFn("[cb] raising interrupt after a second...") time.Sleep(2500 * time.Millisecond) stopChan <- os.Interrupt basics.VerboseFn("[cb] raised.") })
A simple details can be found at:
https://www.developer.com/languages/os-signals-go/
Your logic that shutdown the main loop gracefully could be:
type appS struct{} func (s *appS) MainRunner(stopChan chan<- os.Signal, wgShutdown *sync.WaitGroup) { wgShutdown.Done() stopChan <- os.Interrupt } var app appS env.Signals().Catch(). Wait(app.MainRunner)
type Closable ¶
type Closable interface {
// Close provides a closer to cleanup the peripheral gracefully
Close()
}
Closable clears the hot data of an object before shutting down app.
type Closer ¶
type Closer interface {
Close() error // = io.Closer
}
Closer is synonym about io.Closer
type Infrastructure ¶
type Infrastructure interface { Peripheral // Open does initializing stuffs Open(ctx context.Context) (err error) }
type OnSignalCaught ¶
type Peripheral ¶
type Peripheral interface {
// Close provides a closer to cleanup the peripheral gracefully
Close()
}
Peripheral represents a basic infrastructure which can be initialized and destroyed.
For a Peripheral, the host should add it into a list and destroy them while host is shutting down.
func ClosersClosers ¶
func ClosersClosers() []Peripheral
ClosersClosers returns the closers set as a basics.Peripheral array