Skip to content

Commit

Permalink
remove manager interface (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha authored Mar 9, 2023
1 parent 720347a commit e7bbad7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 51 deletions.
15 changes: 7 additions & 8 deletions component/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,17 @@ func (s *HTTPServerSuite) TestHTTPServer() {
s.Run(t.name, func() {
var opts []xrun.Option
if t.wantShutdownTimeout {
opts = append(opts, xrun.WithGracefulShutdownTimeout(time.Nanosecond))
opts = append(opts, xrun.ShutdownTimeout(time.Nanosecond))
}

m := xrun.NewManager(opts...)
st := s.T()

s.NoError(m.Add(HTTPServer(
HTTPServerOptions{
Server: t.server,
PreStart: func() {
s.T().Log("PreStart called")
},
PreStop: func() {
s.T().Log("PreStop called")
},
Server: t.server,
PreStart: func() { st.Log("PreStart called") },
PreStop: func() { st.Log("PreStop called") },
},
)))

Expand Down
13 changes: 4 additions & 9 deletions component/x/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,16 @@ func (s *ServerTestSuite) TestServer() {
srv := grpc.NewServer()

l, err := t.newListener()
st := s.T()

s.NoError(m.Add(Server(Options{
Server: srv,
NewListener: func() (net.Listener, error) {
return l, err
},
PreStart: func() {
s.T().Log("PreStart called")
},
PreStop: func() {
s.T().Log("PreStop called")
},
PostStop: func() {
s.T().Log("PostStop called")
},
PreStart: func() { st.Log("PreStart called") },
PreStop: func() { st.Log("PreStop called") },
PostStop: func() { st.Log("PostStop called") },
})))

errCh := make(chan error, 1)
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func ExampleNewManager() {
m := xrun.NewManager(xrun.WithGracefulShutdownTimeout(xrun.NoTimeout))
m := xrun.NewManager(xrun.ShutdownTimeout(xrun.NoTimeout))

if err := m.Add(component.HTTPServer(component.HTTPServerOptions{Server: &http.Server{}})); err != nil {
panic(err)
Expand Down
34 changes: 13 additions & 21 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,20 @@ import (
"github.com/hashicorp/go-multierror"
)

// Manager helps to run multiple components
// and waits for them to complete
type Manager interface {
Component

// Add will include the Component, and the Component will
// start running when Run is called.
Add(Component) error
}

// NewManager creates a Manager and applies provided Option
func NewManager(opts ...Option) Manager {
m := &manager{
shutdownTimeout: NoTimeout,
}
func NewManager(opts ...Option) *Manager {
m := &Manager{shutdownTimeout: NoTimeout}

for _, o := range opts {
o(m)
o.apply(m)
}

return m
}

type manager struct {
// Manager helps to run multiple components
// and waits for them to complete
type Manager struct {
mu sync.Mutex

internalCtx context.Context
Expand All @@ -49,7 +41,7 @@ type manager struct {

// Add will enqueue the Component to run it,
// last added component will be started first
func (m *manager) Add(c Component) error {
func (m *Manager) Add(c Component) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -68,7 +60,7 @@ func (m *manager) Add(c Component) error {
// Run starts running the registered components. The components will stop running
// when the context is closed. Run blocks until the context is closed or
// an error occurs.
func (m *manager) Run(ctx context.Context) (err error) {
func (m *Manager) Run(ctx context.Context) (err error) {
m.internalCtx, m.internalCancel = context.WithCancel(ctx)

defer func() {
Expand All @@ -89,7 +81,7 @@ func (m *manager) Run(ctx context.Context) (err error) {
}
}

func (m *manager) start() {
func (m *Manager) start() {
m.mu.Lock()
defer m.mu.Unlock()
m.started = true
Expand All @@ -101,7 +93,7 @@ func (m *manager) start() {
}
}

func (m *manager) startComponent(c Component) {
func (m *Manager) startComponent(c Component) {
m.wg.Add(1)
go func() {
defer m.wg.Done()
Expand All @@ -111,7 +103,7 @@ func (m *manager) startComponent(c Component) {
}()
}

func (m *manager) engageStopProcedure() error {
func (m *Manager) engageStopProcedure() error {
var shutdownCancel context.CancelFunc
if m.shutdownTimeout > 0 {
m.shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), m.shutdownTimeout)
Expand Down
4 changes: 2 additions & 2 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *ManagerSuite) TestNewManager() {
},
{
name: "WithGracefulShutdownErrorOnOneComponent",
options: []Option{WithGracefulShutdownTimeout(5 * time.Second)},
options: []Option{ShutdownTimeout(5 * time.Second)},
wantErr: assert.Error,
components: []Component{
ComponentFunc(func(ctx context.Context) error {
Expand All @@ -70,7 +70,7 @@ func (s *ManagerSuite) TestNewManager() {
},
{
name: "WithGracefulShutdownForTwoLongRunningComponents",
options: []Option{WithGracefulShutdownTimeout(time.Minute)},
options: []Option{ShutdownTimeout(time.Minute)},
wantErr: assert.NoError,
components: []Component{
ComponentFunc(func(ctx context.Context) error {
Expand Down
18 changes: 11 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ const (
)

// Option changes behaviour of Manager
type Option func(*manager)

// WithGracefulShutdownTimeout allows max timeout after which Manager exits
func WithGracefulShutdownTimeout(timeout time.Duration) Option {
return func(m *manager) {
m.shutdownTimeout = timeout
}
type Option interface {
apply(*Manager)
}

// ShutdownTimeout allows max timeout after which Manager exits.
type ShutdownTimeout time.Duration

func (t ShutdownTimeout) apply(m *Manager) { m.shutdownTimeout = time.Duration(t) }

// WithGracefulShutdownTimeout allows max timeout after which Manager exits.
// Deprecated: Use ShutdownTimeout instead.
func WithGracefulShutdownTimeout(timeout time.Duration) Option { return ShutdownTimeout(timeout) }
4 changes: 2 additions & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import (
func TestWithGracefulShutdownTimeout(t *testing.T) {
expected := time.Minute

m := NewManager(WithGracefulShutdownTimeout(expected))
assert.Equal(t, expected, m.(*manager).shutdownTimeout)
m := NewManager(ShutdownTimeout(expected))
assert.Equal(t, expected, m.shutdownTimeout)
}
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// and adds all the components to it. Calling .Run()
// on returned ComponentFunc will call Run on the Manager
func All(shutdownTimeout time.Duration, components ...Component) ComponentFunc {
m := NewManager(WithGracefulShutdownTimeout(shutdownTimeout))
m := NewManager(ShutdownTimeout(shutdownTimeout))
for _, c := range components {
// we can ignore error as `m` is not returned
// and no one can call m.Add() outside
Expand Down

0 comments on commit e7bbad7

Please sign in to comment.