Skip to content

Commit dac0f2a

Browse files
authored
remove parent context from the httpcluster Runnable (#47)
1 parent e6ccf98 commit dac0f2a

File tree

5 files changed

+9
-62
lines changed

5 files changed

+9
-62
lines changed

examples/httpcluster/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ func createHTTPCluster(
241241
// Create the httpcluster
242242
cluster, err := httpcluster.NewRunner(
243243
httpcluster.WithLogger(logger.WithGroup("httpcluster")),
244-
httpcluster.WithContext(ctx),
245244
)
246245
if err != nil {
247246
return nil, nil, fmt.Errorf("failed to create httpcluster: %w", err)

runnables/httpcluster/options.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package httpcluster
22

33
import (
4-
"context"
54
"fmt"
65
"log/slog"
76
"time"
@@ -12,14 +11,6 @@ import (
1211
// Option is a function that configures a Runner.
1312
type Option func(*Runner) error
1413

15-
// WithContext sets the parent context for the cluster.
16-
func WithContext(ctx context.Context) Option {
17-
return func(r *Runner) error {
18-
r.parentCtx = ctx
19-
return nil
20-
}
21-
}
22-
2314
// WithLogger sets the logger for the cluster.
2415
func WithLogger(logger *slog.Logger) Option {
2516
return func(r *Runner) error {

runnables/httpcluster/options_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package httpcluster
22

33
import (
4-
"context"
54
"log/slog"
65
"testing"
76

@@ -74,18 +73,15 @@ func TestOptionApplicationOrder(t *testing.T) {
7473

7574
// Test that multiple options are applied correctly
7675
logger := slog.Default().WithGroup("test")
77-
ctx := context.Background()
7876

7977
runner, err := NewRunner(
8078
WithLogger(logger),
81-
WithContext(ctx),
8279
WithStateChanBufferSize(15),
8380
WithSiphonBuffer(3),
8481
)
8582
require.NoError(t, err)
8683

8784
assert.Equal(t, logger, runner.logger)
88-
assert.Equal(t, ctx, runner.parentCtx)
8985
assert.Equal(t, 15, runner.stateChanBufferSize)
9086
assert.Equal(t, 3, cap(runner.configSiphon))
9187
}

runnables/httpcluster/runner.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ type Runner struct {
2727
restartDelay time.Duration
2828
deadlineServerStart time.Duration
2929

30-
// Context management - similar to composite pattern
31-
parentCtx context.Context // Set during construction
32-
runCtx context.Context // Set during Run()
33-
runCancel context.CancelFunc
30+
// Set by Run()
31+
ctx context.Context
32+
cancel context.CancelFunc
3433

3534
// Configuration siphon channel
3635
configSiphon chan map[string]*httpserver.Config
@@ -76,7 +75,6 @@ func NewRunner(opts ...Option) (*Runner, error) {
7675
logger: slog.Default().WithGroup("httpcluster.Runner"),
7776
restartDelay: defaultRestartDelay,
7877
deadlineServerStart: defaultDeadlineServerStart,
79-
parentCtx: context.Background(),
8078
configSiphon: make(
8179
chan map[string]*httpserver.Config,
8280
), // unbuffered by default
@@ -172,8 +170,8 @@ func (r *Runner) Run(ctx context.Context) error {
172170
r.mu.Lock()
173171
runCtx, runCancel := context.WithCancel(ctx)
174172
defer runCancel()
175-
r.runCtx = runCtx
176-
r.runCancel = runCancel
173+
r.ctx = runCtx
174+
r.cancel = runCancel
177175
r.mu.Unlock()
178176

179177
// Transition to running (no servers initially)
@@ -189,10 +187,6 @@ func (r *Runner) Run(ctx context.Context) error {
189187
logger.Debug("Run context cancelled, initiating shutdown")
190188
return r.shutdown(runCtx)
191189

192-
case <-r.parentCtx.Done():
193-
logger.Debug("Parent context cancelled, initiating shutdown")
194-
return r.shutdown(runCtx)
195-
196190
case newConfigs, ok := <-r.configSiphon:
197191
if !ok {
198192
logger.Debug("Config siphon closed, initiating shutdown")
@@ -214,7 +208,7 @@ func (r *Runner) Stop() {
214208
logger.Debug("Stopping")
215209

216210
r.mu.Lock()
217-
r.runCancel()
211+
r.cancel()
218212
r.mu.Unlock()
219213
}
220214

runnables/httpcluster/runner_test.go

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,17 @@ func TestNewRunner(t *testing.T) {
8989
require.NotNil(t, runner)
9090

9191
assert.NotNil(t, runner.logger)
92-
assert.NotNil(t, runner.parentCtx)
9392
assert.NotNil(t, runner.configSiphon)
9493
assert.NotNil(t, runner.currentEntries)
9594
assert.NotNil(t, runner.fsm)
9695
assert.Equal(t, finitestate.StatusNew, runner.GetState())
9796
})
9897

9998
t.Run("with options", func(t *testing.T) {
100-
ctx := t.Context()
10199
runner, err := NewRunner(
102-
WithContext(ctx),
103100
WithSiphonBuffer(1),
104101
)
105102
require.NoError(t, err)
106-
assert.Equal(t, ctx, runner.parentCtx)
107103

108104
cfg := make(map[string]*httpserver.Config)
109105
select {
@@ -558,7 +554,7 @@ func TestRunnerExecuteActions(t *testing.T) {
558554
ctx := t.Context()
559555

560556
runner.mu.Lock()
561-
runner.runCtx = ctx
557+
runner.ctx = ctx
562558
runner.mu.Unlock()
563559

564560
mockEntries := &MockEntriesManager{}
@@ -603,35 +599,6 @@ func TestRunnerExecuteActions(t *testing.T) {
603599

604600
func TestRunnerContextManagement(t *testing.T) {
605601
t.Parallel()
606-
t.Run("parent context cancellation", func(t *testing.T) {
607-
parentCtx, parentCancel := context.WithCancel(t.Context())
608-
609-
runner, err := NewRunner(WithContext(parentCtx))
610-
require.NoError(t, err)
611-
612-
runCtx := context.Background()
613-
614-
runErr := make(chan error, 1)
615-
go func() {
616-
runErr <- runner.Run(runCtx)
617-
}()
618-
619-
// Wait for running
620-
require.Eventually(t, func() bool {
621-
return runner.IsRunning()
622-
}, time.Second, 10*time.Millisecond)
623-
624-
// Cancel parent context
625-
parentCancel()
626-
627-
// Should stop gracefully
628-
select {
629-
case err := <-runErr:
630-
assert.NoError(t, err)
631-
case <-time.After(time.Second):
632-
t.Fatal("Runner should stop when parent context cancelled")
633-
}
634-
})
635602

636603
t.Run("run context setup", func(t *testing.T) {
637604
runner, err := NewRunner()
@@ -652,8 +619,8 @@ func TestRunnerContextManagement(t *testing.T) {
652619

653620
// Check run context is set
654621
runner.mu.RLock()
655-
runCtx := runner.runCtx
656-
runCancel := runner.runCancel
622+
runCtx := runner.ctx
623+
runCancel := runner.cancel
657624
runner.mu.RUnlock()
658625

659626
assert.NotNil(t, runCtx)

0 commit comments

Comments
 (0)