Documentation
¶
Overview ¶
Package deps helps managing lifecycle of the application's dependencies and shutting down gracefully, with minimalistic API.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dependency ¶
type Dependency struct {
// contains filtered or unexported fields
}
Dependency is a controller of the worker depends on the parent. After receiving abort signal from the parent, wait its dependent's stop and notify the parent of its Stop.
func (*Dependency) AbortContext ¶
func (d *Dependency) AbortContext() context.Context
AbortContext returns a context given to (*Root).Abort. The worker on behalf of this controller can get the deadline of shutdown from the context, if specified.
func (*Dependency) Aborted ¶
func (d *Dependency) Aborted() <-chan struct{}
Aborted returns a channel that's closed when its Root aborted. After the close of Aborted channel, the worker on behalf of this controller will have to start shutdown process including its dependents.
func (*Dependency) Dependent ¶
func (d *Dependency) Dependent() *Dependency
Dependent creates the controller depends on this controller. Dependency should be created before the statement creating the goroutine or other event to be waited for. Otherwise, a data race could occur. Dependency uses sync.WaitGroup internally. For detail, see sync.WaitGroup.Add.
func (*Dependency) Stop ¶
func (d *Dependency) Stop(abortOnError *error)
Stop marks the worker on behalf of this controller stopped after all dependents stopped. If abortOnError indicates error, this requests Root to abort.
func (*Dependency) StopImmediately ¶ added in v0.0.3
func (d *Dependency) StopImmediately(abortOnError *error)
StopImmediately marks the worker on behalf of this controller stopped, even if its any dependents still working. If abortOnError indicates error, this requests Root to abort.
func (*Dependency) Wait ¶
func (d *Dependency) Wait() <-chan struct{}
Wait returns a channel that's closed when its all dependents stopped. To shutdown gracefully, the worker on behalf of this controller have to wait the stop of its children before starting its shutdown process.
type Root ¶
type Root struct {
// contains filtered or unexported fields
}
Root is a root controller and describe its dependents using (*Root).Dependent. Root can send signal of shutdown to all its dependents.
func New ¶
func New() *Root
New creates Root controller.
Example ¶
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/daichitakahashi/deps"
)
func main() {
root := deps.New()
go func(dep *deps.Dependency) {
defer dep.Stop(nil)
for {
select {
case <-dep.Aborted():
return
default:
for i := 0; i < 3; i++ {
time.Sleep(time.Second)
fmt.Printf("...%d", i)
}
fmt.Println()
}
}
}(root.Dependent())
time.Sleep(time.Second * 4)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err := root.Abort(ctx)
if err != nil {
log.Fatal(err)
}
}
Output: ...0...1...2 ...0...1...2
func (*Root) Abort ¶
Abort fires shutdown of the application. When all dependents stopped successfully, it returns nil. The context given as argument can be accessed via (Dependency).AbortContext.
func (*Root) AbortRequested ¶ added in v0.0.3
func (r *Root) AbortRequested() <-chan struct{}
func (*Root) Dependent ¶
func (r *Root) Dependent() *Dependency
Dependent creates the controller depends on this root. Dependency should be created before the statement creating the goroutine or other event to be waited for. Otherwise, a data race could occur. Root uses sync.WaitGroup internally. For detail, see sync.WaitGroup.Add.