go-scopes/lib/context/context.go

143 lines
2.6 KiB
Go

package context
import (
stdlib_context "context"
"sync"
"time"
"git.sr.ht/~cco/go-scopes/lib"
)
type Context = lib.Context
type ContextState = lib.ContextState
type Services = lib.Services
type context struct {
cfg lib.Config
parent Context
children []Context
state ContextState
mailbox chan lib.Message
}
func (ctx *context) Config() lib.Config {
return ctx.cfg
}
func (ctx *context) Parent() Context {
return ctx.parent
}
func (ctx *context) Services() Services {
return ctx.parent.Services()
}
func (ctx *context) ChildContext(cfg lib.Config) Context {
cctx := makeCtx(cfg, ctx)
ctx.Services()[cfg.Name()] = cctx
ctx.children = append(ctx.children, cctx)
return cctx
}
func (ctx *context) State() ContextState {
return ctx.state
}
func (ctx *context) WithState(state ContextState) Context {
ctx.state = state
return ctx
}
func (ctx *context) Mailbox() chan lib.Message {
return ctx.mailbox
}
func (ctx *context) WaitGroup() *sync.WaitGroup {
return ctx.parent.WaitGroup()
}
func (ctx *context) Done() <-chan struct{} {
return ctx.parent.Done()
}
func (ctx *context) Stop() {
// ctx.Warn("Only application-level service can be stopped
}
func makeCtx(cfg lib.Config, parent Context) *context {
return &context{
cfg: cfg,
parent: parent,
mailbox: make(chan lib.Message, 10),
}
}
// top-level application context
type appContext struct {
*context
services Services
waitgroup *sync.WaitGroup
doneCh chan struct{}
}
func (ctx *appContext) ChildContext(cfg lib.Config) Context {
cctx := makeCtx(cfg, ctx)
ctx.services[cfg.Name()] = cctx
ctx.children = append(ctx.children, cctx)
return cctx
}
func (ctx *appContext) Services() Services {
return ctx.services
}
func (ctx *appContext) WithState(state ContextState) Context {
ctx.state = state
return ctx
}
func (ctx *appContext) WaitGroup() *sync.WaitGroup {
return ctx.waitgroup
}
func (ctx *appContext) Done() <-chan struct{} {
return ctx.doneCh
}
func (ctx *appContext) Stop() {
close(ctx.doneCh)
}
func AppContext(cfg lib.Config) Context {
ctx := &appContext{
context: makeCtx(cfg, nil),
services: Services{},
waitgroup: &sync.WaitGroup{},
doneCh: make(chan struct{}),
}
ctx.services[cfg.Name()] = ctx
return ctx
}
// implement interface context.Context from standard library
func (ctx *context) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}
func (ctx *context) Err() error {
select {
case _, ok := <-ctx.Done():
if !ok {
return stdlib_context.Canceled
}
default:
return nil
}
return nil
}
func (ctx *context) Value(key interface{}) interface{} {
return nil
}