octavore/naga

Add support for context.Context

Opened this issue · 1 comments

naga should special case context.Context so that you can inject a context.Background.

Here's a simple implementation:

package context

import (
	"context"
	"github.com/octavore/naga/service"
)

// Module provides a Context whose lifecycle is managed by naga.
type Module struct {
	context.Context
	cancel context.CancelFunc
}

var _ service.Module = &Module{}

// Init implements github.com/octavore/naga/service.
func (m *Module) Init(c *service.Config) {
	c.Setup = func() error {
		m.Context, m.cancel = context.WithCancel(context.Background())
		return nil
	}
	c.Stop = func() {
		m.cancel()
	}
}